[Bug #595] Fiber ignores ensure clause

Bug #595: Fiber ignores ensure clause
http://redmine.ruby-lang.org/issues/show/595

e$B5/I<<Te(B: Koichi Sasada
e$B%9%F!<%?%9e(B: Open, e$BM%@hEYe(B: Normal
e$BC4Ev<Te(B: Koichi Sasada, e$B%+%F%4%je(B: core

Ruby e$B%W%m%;%9=*N;;~!$e(BFiber e$B$,e(B ensure e$B$rL5;k$7$^$9!%e(B
e$B$3$l$O!$A0$+$iD>$=$&$H;W$C$F<j$,$D$$$F$$$J$+$C$?LdBj$G$9!%e(B
10e$B7nKv$^$G$K$OD>$=$&$H;W$$$^$9!%7k9=J#;($J$N$G!$8e2s$7$K$7$F$$$^$7$^$7$?!%e(B

fib = Fiber.new{
begin
Fiber.yield :ok
ensure
puts “should be print out”
end
}
p fib.resume

e$B%A%1%C%He(B #595 e$B$,99?7$5$l$^$7$?!#e(B (by Roger P.)

Help me out–shouldn’t this print out only when you call fib.resume
twice?

fib = Fiber.new{
begin
Fiber.yield :ok
ensure
puts “should be print out”
end
}
p fib.resume
p fib.resume

prints out all right.

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

e$B%A%1%C%He(B #595 e$B$,99?7$5$l$^$7$?!#e(B (by Yuki S.)

e$B%Q%C%A$r=q$/$+!"$b$7$/$O%I%-%e%a%s%H$Ke(BKNOWN
BUGe$B$H$7$F=q$/!"$H$$$&$3$H$G!#e(B

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

[email protected]$G$9!%e(B

e$B!!JV;v$,?oJ,CY$/$J$C$F$7$^$C$F$9$_$^$;$s!%e(B

wanabe wrote::

e$B$+$J$jA0$N%A%1%C%H$G$9$,!"BjL>$N7o$K$D$$$F%Q%C%A$r=q$-$^$7$?!#e(B
e$B$b$7$^[email protected]$I$J$?$b%Q%C%A$r=q$+$l$F$$$J$$$h$&$J$i$48!F$$/[email protected]$5$$!#e(B

e$B!!<B$O!$0JA0;w$?$h$&$J$b$N$r:n$C$?$N$G$9$,!$$5$/$C$He(B SEGV
e$B$NMr$G!$LLE]$/e(B
e$B$5$$$J$!!$$H;W$C$FJ|CV$7$F$$$?$N$G$7$?!%$3$N%Q%C%A$G$9$H!$$?$H$($Pe(B
test-all e$B$H$+$O$I$&$G$7$?!)e(B

e$B%o%J%Y$H?=$7$^$9!#e(B

e$B$+$J$jA0$N%A%1%C%H$G$9$,!"BjL>$N7o$K$D$$$F%Q%C%A$r=q$-$^$7$?!#e(B
e$B$b$7$^[email protected]$I$J$?$b%Q%C%A$r=q$+$l$F$$$J$$$h$&$J$i$48!F$$/[email protected]$5$$!#e(B

Index: thread.c

— thread.c (e$B%j%S%8%g%se(B 23617)
+++ thread.c (e$B:n6H%3%T!<e(B)
@@ -293,6 +293,8 @@

static void rb_mutex_unlock_all(mutex_t *mutex, rb_thread_t *th);

+void rb_fiber_terminate_all(rb_thread_t *th);
+
void
rb_thread_terminate_all(void)
{
@@ -310,6 +312,7 @@

 thread_debug("rb_thread_terminate_all (main thread: %p)\n", (void 

*)th);
st_foreach(vm->living_threads, terminate_i, (st_data_t)th);

  • rb_fiber_terminate_all(th);

    while (!rb_thread_alone()) {
    PUSH_TAG();
    @@ -1210,6 +1213,7 @@
    thread_debug(“rb_thread_execute_interrupts: %ld\n”, err);

    if (err == eKillSignal || err == eTerminateSignal) {

  • rb_fiber_terminate_all(th);
    th->errinfo = INT2FIX(TAG_FATAL);
    TH_JUMP_TAG(th, TAG_FATAL);
    }
    Index: cont.c
    ===================================================================
    — cont.c (e$B%j%S%8%g%se(B 23617)
    +++ cont.c (e$B:n6H%3%T!<e(B)
    @@ -534,6 +534,7 @@
    case 0:
    return Qnil;
    case 1:

  •  case -1:
    

    return argv[0];
    default:
    return rb_ary_new4(argc, argv);
    @@ -946,6 +947,36 @@
    return fib->status != TERMINATED ? Qtrue : Qfalse;
    }

+static VALUE
+terminate_all_i(VALUE fibval)
+{

  • if (rb_fiber_alive_p(fibval)) {
  • VALUE value = rb_exc_new2(rb_eSystemExit, “terminate”);
  • return fiber_switch(fibval, -1, &value, 0);
  • }
    +}

+void
+rb_fiber_terminate_all(rb_thread_t *th)
+{

  • VALUE fibval;
  • rb_fiber_t *fib, *root_fib;
  • rb_thread_t *_th = GET_THREAD();
  • rb_thread_set_current(th);
  • fibval = th->root_fiber;
  • if (!RTEST(fibval)) return;
  • GetFiberPtr(fibval, root_fib);
  • fib = root_fib->prev_fiber;
  • while (fib != root_fib) {
  • rb_rescue2(terminate_all_i, fib->cont.self,
  •   0, 0, rb_eSystemExit);
    
  • fib = fib->prev_fiber;
  • }
  • rb_thread_set_current(_th);
    +}

/*

  • call-seq:
  • fiber.resume(args, ...) -> obj

e$B%o%J%Y$G$9!#e(B

2009/06/15 6:33 e$B$Ke(B SASADA Koichi[email protected]
e$B$5$s$O=q$-$^$7$?e(B:

wanabe wrote::

e$B$+$J$jA0$N%A%1%C%H$G$9$,!"BjL>$N7o$K$D$$$F%Q%C%A$r=q$-$^$7$?!#e(B
e$B$b$7$^[email protected]$I$J$?$b%Q%C%A$r=q$+$l$F$$$J$$$h$&$J$i$48!F$$/[email protected]$5$$!#e(B

e$B<B$O!$0JA0;w$?$h$&$J$b$N$r:n$C$?$N$G$9$,!$$5$/$C$He(B SEGV e$B$NMr$G!$LLE]$/e(B
e$B$5$$$J$!!$$H;W$C$FJ|CV$7$F$$$?$N$G$7$?!%$3$N%Q%C%A$G$9$H!$$?$H$($Pe(B
test-all e$B$H$+$O$I$&$G$7$?!)e(B

ruby 1.9.2dev (2009-06-14 trunk 23691) [i386-mingw32] e$B$G$Oe(B
make test-all e$B$Ge(B SEGV e$B$,=P$F$7$^$$$^$7$?!#e(B
TestFiber#test_many_fibers_with_threads e$B$,e(B E
e$B$G=*$o$C$?D>8e$Ne(B
TestFiber#test_normal e$B$GMn$A$F$$$k$h$&$G$9!#e(B
e$B$G$9$,$J$<$+e(B test_fiber.rb
e$B$rD>@\5/F0$9$k$H%(%i!<$J$7$G40Av$7$^$9!#e(B

e$B$^$?%Q%C%A$Oe(BLinuxe$B4D6-$G=q$$$?$N$G$9$,!"$=$N;~$Ne(Bmake
test-alle$B$G$OLdBj$O$J$/!"e(B
e$B:#2~$a$Fe(B ruby 1.9.2dev (2009-06-15 trunk 23692) [i686-linux]
e$B$G;n$7$F$_$F$be(B
SEGV e$B$OH/@8$7$^$;$s$G$7$?!#e(B

e$B$I$&$$$&;v$+$h$/J,$+$j$^$;$s$,!“IT0BDj$G$”$k$3$H$O4V0c$$$J$$$N$Ge(B
e$B$3$N%Q%C%A$OLr$KN)$?$J$5$=$&$G$9!#?=$7Lu$"$j$^$;$s!#e(B

[email protected]$G$9!%e(B

wanabe wrote::

e$B$I$&$$$&;v$+$h$/J,$+$j$^$;$s$,!“IT0BDj$G$”$k$3$H$O4V0c$$$J$$$N$Ge(B
e$B$3$N%Q%C%A$OLr$KN)$?$J$5$=$&$G$9!#?=$7Lu$"$j$^$;$s!#e(B

e$B!!$$$($$$(!%$b$&[email protected]$9$l$P$J$s$H$+$J$k$H;W$&$N$G$9$,!$$3$3$O$I$&e(B
e$B$K$bFq$7$$$G$9$h$M!%e(BFiber
e$B$N=89g$rJ]B8$9$k!$;d$,:G8e$K$"[email protected](B
e$BItJ,$K%P%0$,$"$k$h$&$J5$$,$7$F$$$^$9!%e(B

e$B!!e(B1.9.2 e$B$K$O4V$K9g$o$;$?$$$H$3$m!%e(B

e$B%A%1%C%He(B #595 e$B$,99?7$5$l$^$7$?!#e(B (by _ wanabe)

e$B%U%!%$%ke(B ensure_fiber.patch e$BDI2Ce(B
ruby -v ruby 1.9.2dev (2010-01-13) [i386-mingw32]e$B$K%;%C%He(B

e$B0JA0$3$N%A%1%C%H$K$D$$$F=q$$$?%Q%C%A$,e(B SEGV
e$B$9$k$H=q$-$^$7$?$,!"e(B
e$B%Q%C%A$NLdBj$G$O$J$/e(B Bug #1325 [email protected]$C$?$h$&$G$9!#e(B
e$B2~$a$F3NG’$7$?$H$3$me(B make test-all e$B$G$Oe(B SEGV
e$B$7$^$;$s$G$7$?!#e(B

e$B$=$l$H$OJL$NLdBj$H$7$F!“Nc30$G%8%c%s%W$5$;$k$He(B rescue
e$B$5$l$k62$l$,$”$C$?$N$Ge(B
throw/catch e$B$r;H$&$h$&$KJQ99$7$?%Q%C%A$rE:IU$7$^$9!#e(B
e$B$48!F$$/[email protected]$5$l$P9,$$$G$9!#e(B

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

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

2010e$BG/e(B4e$B7ne(B10e$BF|e(B23:20 _ wanabe [email protected]:

[email protected]$5$se(B

e$B$3$N%A%1%C%H$*$h$S%Q%C%A$K$D$$$F%3%a%s%[email protected]$1$l$P9,$$$G$9!#e(B
e$B$3$NBP=h$GLdBj$J$$$+$I$&$+!"$b$7LdBj$J$$$H$7$?$ie(B
1.9.2 e$B%j%j!<%9A0$KF~$l$k$Y$-$+$I$&$+$,5$$K$J$C$F$$$^$9!#e(B
e$B$=$b$=$b%"%W%m!<%A$,$^$:$$$h$&$G$7$?$iJL$NJ}K!$r9M$($^$9!#e(B

[email protected]$5$s$G$O$J$$$G$9$,>!<j$K%3%a%s%H$7$^$9!#e(B

  1. e$B%W%m%;%9=*N;;~$G$J$/!"e(BFiber e$B$,e(B GC e$B$G2s<}$5$l$k;~$Ke(B
    ensure [email protected]$,e(B
    e$B<B9T$5$l$^$;$s!#e(B

1000.times do
Fiber.new do
begin
Fiber.yield
ensure
puts “foo!”
end
end.resume
GC.start
end

foo! e$B$,e(B 1 e$B8D$7$+=P$J$$e(B (e$B4|BT$Oe(B 1000 e$B8De(B)

$ ./ruby t.rb
foo!

e$B<B9TCf$Ne(B Thread e$B$HF1MM$K!"e(Byield e$BCf$Ne(B Fiber e$B$Oe(B GC
e$B$7$J$$$h$&$K$9$ke(B
e$B$/$i$$$7$+;W$$$D$-$^$;$s!#e(B

  1. Fiber e$B$r:n$C$?%9%l%C%I$,=*N;$7$?>l9g!"e(BFiber e$B$Ne(B ensure
    [email protected]$,<B9Te(B
    e$B$5$l$^$;$s!#e(B

Thread.new do
Fiber.new do
begin
Fiber.yield
ensure
puts “foo!”
end
end.resume
end
sleep 1
puts “end”

foo! e$B$,=P$J$$e(B

$ ./ruby t.rb
end

e$B%W%m%;%9$N=*N;;~$G$J$/!"%9%l%C%I$N=*N;;~$Ke(B Fiber
e$B$r5/$3$5$J$$$He(B
e$B9T$1$J$$!)e(B

e$B$I$A$i$b;EMM%l%Y%k$G8!F$$7$J$$$H$$$1$J$$$3$H$N$h$&$J5$$,$7$^$9!#e(B
e$B$^[email protected]=BgJQ$=$&$J$N$G!"e(B1.9.2
e$B$O8+Aw$C$?J}$,$$$$$s$8$c$J$$$+$J$He(B
e$B;W$$$^$9!#Aa$/%3%a%s%H$7$F$$$l$P!"e(Bwanabe
e$B$5$s$J$iM>M5$GD>$;$?$He(B
e$B;W$&$N$G;DG0$G$9$,!#e(B

e$B%A%1%C%He(B #595 e$B$,99?7$5$l$^$7$?!#e(B (by _ wanabe)

[email protected]$5$se(B

e$B$3$N%A%1%C%H$*$h$S%Q%C%A$K$D$$$F%3%a%s%[email protected]$1$l$P9,$$$G$9!#e(B
e$B$3$NBP=h$GLdBj$J$$$+$I$&$+!"$b$7LdBj$J$$$H$7$?$ie(B
1.9.2 e$B%j%j!<%9A0$KF~$l$k$Y$-$+$I$&$+$,5$$K$J$C$F$$$^$9!#e(B
e$B$=$b$=$b%"%W%m!<%A$,$^$:$$$h$&$G$7$?$iJL$NJ}K!$r9M$($^$9!#e(B

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

(2010/04/21 23:55), Yusuke ENDOH wrote::

e$B$I$A$i$b;EMM%l%Y%k$G8!F$$7$J$$$H$$$1$J$$$3$H$N$h$&$J5$$,$7$^$9!#e(B
e$B$^[email protected]=BgJQ$=$&$J$N$G!"e(B1.9.2 e$B$O8+Aw$C$?J}$,$$$$$s$8$c$J$$$+$J$He(B
e$B;W$$$^$9!#Aa$/%3%a%s%H$7$F$$$l$P!"e(Bwanabe e$B$5$s$J$iM>M5$GD>$;$?$He(B
e$B;W$&$N$G;DG0$G$9$,!#e(B

e$B!!JV;v$,=PMh$F$$$J$/$F$9$_$^$;$s!%:G6a!$?’!9$HM>M5$,$J$/$F!$$8$C$/$j9M$(e(B
e$B$FJV;v$,=PMh$:!%e(B

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs