From: merch-redmine@... Date: 2021-03-19T23:51:56+00:00 Subject: [ruby-core:102933] [Ruby master Bug#13930] Exception is caught in rescue above ensure Issue #13930 has been updated by jeremyevans0 (Jeremy Evans). Assignee set to ko1 (Koichi Sasada) Status changed from Open to Assigned Thanks to @wanabe's insight that this issue is related to the scope of the rescue catch entry, I came up with a pull request that avoids the issues @wanabe identified in his patch: https://github.com/ruby/ruby/pull/4291. Instead of trying to fix things cleanly (which I don't know how to do), just adjust the scope of the rescue catch entry so it doesn't cover the ensure block. This approach fixes the original example and @wanabe's simplified example in this issue, as well as all failing examples in #16618. The heuristic this approach uses may not fix all issues, and it may cause issues, but with my limited knowledge of the compiler I cannot be certain of either. However, the pull request does pass all existing tests. ---------------------------------------- Bug #13930: Exception is caught in rescue above ensure https://bugs.ruby-lang.org/issues/13930#change-90998 * Author: msauter (Michael Sauter) * Status: Assigned * Priority: Normal * Assignee: ko1 (Koichi Sasada) * ruby -v: 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux] * Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN ---------------------------------------- Given the following code: ~~~ ruby def foo i = 0 3.times do |n| begin puts "a" i += 1 next if i > 1 puts "b" rescue => e puts "rescue in foo, caught #{e}" ensure puts "ensure in foo, yield from foo: #{n}" yield n end end end begin x = 0 foo do |o| puts o x += 1 raise "test" if x > 1 puts "done yielding" end rescue => e puts "rescue outside, caught #{e}" ensure puts "ensure outside" end ~~~ The output is: ~~~ a b ensure in foo, yield from foo: 0 0 done yielding a ensure in foo, yield from foo: 1 1 rescue in foo, caught test ensure in foo, yield from foo: 1 1 rescue outside, caught test ensure outside ~~~ So the exception raised within the yielded block is caught in the rescue block above the ensure block which yielded. That sounds wrong to me. Or is it intended? The issue seems to be with the usage of next. Also, exception is caught inside AND outside as it seems that the ensure block ends up being called twice ?! If I change the code to this: ~~~ ruby def foo i = 0 3.times do |n| begin puts "a" i += 1 # next if i > 1 puts "b" rescue => e puts "rescue in foo, caught #{e}" ensure puts "ensure in foo, yield from foo: #{n}" yield n end end end begin x = 0 foo do |o| puts o x += 1 raise "test" if x > 1 puts "done yielding" end rescue => e puts "rescue outside, caught #{e}" ensure puts "ensure outside" end ~~~ Then the output is: ~~~ a b ensure in foo, yield from foo: 0 0 done yielding a b ensure in foo, yield from foo: 1 1 rescue outside, caught test ensure outside ~~~ I would have expected this output also when using next as above. ---Files-------------------------------- bug13930.disasm (3.62 KB) bug13930.patch (492 Bytes) -- https://bugs.ruby-lang.org/ Unsubscribe: