Skip to content

fix(engine): take catch exception before running finally in try/catch… - #5695

Open
tarushmohindru wants to merge 1 commit into
boa-dev:mainfrom
tarushmohindru:fix/async-finally-return
Open

tarushmohindru wants to merge 1 commit into
boa-dev:mainfrom
tarushmohindru:fix/async-finally-return

Conversation

@tarushmohindru

Copy link
Copy Markdown
Contributor

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:

  • In TryVariant::CatchFinally (core/engine/src/bytecompiler/statement/try.rs), the catch handler now takes the exception thrown from the catch block into a register with MaybeException, instead of leaving it in vm.pending_exception while the finally block runs. After finally, it emits Throw with that register, or ReThrow when there was no pending exception (a generator return(), which must still run finally). This matches how the finally-only variant already handles it.
  • Adds tests in core/engine/src/tests/control_flow/mod.rs for async finally completions overriding a catch throw, catch throws surviving handlers inside finally, and generator return()/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:

  1. Any abrupt completion of finally in an async function hit MaybeException in the async return path and rejected the promise with the stale exception. This also affects break/continue out of finally followed by a later return, which the issue does not mention:
    async function h() {
      for (;;) { try { throw 1; } catch (e) { throw e; } finally { break; } }
      return 3;
    }
    // before: rejected 1, after: resolved 3
  2. Any handler that ran inside finally (a nested try/catch, or await resuming) consumed the pending exception, so the rethrow after finally was lost ((comment on #5648) labelled break out of nested for…of; catch rethrow lost when finally catches; delete (o?.p); case 3 without rest #5671 item 2):
    function g() { try { throw "x"; } catch (e) { throw e; } finally { try { throw "y"; } catch {} } }
    // before: returns undefined, after: throws "x"

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 the try.rs change; the generator test passes before and after.

  • insta-bytecode snapshot tests: pass.

  • cargo fmt --all -- --check and cargo clippy -p boa_engine --all-targets: clean.

  • test262, results unchanged before and after:

    Suite Result
    language/statements/try 198/201
    language/statements/async-function, language/expressions/async-function, language/expressions/async-arrow-function 100%
    language/statements/generators, language/statements/async-generator 100%
    built-ins/GeneratorPrototype, built-ins/AsyncGeneratorPrototype 100%
    language/statements/for-of 740/751 (10 ignored)

    The 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 on main too.

…/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)
@tarushmohindru
tarushmohindru requested a review from a team as a code owner November 27, 2026 11:40
@github-actions github-actions Bot added Waiting On Review Waiting on reviews from the maintainers C-Tests Issues and PRs related to the tests. C-VM Issues and PRs related to the Boa Virtual Machine. and removed Waiting On Review Waiting on reviews from the maintainers labels Nov 27, 2026
@github-actions github-actions Bot added this to the v0.23 milestone Nov 27, 2026
@github-actions

Copy link
Copy Markdown

Test262 conformance changes

Test result main count PR count difference
Total 53,578 53,578 0
Passed 51,457 51,457 0
Ignored 1,629 1,629 0
Failed 492 492 0
Panics 0 0 0
Conformance 96.04% 96.04% 0.00%

Tested main commit: 2d8a534c683d386144d202e88e642c7e09382e01
Tested PR commit: b650813b335e59305e9bacdaccc859e54057a05d
Compare commits: 2d8a534...b650813

@codecov

codecov Bot commented Nov 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 63.02%. Comparing base (6ddc2b4) to head (b650813).
⚠️ Report is 1090 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-Tests Issues and PRs related to the tests. C-VM Issues and PRs related to the Boa Virtual Machine.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant