Skip to content

fix(quickjs): release runtime/context on every failure path - #44

Open
takomitkm wants to merge 2 commits into
SaintWe:multi-tenantfrom
takomitkm:fix/quickjs-cleanup
Open

takomitkm wants to merge 2 commits into
SaintWe:multi-tenantfrom
takomitkm:fix/quickjs-cleanup

Conversation

@takomitkm

Copy link
Copy Markdown

Summary

runScriptOnce disposed the VM and WASM runtime only on the success path.
Compile errors, synchronous throws, rejected promises, pending timeouts and
pendingJobs failures all threw without cleanup, leaking WASM runtimes on
repeated failures.

All handles are now tracked and disposed in a finally block. newContext()
and runtime configuration are inside that block too, so failures there cannot
leak the runtime. The three QuickJSDeferredPromise instances are also
tracked: when a host promise never settles, leaving the deferred alive makes
vm.dispose() abort with a QuickJS GC assertion. Once aborted, pending host
callbacks no longer write into a VM that is about to be disposed.

Tests

  • Compile error, synchronous throw, rejected promise, success, and
    failure-then-success cases (unchanged from the original PR).
  • New: a pending-timeout case (5 runs, 200 ms each) with a leak probe.
  • New: a host callback that settles only after the timeout, verifying the
    aborted guard and that no GC objects are left behind.

The Workers CI job now runs these Node tests.

Part of the split from the original combined PR. Uses the same test hooks
loader as the UserDO PR; no business-code dependency on it.

runScriptOnce only disposed the VM and WASM runtime on the success path.
Compile errors, synchronous throws, rejected promises, pending timeouts
and pendingJobs failures all threw without cleanup, leaking WASM runtimes
on repeated failures.

Track every handle and dispose all of them plus vm/runtime in a finally
block that runs on success, error and timeout alike. Once aborted, pending
host-call callbacks no longer write into a VM that is about to be disposed.

Add regression tests with a leak probe: after 25 script runs, assert every
created runtime has alive === false (23/25 leaked before the fix, 0 after).
Move newContext() and runtime configuration inside try/finally so failures
there cannot leak the WASM runtime. Track all three QuickJSDeferredPromise
instances and dispose them on abort: an unsettled host promise otherwise
leaves GC objects behind and vm.dispose() aborts with an assertion.

Add regression tests for the pending-timeout path and for a host callback
that settles only after the runtime was disposed (via  guard). Include
the shared test hooks loader and wire the suite into CI.
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