Skip to content
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

feat: Guaranteed finalizers #1076

Merged
merged 2 commits into from
Oct 6, 2022

Conversation

andreubotella
Copy link
Contributor

Currently, when a finalizer callback is registered, it is not guaranteed to be called if there is a global reference to the corresponding object that survives the isolate. This is because the finalizer callback takes a &mut Isolate, and so it must be called before the isolate is fully destroyed, but all existing globals (including possibly the one being currently finalized) are still usable while there still exists a mutable reference to the isolate.

However, there are still use cases for having finalizers that are guaranteed to run regardless of any remaining globals, but that don't require any interaction with the isolate. This change adds them.

This change also changes the context annex to use a guaranteed finalizer, fixing a bug with context slots not being freed if there were any globals to the context at the time the isolate is dropped.

Closes #1066.

@andreubotella
Copy link
Contributor Author

This is a way to fix #1066, which was blocking denoland/deno#15760, but it has not been discussed with the core team and might not be a good idea. I'm raising this PR as a way to initiate the discussion.

Currently, when a finalizer callback is registered, it is not
guaranteed to be called if there is a global reference to the
corresponding object that survives the isolate. This is because the
finalizer callback takes a `&mut Isolate`, and so it must be called
before the isolate is fully destroyed, but all existing globals
(including possibly the one being currently finalized) are still
usable while there still exists a mutable reference to the isolate.

However, there are still use cases for having finalizers that are
guaranteed to run regardless of any remaining globals, but that don't
require any interaction with the isolate. This change adds them.

This change also changes the context annex to use a guaranteed
finalizer, fixing a bug with context slots not being freed if there
were any globals to the context at the time the isolate is dropped.

Closes denoland#1066.
@andreubotella
Copy link
Contributor Author

I added tests for the guaranteed finalization API. PTAL

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for adding the tests @andreubotella!

@bartlomieju bartlomieju changed the title [WIP] feat: Guaranteed finalizers feat: Guaranteed finalizers Oct 6, 2022
@bartlomieju bartlomieju merged commit cfdb8b0 into denoland:main Oct 6, 2022
@andreubotella andreubotella deleted the guaranteed-finalizer branch October 6, 2022 16:36
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.

Context slots are never dropped if there are Globals to the context when the isolate is dropped
2 participants