Hi,

At Mon, 21 Sep 2009 09:12:52 +0900,
Suraj Kurapati wrote in [ruby-core:25681]:
> Both Fiber#resume and rb_fiber_resume() are causing a segfault
> when called from inside a very simple C extension (see below).

Don't create a fiber inside a not-running thread.

$ cat bug-2127.c
#include <stdio.h>
#include <ruby.h>

VALUE
body(VALUE arg)
{
    printf("Fiber: Inside fiber body\n");
    fflush(stdout);
    return Qnil;
}

VALUE
run_body(VALUE arg)
{
    VALUE fib = rb_fiber_new(body, arg), dump;

    printf("Main: Creating Ruby fiber..."); 
    fflush(stdout);
    dump = rb_inspect(fib);
    rb_io_puts(1, &dump, rb_stdout);

    printf("Main: Going to resume fiber...\n"); 
    fflush(stdout);

    rb_fiber_resume(fib, 0, 0); /* <== SEGFAULT */

    printf("Main: Fiber resumed successfully.\n");
    fflush(stdout);
    return Qnil;
}

RUBY_GLOBAL_SETUP

int
main(int argc, char** argv)
{
    ruby_sysinit(&argc, &argv);

    {
	RUBY_INIT_STACK;
	ruby_init();

	rb_protect(run_body, Qnil, 0);
    }

    return ruby_cleanup(0);
}

$ make MAINOBJ='$(PROGRAM).o' PROGRAM=bug-2127 program LIBRUBYARG='$(LIBRUBY_A)' 
$ ./bug-2127
Main: Creating Ruby fiber...#<Fiber:0x2c365c>
Main: Going to resume fiber...
Fiber: Inside fiber body
Main: Fiber resumed successfully.

-- 
Nobu Nakada