break in Fiber in loop kills mruby #1766

Closed
tsahara opened this Issue Feb 28, 2014 · 3 comments

Projects

None yet

2 participants

@tsahara
tsahara commented Feb 28, 2014
loop do
  Fiber.new { break }.resume
end
% mruby a.rb
zsh: segmentation fault (core dumped)  mruby a.rb
@Fleurer
Fleurer commented Feb 28, 2014

in CRuby, this will trigger a LocalJumpError:

test.rb:2:in `block (2 levels) in <main>': break from proc-closure (LocalJumpError)
@Fleurer
Fleurer commented Feb 28, 2014

this patch can be taken as a work around, but I haven't found out the root cause yet. please do not check in this patch until we get the root cause clear.

diff --git a/src/vm.c b/src/vm.c
index 13aa7c2..80a51ee 100644
--- a/src/vm.c
+++ b/src/vm.c
@@ -1358,7 +1358,12 @@ mrb_context_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int
             localjump_error(mrb, LOCALJUMP_ERROR_BREAK);
             goto L_RAISE;
           }
-          ci = mrb->c->ci = mrb->c->cibase + proc->env->cioff + 1;
+          ci = mrb->c->cibase + proc->env->cioff + 1;
+          if (!ci->stackent) {
+            localjump_error(mrb, LOCALJUMP_ERROR_BREAK);
+            goto L_RAISE;
+          }
+          mrb->c->ci = ci;
           break;
         default:
           /* cannot happen */
@Fleurer
Fleurer commented Feb 28, 2014

there's some inconsistence on the semantic between cruby and mruby of break from a block, like:

proc do
  proc { break }.call
  puts 'a'
end.call
$ ruby bug.rb
bug.rb:2:in `block (2 levels) in <main>': break from proc-closure (LocalJumpError)
    from bug.rb:2:in `call'
    from bug.rb:2:in `block in <main>'
    from bug.rb:4:in `call'
    from bug.rb:4:in `<main>'
$ bin/mruby bug.rb
a

as described in http://readruby.io/closures ,

A LocalJumpError is raised if break is used from a block no longer in scope, e.g. at the top-level of a block created with Proc.new or proc.

@matz matz closed this in 6bd3d88 Feb 28, 2014
@cremno cremno added a commit to cremno/mruby that referenced this issue Mar 4, 2014
@matz matz restore proc after restoring from fiber; ref #1766 e7a28ad
@cremno cremno added a commit to cremno/mruby that referenced this issue Mar 4, 2014
@matz matz support break from fiber block; fix #1766 9bb2eba
@ghost Unknown pushed a commit to iij/mruby that referenced this issue Mar 11, 2014
@matz matz restore proc after restoring from fiber; ref #1766 bb43f18
@ghost Unknown pushed a commit to iij/mruby that referenced this issue Mar 11, 2014
@matz matz support break from fiber block; fix #1766 3adc7f5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment