https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112020-02-10T02:14:13ZRuby Issue Tracking SystemRuby master - Bug #16618: Ensure called twice when raise in ensurehttps://redmine.ruby-lang.org/issues/16618?journal_id=842112020-02-10T02:14:13Zdavidsiaw (David Siaw)
<ul><li><strong>Subject</strong> changed from <i>Rescue called twice when raise in ensure</i> to <i>Ensure called twice when raise in ensure</i></li></ul> Ruby master - Bug #16618: Ensure called twice when raise in ensurehttps://redmine.ruby-lang.org/issues/16618?journal_id=842132020-02-10T02:27:23Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Related to</strong> <i><a class="issue tracker-1 status-5 priority-4 priority-default closed" href="/issues/13930">Bug #13930</a>: Exception is caught in rescue above ensure</i> added</li></ul> Ruby master - Bug #16618: Ensure called twice when raise in ensurehttps://redmine.ruby-lang.org/issues/16618?journal_id=842872020-02-17T09:04:12Zdavidsiaw (David Siaw)
<ul></ul><p>I found another issue with this where</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">meow</span>
<span class="nb">puts</span> <span class="s1">'start'</span>
<span class="k">begin</span>
<span class="k">return</span>
<span class="nb">puts</span> <span class="s1">'should not run'</span>
<span class="k">rescue</span>
<span class="nb">puts</span> <span class="s1">'inner rescue'</span>
<span class="k">ensure</span>
<span class="nb">puts</span> <span class="s1">'inner ensure'</span>
<span class="k">end</span>
<span class="k">rescue</span>
<span class="nb">puts</span> <span class="s1">'rescue'</span>
<span class="k">ensure</span>
<span class="nb">puts</span> <span class="s1">'ensure'</span>
<span class="k">raise</span> <span class="s1">'heh'</span>
<span class="k">end</span>
<span class="n">meow</span>
</code></pre>
<p>gives the output</p>
<pre><code>start
inner ensure
ensure
inner rescue
inner ensure
ensure
Traceback (most recent call last):
1: from meow.rb:19:in `<main>'
meow.rb:16:in `meow': heh (RuntimeError)
</code></pre>
<p>It turns out that the raise gets rescued by the inner rescue, not the rescue above it even though that code should have ended already.</p> Ruby master - Bug #16618: Ensure called twice when raise in ensurehttps://redmine.ruby-lang.org/issues/16618?journal_id=916492021-04-22T02:33:55Zko1 (Koichi Sasada)
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Applied in changeset <a class="changeset" title="fix raise in exception with jump add_ensure_iseq() adds ensure block to the end of jump such as ..." href="https://redmine.ruby-lang.org/projects/ruby-master/repository/git/revisions/609de71f043e8ba34f22b9993e444e2e5bb05709">git|609de71f043e8ba34f22b9993e444e2e5bb05709</a>.</p>
<hr>
<p>fix raise in exception with jump</p>
<p>add_ensure_iseq() adds ensure block to the end of<br>
jump such as next/redo/return. However, if the rescue<br>
cause are in the body, this rescue catches the exception<br>
in ensure clause.</p>
<p>iter do<br>
next<br>
rescue<br>
R<br>
ensure<br>
raise<br>
end</p>
<p>In this case, R should not be executed, but executed without this patch.</p>
<p>Fixes [Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Exception is caught in rescue above ensure (Closed)" href="https://redmine.ruby-lang.org/issues/13930">#13930</a>]<br>
Fixes [Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Ensure called twice when raise in ensure (Closed)" href="https://redmine.ruby-lang.org/issues/16618">#16618</a>]</p>
<p>A part of tests are written by @jeremyevans <a href="https://github.com/ruby/ruby/pull/4291" class="external">https://github.com/ruby/ruby/pull/4291</a></p>