Skip to content

Implement FinalizationRegistry cleanup callbacks - #35868

Draft
sebsebmc wants to merge 5 commits into
servo:mainfrom
sebsebmc:finalization-registry-cleanup
Draft

sebsebmc wants to merge 5 commits into
servo:mainfrom
sebsebmc:finalization-registry-cleanup

Conversation

@sebsebmc

@sebsebmc sebsebmc commented Mar 8, 2025 •

Copy link
Copy Markdown
Contributor

FinalizationRegistry is exposed by default in SpiderMonkey now that Servo is using ESR 128 but Servo still needs to register to receive and queue FinalizationRegistry cleanup callbacks.

SpiderMonkey does not support FinalizationRegistry.cleanupSome so most of the js/builtins/weakrefs WPT tests will continue to fail.

I'm not 100% sure that I am using LiveDOMReferences correctly for this use but it was what @jdm suggested on Zulip.
I am not sure if:

  1. I will need to key the callbacks on the global scope
  2. that I am tracing the values correctly
  3. if I am going to need to filter out callbacks when a global scope is killed (if thats even possible without LiveDOMReferences getting cleaned up).

@sebsebmc
sebsebmc force-pushed the finalization-registry-cleanup branch 3 times, most recently from 1aeefcd to 13eac7b Compare March 11, 2025 20:44
@sebsebmc sebsebmc changed the title Proof of concept for FinalizationRegistry cleanup callbacks Implement FinalizationRegistry cleanup callbacks Mar 11, 2025
@sebsebmc

Copy link
Copy Markdown
Contributor Author

My main concern is around needing to prune/drop registered callbacks when the matching global gets killed (or the page gets refreshed etc).

@sebsebmc
sebsebmc marked this pull request as ready for review March 11, 2025 22:12
@sebsebmc
sebsebmc requested a review from gterzian as a code owner March 11, 2025 22:12
@sebsebmc
sebsebmc marked this pull request as draft March 13, 2025 22:24
@sebsebmc
sebsebmc force-pushed the finalization-registry-cleanup branch 2 times, most recently from 9fa1eb6 to 9cbfde5 Compare March 18, 2025 16:45
@sebsebmc

Copy link
Copy Markdown
Contributor Author

This can now pass the WPT test /js/builtins/weakrefs/finalizationregistry-cleanupCallback-gets-a-microtask.optional.any.html without needing a page reload if we make 2 calls to await maybeGarbageCollectAsync(); and use the dom_servo_helpers_enabled pref.

  1. Should I submit the modified WPT test to the repo or is there some process for having a modified test for browser-specific behaviors?
  2. I am still concerned about the lifecycle for the callbacks that I currently store in LiveDOMReferences. Under what conditions are script Runtime and GlobalScope reused? Reloading a page was resulting in cleanup callbacks from the previous load to be called (surprisingly with no crashes or errors?) If they are reused I think it would be more correct to key the callbacks in LiveDOMReferences to something that is specific to a page load and then prune and fetch based on the key of the current page.

@sebsebmc
sebsebmc force-pushed the finalization-registry-cleanup branch from 9cbfde5 to 7b3da98 Compare March 18, 2025 17:36
@sebsebmc sebsebmc mentioned this pull request Apr 2, 2025
sebsebmc added 5 commits April 5, 2025 14:12
Signed-off-by: Sebastian C <[email protected]>
Signed-off-by: Sebastian C <[email protected]>
Signed-off-by: Sebastian C <[email protected]>
Signed-off-by: Sebastian C <[email protected]>
@sebsebmc
sebsebmc force-pushed the finalization-registry-cleanup branch from 7b3da98 to c660159 Compare April 5, 2025 19:29
@sebsebmc

sebsebmc commented Apr 5, 2025

Copy link
Copy Markdown
Contributor Author

With TestUtils implemented the /js/builtins/weakrefs/finalizationregistry-cleanupCallback-gets-a-microtask.optional.any.html test now passes (including the worker test).

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.

Support FinalizationRegistry

1 participant