Repository navigation
fix(engine): take catch exception before running finally in try/catch… - #5695
Open
tarushmohindru wants to merge 1 commit into
Open
tarushmohindru wants to merge 1 commit into
tarushmohindru wants to merge 1 commit into
Conversation
…/finally When the catch block of a `try`/`catch`/`finally` threw, the exception was left in `vm.pending_exception` while the finally block ran, and `ReThrow` was emitted afterwards to rethrow it. This leaked into the finally block: - An abrupt completion in finally (`return`, or `break`/`continue` followed by a later `return`) in an async function went through the async return path, whose `MaybeException` picked up the stale exception and rejected the promise instead of resolving it. - Any handler that ran inside finally (a nested `try`/`catch`, or another catch running while finally was suspended at `await`) consumed the pending exception, so the rethrow after finally was lost. The catch handler now uses `MaybeException` to move the exception into a register. After finally, it emits `Throw` with that register, or `ReThrow` when there was no pending exception (a generator `return()`), matching how the finally-only variant already works. Fixes boa-dev#5648 (case 4) Fixes boa-dev#5671 (case 2)
Test262 conformance changes
Tested main commit: |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5695 +/- ##
===========================================
+ Coverage 47.24% 63.02% +15.77%
===========================================
Files 476 538 +62
Lines 46892 58590 +11698
===========================================
+ Hits 22154 36925 +14771
+ Misses 24738 21665 -3073 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This Pull Request fixes case 4 of #5648 and item 2 of #5671 (the other cases in both issues are not addressed here).
It changes the following:
TryVariant::CatchFinally(core/engine/src/bytecompiler/statement/try.rs), the catch handler now takes the exception thrown from the catch block into a register withMaybeException, instead of leaving it invm.pending_exceptionwhile the finally block runs. After finally, it emitsThrowwith that register, orReThrowwhen there was no pending exception (a generatorreturn(), which must still run finally). This matches how the finally-only variant already handles it.core/engine/src/tests/control_flow/mod.rsfor asyncfinallycompletions overriding a catch throw, catch throws surviving handlers inside finally, and generatorreturn()/throw()through catch/finally.Root cause
The issue points at the async return path in
jump_control.rs, but that is where the symptom shows up, not the cause. Because the catch exception stayed pending during finally:MaybeExceptionin the async return path and rejected the promise with the stale exception. This also affectsbreak/continueout of finally followed by a laterreturn, which the issue does not mention:try/catch, orawaitresuming) consumed the pending exception, so the rethrow after finally was lost ((comment on #5648) labelledbreakout of nestedfor…of;catchrethrow lost whenfinallycatches;delete (o?.p); case 3 without rest #5671 item 2):Per TryStatement evaluation, an abrupt completion from Finally replaces the Catch completion; otherwise the Catch completion is kept.
Testing
cargo test -p boa_engine: all pass. The two new bug tests fail without thetry.rschange; the generator test passes before and after.insta-bytecodesnapshot tests: pass.cargo fmt --all -- --checkandcargo clippy -p boa_engine --all-targets: clean.test262, results unchanged before and after:
language/statements/trylanguage/statements/async-function,language/expressions/async-function,language/expressions/async-arrow-functionlanguage/statements/generators,language/statements/async-generatorbuilt-ins/GeneratorPrototype,built-ins/AsyncGeneratorPrototypelanguage/statements/for-ofThe remaining failures (
try/tco-catch.js,try/tco-finally.js,try/tco-catch-finally.js,for-of/dstr/obj-rest-before-comma-invalid.js) fail onmaintoo.