Skip to content

A [Symbol.dispose]() that throws while an exception unwinds no longer terminates - #749

Merged
ASDAlexander77 merged 1 commit into
mainfrom
using-dispose-throws
Oct 30, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
using-dispose-throws

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

A [Symbol.dispose]() that throws while another exception is unwinding through its using terminated the process: every memory model, JIT and AOT, Windows and Linux.

try {
    using d = new Thrower();   // [Symbol.dispose]() throws
    throw "first";
} catch (e: string) { ... }   // 0x80000003 on Windows, "terminate called" on Linux

What changed (TryOpLowering)

  • Windows - the cleanup ran in a cleanup funclet, and the MSVC personality terminates on an exception out of one. A cleanup that may throw is now entered through a catch-all pad, as A finally that throws during unwinding replaces the exception; fewer-parameter functions as wider function types #733 did for finally (windowsCleanupCatches).
  • Linux - the cleanup shared its landing pad with the try's catch and ran before it, with nothing caught. Its dispose call had no unwind edge, so the exception left the function. With a catch or finally of its own, such a cleanup now gets its own catch-all that rethrows to them (linuxCleanupCatches).
  • No leak, rest disposed - the cleanup's calls unwind to a copy of the cleanup (cleanupRetries). Each disposal's flag is cleared before its call, so the copy skips what was attempted, disposes the remaining resources and runs the releases. Without this the ownership verifier reported every using as leaking on that path. The copy's pad is nested in the cleanup's, so the cleanup's terminator moves after it: Win32ExceptionPass reads nesting from code order.

Semantics

  • The disposal's exception replaces the one it interrupted, as a throw in finally does. tslang has no SuppressedError (TC39 wraps both); adding one is a separate decision.
  • If a second disposal throws, it comes from the copy as a nested exception and the rest of the copy is skipped. On Windows this try's catch takes it; on Linux the caller does. The test checks only what both back ends do.

Tests

  • New 00using_dispose_throws.ts covers: the try's own catch; finally only; a nested block; the caller catching; a catch of another type; a loop; two usings on both paths; both disposals throwing; a disposal that does not throw. It is registered for compile and JIT, plus the rc/none corpus.
  • Also checked by hand under gc/rc/own/none, at -O0 and -O3, JIT and AOT, on Windows and on Linux (WSL).
  • Full suites: Windows 6426/6426, Linux (WSL) 6399/6399, ownership verifier included.
  • The 00nested_catch.ts header no longer claims that reading a catch value is broken (item 5am, already fixed).

🤖 Generated with Claude Code

…er terminates

A disposal that threw on the way out of an exception - `try { using d = ...; throw "x" } catch`
with a throwing dispose - terminated the process in every model, in both tiers, on both back ends.

- Windows: the cleanup region ran in a cleanup funclet, and an exception out of one is thrown
  during unwinding to the MSVC personality (0x80000003). A cleanup that may throw is now entered
  through a catch-all pad, as #733 did for `finally` (windowsCleanupCatches).
- Linux: the cleanup shared its landing pad with the try's catch, ran before it with nothing
  caught, and its dispose call had no unwind edge, so the exception left the function. With a
  catch or finally of its own, such a cleanup is a catch-all of its own that rethrows to them
  (linuxCleanupCatches).
- The cleanup's calls unwind to a copy of the cleanup (cleanupRetries), which re-runs it - each
  disposal's flag is cleared before its call - so the resources still to dispose are disposed and
  the releases after the throwing call run. Without it the ownership verifier reported every
  `using` as leaking on that path. The copy's pad is nested in the cleanup's, so the cleanup's
  terminator moves after it, where Win32ExceptionPass reads the nesting from.

The disposal's exception replaces the one it interrupted, as a throw in a `finally` does; there
is no SuppressedError. A second disposal that throws is a nested exception from the copy: this
try's catch takes it on Windows, the caller on Linux.

Test: 00using_dispose_throws.ts (compile, JIT, and the rc/none corpus). Also corrects the
00nested_catch.ts header, which still said reading a catch value was broken (item 5am, fixed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit f269356 into main Oct 30, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the using-dispose-throws branch October 30, 2026 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant