Repository navigation
test: keep FinalizationRegistry cleanups out of bun test --isolate on Bun 1.4 - #1012
Merged
Merged
Conversation
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
force-pushed
the
fix/isolate-finalization-registry-guard
branch
from
September 23, 2026 09:10
c165ff1 to
e13735c
Compare
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.
Problem
The
sqlite-smokejob (Bun 1.4.2) died twice onmainin one day withSegmentation fault at address 0x70andtest:bunexiting 139, and thebuild-and-test (1.4.2)lane earlier ended with0 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
FinalizationRegistrycleanup as aDeferredWorkTimerticket 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:Unhandled error between tests(fixed upstream in bun test --isolate: drop a retired file's deferred work oven-sh/bun#42110, not in any release yet);takeDeadHoldingsValueon a vector whose buffer is null, the segfault at0x70(bun.report stack from the crash; Let JSC's DeferredWorkTimer own the deferred-work tickets so a collection cancels a dead realm's work (WebKit bump) oven-sh/bun#39994 with DeferredWorkTimer: keep the embedder's tickets in m_pendingTickets so the end of a collection cancels them oven-sh/WebKit#487, still open).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_threadseach hold one).Fix
A third
test:bunpreload,scripts/test-finalization-registry-guard.ts, replaces the globalFinalizationRegistrywith one that registers nothing on Bun 1.4.x, so no cleanup is ever queued.--preloadre-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.--isolateitself stays: it is what keeps amock.module()swap in one file from leaking into the next (#114).Verification
bun test scripts/test-finalization-registry-guard.test.tson Bun 1.3.14 and 1.4.2bun run test:bunon Bun 1.4.2 with the guard: 9973 pass, 157 skip, 0 fail, no segfault, no unhandled errorFixes #969