-
Notifications
You must be signed in to change notification settings - Fork 10k
Add FinalizationRegistry docs (Revert #22239) #22295
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
nikitacano
merged 4 commits into
cloudflare:production
from
ketanhwr:ketan/add-js-weak-ref
May 8, 2025
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
13 changes: 13 additions & 0 deletions
13
src/content/changelog/workers/2025-05-08-finalization-registry.mdx
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,13 @@ | ||
| --- | ||
| title: Improved Memory Efficiency for WebAssembly Workers | ||
| description: With the introduction of FinalizationRegistry in Workers Runtime, toolchains like Emscripten and wasm-bindgen can automatically reclaim unused WebAssembly heap and reduce memory leaks. | ||
| products: | ||
| - workers | ||
| date: 2025-05-08T00:00:00Z | ||
| --- | ||
|
|
||
| [FinalizationRegistry](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry) is now available in Workers. You can opt-in using the [`enable_weak_ref`](/workers/configuration/compatibility-flags/#enable-finalizationregistry-and-weakref) compatibility flag. | ||
|
|
||
| This can reduce memory leaks when using WebAssembly-based Workers, which includes [Python Workers](/workers/languages/python/) and [Rust Workers](/workers/languages/rust/). The FinalizationRegistry works by enabling toolchains such as [Emscripten](https://emscripten.org/) and [wasm-bindgen](https://rustwasm.github.io/wasm-bindgen/) to automatically free WebAssembly heap allocations. If you are using WASM and seeing Exceeded Memory errors and cannot determine a cause using [memory profiling](/workers/observability/dev-tools/memory-usage/), you may want to enable the FinalizationRegistry. | ||
|
|
||
| For more information see the [`enable_weak_ref`](/workers/configuration/compatibility-flags/#enable-finalizationregistry-and-weakref) compatibility flag documentation. | ||
ketanhwr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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 @@ | ||
| --- | ||
| _build: | ||
| publishResources: false | ||
| render: never | ||
| list: never | ||
|
|
||
| name: "Enable `FinalizationRegistry` and `WeakRef`" | ||
| sort_date: "2025-05-05" | ||
| enable_date: "2025-05-05" | ||
| enable_flag: "enable_weak_ref" | ||
| disable_flag: "disable_weak_ref" | ||
| --- | ||
|
|
||
| Enables the use of [`FinalizationRegistry`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry) and [`WeakRef`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef) built-ins. | ||
| * `FinalizationRegistry` allows you to register a cleanup callback that runs after an object has been garbage-collected. | ||
ketanhwr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * `WeakRef` creates a weak reference to an object, allowing it to be garbage-collected if no other strong references exist. | ||
ketanhwr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| :::note[Behaviour] | ||
| `FinalizationRegistry` cleanup callbacks may execute at any point during your request lifecycle, even after your invoked handler has completed (similar to `ctx.waitUntil()`). These callbacks do not have an associated async context — you cannot perform any I/O within them, including emitting events to a tail Worker. | ||
ketanhwr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ::: | ||
|
|
||
| :::caution | ||
| These APIs are fundamentally non-deterministic — the timing and execution of garbage collection are unpredictable, and you **should not rely on them for essential program logic**. Additionally, cleanup callbacks registered with `FinalizationRegistry` may **never be executed**, including but not limited to cases where garbage collection is not triggered, or your Worker gets evicted. | ||
ketanhwr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ::: | ||
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.