Bug #2729

`unexpected break' occurs when a proc is called in ensure

Added by Nobuyoshi Nakada about 4 years ago. Updated over 1 year ago.

[ruby-core:28132]
Status:Closed
Priority:Normal
Assignee:Koichi Sasada
Category:core
Target version:1.9.3
ruby -v:- Backport:

Description

=begin
Reproducing code:

class Bug
include Enumerable
def each
begin
yield :foo
ensure
proc {}.call
end
end
end
e = Bug.new
p e.detect{true} # => :foo
p e.any?{true} # => true
p e.all?{false} # => false
p e.include?(:foo) # => true

All work expectedly if no Proc#call, but all occurs `unexpected break' with it.
=end

proc_in_ensure.patch Magnifier (449 Bytes) _ wanabe, 02/21/2010 07:21 PM

rewrite-dfp-in-errinfo.txt Magnifier (858 Bytes) Kazuki Tsujimoto, 07/31/2011 10:58 AM

Associated revisions

Revision 33064
Added by Koichi Sasada over 2 years ago

  • vm.c (vmmakeenv_each): work around to solve Bug #2729. fixes: Bug #2729 a patch from Kazuki Tsujimoto kazuki@callcc.net This problem is caused by changing dfp (dynamic env pointer) from saved dfp. Saved dfp is pointed env in VM stack. However, the dfp can be moved because VM copies env from VM stack to the heap. At this copying, dfp was also changed. To solve this problem, I'll try to change throw mechanism (not save target dfp, but save target cfp).
  • bootstraptest/test_flow.rb: add a test for above.

History

#1 Updated by Nobuyoshi Nakada about 4 years ago

=begin
Hi,

At Wed, 10 Feb 2010 04:36:18 +0900,
Nobuyoshi Nakada wrote in :

Bug #2729: `unexpected break' occurs when a proc is called in ensure

Test case.

Index: bootstraptest/testflow.rb
===================================================================
--- bootstraptest/test
flow.rb (revision 26629)
+++ bootstraptest/testflow.rb (working copy)
@@ -518,4 +518,17 @@ assert
equal %Q{ENSURE\n}, %q{
end
e = Bug2728.new
+}],
+ ['', %q{
+ class Bug2729
+ include Enumerable
+ def each
+ begin
+ yield :foo
+ ensure
+ proc {}.call
+ end
+ end
+ end
+ e = Bug2729.new
}]].each do |bug, src|
assert_equal "foo", src + %q{e.detect {true}}, bug

--
Nobu Nakada

=end

#2 Updated by _ wanabe about 4 years ago

=begin
This patch prevent the exception.
I know it's not a fundamental solution, but I hope it will help for finding a bug.
=end

#3 Updated by Yui NARUSE almost 3 years ago

  • Status changed from Open to Assigned
  • Assignee set to Koichi Sasada

#4 Updated by Yui NARUSE almost 3 years ago

  • Target version changed from 2.0.0 to 1.9.3

#5 Updated by Kazuki Tsujimoto over 2 years ago

A patch fixing the problem is attached, but it might be only a workaroud.

#6 Updated by Koichi Sasada over 2 years ago

  • ruby -v changed from ruby 1.9.2dev (2010-02-10 trunk 26629) [x86_64-darwin9.0] to -

(2011/07/30 18:58), Kazuki Tsujimoto wrote:

A patch fixing the problem is attached, but it might be only a workaroud.

Thank you. I'll apply this patch for 1.9.3 and trunk.

To solve it, I'll change the vm_throw mechanism on trunk (not throw dfp,
but throw cfp).

# BTW, vm_throw() is toooo complicated.

Regards,
Koichi

--
// SASADA Koichi at atdot dot net

#7 Updated by Koichi Sasada over 2 years ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r33064.
Nobuyoshi, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • vm.c (vmmakeenv_each): work around to solve Bug #2729. fixes: Bug #2729 a patch from Kazuki Tsujimoto kazuki@callcc.net This problem is caused by changing dfp (dynamic env pointer) from saved dfp. Saved dfp is pointed env in VM stack. However, the dfp can be moved because VM copies env from VM stack to the heap. At this copying, dfp was also changed. To solve this problem, I'll try to change throw mechanism (not save target dfp, but save target cfp).
  • bootstraptest/test_flow.rb: add a test for above.

#8 Updated by Lars Gierth over 1 year ago

=begin
This patch doesn't seem to have landed in 1.9.2 and 1.9.3. Can someone backport it?

$ git checkout trunk
$ git log --oneline | grep 33064
a09e713 * proc.c (procnew): force to rewrite errinfo when calling Proc.new in ensure. [Bug #5234] This code will be removed after changing throw mechanism (see r33064).
$ git checkout ruby
192
$ git log --oneline | grep 33064
$ git checkout ruby19_3
$ git log --oneline | grep 33064
=end

#9 Updated by Lars Gierth over 1 year ago

#5234 has been backported to 1.9.3 at least.

Also available in: Atom PDF