[Bug:trunk] cannot resume Fiber from forked process

e$B1sF#$G$9!#e(B

fork e$B$N;R%W%m%;%9$+$i85%W%m%;%9$Ne(B Fiber e$B$,8F$Y$^$;$s!#e(B

$ ./ruby -e ’
f = Fiber.new { }
fork { f.resume }
Process.waitpid

-e:3:in resume': fiber called across trap (FiberError) from -e:3:inblock in ’
from -e:3:in fork' from -e:3:in

e$B$=$&$$$&;EMM$J$i$=$&$$$&;EMM$GJL$K9=$o$J$$$N$G$9$,!"e(Bfiber
called across trap e$B$H$$$&Nc30%a%C%;!<%8$O$*$+$7$$5$$,$7$^$9!#e(B

e$B$A$J$_$K!"e(Btrap e$B$r$^$?$,$C$Fe(B Fiber
e$B$r8F$V$3$H$O2DG=$J$h$&$G$9!#e(B

$ ./ruby -e ’
f = Fiber.new { p :ok }
trap(:USR1) { f.resume }
Process.kill :USR1, $$
sleep 0.2

:ok

$ ./ruby -e ’
f = nil
trap(:USR1) { f = Fiber.new { p :ok } }
Process.kill :USR1, $$
sleep 0.2
f.resume

:ok

e$B$3$N%A%’%C%/$O$b$&I,MW$J$$$N$G$O$J$$$G$7$g$&$+!#H?BP$,$J$1$l$Pe(B
e$B%3%_%C%H$7$h$&$H;W$$$^$9!#e(B

diff --git a/cont.c b/cont.c
index a77f35e…932b10e 100644
— a/cont.c
+++ b/cont.c
@@ -388,7 +388,6 @@ cont_restore_1(rb_context_t *cont)
th->state = sth->state;
th->status = sth->status;
th->tag = sth->tag;

  • th->trap_tag = sth->trap_tag;
    th->errinfo = sth->errinfo;
    th->first_proc = sth->first_proc;

@@ -621,9 +620,6 @@ rb_cont_call(int argc, VALUE *argv, VALUE contval)
if (cont->saved_thread.self != th->self) {
rb_raise(rb_eRuntimeError, “continuation called across threads”);
}

  • if (cont->saved_thread.trap_tag != th->trap_tag) {

  • rb_raise(rb_eRuntimeError, “continuation called across trap”);

  • }
    if (cont->saved_thread.fiber) {
    rb_fiber_t *fcont;
    GetFiberPtr(cont->saved_thread.fiber, fcont);
    @@ -940,9 +936,6 @@ fiber_switch(VALUE fibval, int argc, VALUE *argv,
    int is_resume)
    if (cont->saved_thread.self != th->self) {
    rb_raise(rb_eFiberError, “fiber called across threads”);
    }

  • else if (cont->saved_thread.trap_tag != th->trap_tag) {

  • rb_raise(rb_eFiberError, “fiber called across trap”);

  • }
    else if (fib->status == TERMINATED) {
    value = rb_exc_new2(rb_eFiberError, “dead fiber called”);
    if (th->fiber != fibval) rb_exc_raise(value);
    diff --git a/eval.c b/eval.c
    index c5a72dc…aa01d81 100644
    — a/eval.c
    +++ b/eval.c
    @@ -677,19 +677,14 @@ rb_protect(VALUE (* proc) (VALUE), VALUE data,
    int * state)
    int status;
    rb_thread_t *th = GET_THREAD();
    rb_control_frame_t *cfp = th->cfp;

  • struct rb_vm_trap_tag trap_tag;
    rb_jmpbuf_t org_jmpbuf;

  • trap_tag.prev = th->trap_tag;

  • PUSH_TAG();

  • th->trap_tag = &trap_tag;
    MEMCPY(&org_jmpbuf, &(th)->root_jmpbuf, rb_jmpbuf_t, 1);
    if ((status = EXEC_TAG()) == 0) {
    SAVE_ROOT_JMPBUF(th, result = (*proc) (data));
    }
    MEMCPY(&(th)->root_jmpbuf, &org_jmpbuf, rb_jmpbuf_t, 1);

  • th->trap_tag = trap_tag.prev;
    POP_TAG();

    if (state) {
    diff --git a/vm_core.h b/vm_core.h
    index b9b584a…b781765 100644
    — a/vm_core.h
    +++ b/vm_core.h
    @@ -347,10 +347,6 @@ struct rb_vm_tag {
    struct rb_vm_tag *prev;
    };

-struct rb_vm_trap_tag {

  • struct rb_vm_trap_tag *prev;
    -};

#define RUBY_VM_VALUE_CACHE_SIZE 0x1000
#define USE_VALUE_CACHE 0

@@ -414,7 +410,6 @@ typedef struct rb_thread_struct
int transition_for_lock;

 struct rb_vm_tag *tag;
  • struct rb_vm_trap_tag *trap_tag;

    int parse_in_eval;
    int mild_compile_error;

e$B%A%1%C%He(B #2621 e$B$,99?7$5$l$^$7$?!#e(B (by Yusuke E.)

e$B%9%F!<%?%9e(B Opene$B$+$ie(BClosede$B$KJQ99e(B
ruby -v ruby 1.9.2dev (2010-01-25 trunk 26406)
[i686-linux]e$B$K%;%C%He(B

e$BH?BP$,$J$+$C$?$N$G%3%_%C%H$7$^$7$?e(B (r26407)

http://redmine.ruby-lang.org/issues/show/2621

e$B%A%1%C%He(B #2621 e$B$,99?7$5$l$^$7$?!#e(B (by Yusuke E.)

[email protected]$5$s$K$3$N%A%’%C%/$OI,[email protected]$o$l$?$N$Ge(B revert
e$B$7$^$7$?!#e(B
rb_protect e$B$re(B callcc
e$B$GH4$1$i$l$k$H3N$+$K$^$:$=$&$G$7$?!#$9$_$^$;$s!#e(B

Fiber e$B$re(B resume

e$B$7$F$^$:$$Nc$O$h$/$o$+$j$^$;$s$,$H$j$"$($:La$7$^$7$?e(B

e$B$=$l$G$be(B trap e$B$OJQ$J$N$G!"e(Btrap_tag e$B$Oe(B protect_tag
e$B$K!"Nc30%a%C%;!<%8$Ne(B
across trap e$B$Oe(B across stack rewinding barrier
e$B$KCV$-49$($^$7$?!#e(B

http://redmine.ruby-lang.org/issues/show/2621