Repository navigation
Run FinalizationRegistry cleanup callbacks #331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
andreiltd
merged 4 commits into
bytecodealliance:main
from
veeshi:finalization-registry-cleanup
Oct 26, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4f73c80
Run FinalizationRegistry cleanup callbacks
veeshi 66537a0
Name the unused cleanup callback parameters for clang-tidy
veeshi d48f2f6
Check for a TLA rejection again after running FinalizationRegistry cl…
veeshi a394fe3
Treat a failed FinalizationRegistry cleanup append as fatal
veeshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| finalized 1 |
16 changes: 16 additions & 0 deletions
16
tests/e2e/finalization-registry-snapshot/finalization-registry-snapshot.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // The collection that runs before the Wizer snapshot is taken finds the target registered below | ||
| // dead. Its cleanup must have run before the snapshot, not be baked into it and run after it is | ||
| // resumed: the handler answers synchronously, before the first event loop of the resumed | ||
| // instance, so it sees the count only if the cleanup ran during pre-initialization. | ||
| let finalized = 0; | ||
| const registry = new FinalizationRegistry(() => { | ||
| finalized += 1; | ||
| }); | ||
|
|
||
| // In a function, so that no top-level frame still refers to the target. | ||
| function register() { | ||
| registry.register({}, 0); | ||
| } | ||
| register(); | ||
|
|
||
| addEventListener("fetch", (event) => event.respondWith(new Response(`finalized ${finalized}`))); |
Empty file.
2 changes: 2 additions & 0 deletions
2
tests/e2e/finalization-registry-tla-err/expect_wizer_stderr.txt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Exception while evaluating top-level script | ||
| e2e/finalization-registry-tla-err/finalization-registry-tla-err.js:6:10 Error: rejected by a FinalizationRegistry cleanup |
15 changes: 15 additions & 0 deletions
15
tests/e2e/finalization-registry-tla-err/finalization-registry-tla-err.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // The collection that runs before the Wizer snapshot is taken finds the target registered below | ||
| // dead, and its cleanup rejects the pending top-level await. Pre-initialization must fail, as it | ||
| // does for any TLA rejection, rather than snapshot a failed module. | ||
| let reject; | ||
| const registry = new FinalizationRegistry(() => { | ||
| reject(new Error('rejected by a FinalizationRegistry cleanup')); | ||
| }); | ||
|
|
||
| // In a function, so that no top-level frame still refers to the target. | ||
| function register() { | ||
| registry.register({}, 0); | ||
| } | ||
| register(); | ||
|
|
||
| await new Promise((_, _reject) => void (reject = _reject)); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| finalized |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // A FinalizationRegistry callback runs once a collection has found its target dead. The | ||
| // collection is started by allocation, and the callback runs in the event loop, after the | ||
| // microtask checkpoint that follows it. | ||
| let finalized = 0; | ||
| const registry = new FinalizationRegistry(() => { | ||
| finalized += 1; | ||
| }); | ||
|
|
||
| async function handle() { | ||
| registry.register({}, 0); | ||
|
|
||
| // Allocate until a collection has run and its cleanup has been called, giving up after a | ||
| // bounded number of attempts. | ||
| for (let i = 0; i < 1000 && finalized === 0; i++) { | ||
| const buffer = new Uint8Array(2 << 20); | ||
| buffer[0] = 1; | ||
| // Wait for a task, so that the cleanups queued by a collection can run. | ||
| await new Promise((resolve) => setTimeout(resolve, 0)); | ||
| } | ||
|
|
||
| return new Response(finalized > 0 ? "finalized" : "not finalized"); | ||
| } | ||
|
|
||
| addEventListener("fetch", (event) => event.respondWith(handle())); |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.