Skip to content

test: keep FinalizationRegistry cleanups out of bun test --isolate on Bun 1.4 - #1012

Merged
7nohe merged 1 commit into
mainfrom
fix/isolate-finalization-registry-guard
Sep 23, 2026
Merged

7nohe merged 1 commit into
mainfrom
fix/isolate-finalization-registry-guard

Conversation

@7nohe

@7nohe 7nohe commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The sqlite-smoke job (Bun 1.4.2) died twice on main in one day with Segmentation fault at address 0x70 and test:bun exiting 139, and the build-and-test (1.4.2) lane earlier ended with 0 fail, 1 error (Unhandled error between tests, a readonly-property TypeError inside aws-cdk-lib). Bun 1.3.14 never hit either.

Both are one mechanism. JSC queues a FinalizationRegistry cleanup as a DeferredWorkTimer ticket and Bun 1.4.0-1.4.2 run it with no realm check (Bun::runPendingWork), so it lands after the test file that created the registry is retired under --isolate:

Any registry does it. One crash came right after the plugin-lambda CDK test, where aws-cdk-lib's bundled @aws/cloudformation-validate (wasm-bindgen) creates four registries per synth; the other came while loading a CLI test file, where the registry is one of Bun's own builtins (fs.promises, util, diagnostics_channel, v8, worker_threads each hold one).

Fix

A third test:bun preload, scripts/test-finalization-registry-guard.ts, replaces the global FinalizationRegistry with one that registers nothing on Bun 1.4.x, so no cleanup is ever queued. --preload re-runs in each isolated file's fresh global before its imports, so aws-cdk-lib's wasm bindings and Bun's builtins both capture the inert one (verified through the runner with a probe test). A registered cleanup only ever released memory the test process gives back at exit. Bun 1.3.14 is left untouched; the header says when to delete the file.

--isolate itself stays: it is what keeps a mock.module() swap in one file from leaking into the next (#114).

Verification

  • bun test scripts/test-finalization-registry-guard.test.ts on Bun 1.3.14 and 1.4.2
  • full bun run test:bun on Bun 1.4.2 with the guard: 9973 pass, 157 skip, 0 fail, no segfault, no unhandled error
  • oxlint on the three touched files

Fixes #969

JSC queues a FinalizationRegistry cleanup as a DeferredWorkTimer ticket,
and Bun 1.4.0-1.4.2 run it with no realm check, so it lands after the
test file that created the registry is retired. In a retired realm the
cleanup surfaces as "Unhandled error between tests" (exit 1 with 0
fail); once the retired global has been collected it reads a freed
registry and the process dies with a segfault at address 0x70 (exit
139). The sqlite-smoke job hit the crash twice on main in one day, once
right after the plugin-lambda CDK test (aws-cdk-lib's wasm-bindgen
registries) and once while loading a CLI test file, where the registry
is one of Bun's own builtins.

The first face is fixed upstream in oven-sh/bun#42110 (unreleased); the
second is oven-sh/bun#39994 with oven-sh/WebKit#487, still open. Until a
release carries both, a test:bun preload replaces the global
FinalizationRegistry with one that registers nothing on Bun 1.4.x, so no
cleanup is ever queued. It runs first, before the other preloads import
node:fs. A registered cleanup only ever released memory the test process
gives back at exit. Bun 1.3.14 is left untouched.

Fixes: #969
@7nohe
7nohe force-pushed the fix/isolate-finalization-registry-guard branch from c165ff1 to e13735c Compare September 23, 2026 09:10
@7nohe
7nohe merged commit ce34f62 into main Sep 23, 2026
9 checks passed
@7nohe
7nohe deleted the fix/isolate-finalization-registry-guard branch September 23, 2026 09:17
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.

Flaky unhandled aws-cdk-lib error fails test:bun on Bun 1.4.2 CI

1 participant