Skip to content

fix(utils): prevent FinalizationRegistry leak on every re-render in useId#9896

Closed
Gdhanush-13 wants to merge 1 commit intoadobe:mainfrom
Gdhanush-13:fix/useId-finalization-registry-leak
Closed

fix(utils): prevent FinalizationRegistry leak on every re-render in useId#9896
Gdhanush-13 wants to merge 1 commit intoadobe:mainfrom
Gdhanush-13:fix/useId-finalization-registry-leak

Conversation

@Gdhanush-13
Copy link
Copy Markdown

Problem

registry.register(cleanupRef, res) in useId runs unconditionally in the render body, adding a new FinalizationRegistry entry on every re-render. Since FinalizationRegistry.register() always adds a new entry (even with the same target), this leaks entries that are never cleaned up until the component unmounts.

Any hook that calls useId internally (e.g. useMenuTrigger) inherits this leak.

Fix

Track the last registered id in a ref. Only call register() when res changes, and unregister() the previous entry before re-registering. Also reset the ref on cleanup so remounted components re-register correctly.

Fixes #9852

registry.register() was called unconditionally in the render body,
adding a new entry on every render. Since FinalizationRegistry.register()
always adds a new entry (even with the same target), this leaked entries
until the component unmounted.

Fix: track the last registered id in a ref and only call register()
when the id changes. Unregister the previous entry before re-registering.

Fixes adobe#9852
@nwidynski
Copy link
Copy Markdown
Contributor

Duplicate of #9853

@Gdhanush-13 Gdhanush-13 changed the title fix(useId): prevent FinalizationRegistry leak on every re-render fix(utils): prevent FinalizationRegistry leak on every re-render in useId Apr 9, 2026
@Gdhanush-13
Copy link
Copy Markdown
Author

Closing - duplicate of #9853 which addresses the same fix for #9852.

@Gdhanush-13 Gdhanush-13 closed this Apr 9, 2026
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.

useId leaks FinalizationRegistry entries on every re-render

2 participants