Skip to content

NativeFinalizer and Finalizer: Should calling detach throw an error instead of be a no-op if nothing is detached? #56632

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

Closed
dcharkes opened this issue Sep 2, 2024 · 3 comments
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug

Comments

@dcharkes
Copy link
Contributor

dcharkes commented Sep 2, 2024

Trying to detach from a non-shared static nativeFinalizer would lead to a no-op

Wouldn't it throw a nice exception: "You cannot detach object foo from this finalizer as it was not attached" ?

Originally posted by @mkustermann in #56227 (comment)

Changing it now, would be a breaking change.

Also, it would mean that code racing with the GC to detach/run the finalizer if you only hold on to the detach key would start throwing if the finalizer is run first. Arguably, such code is likely broken.

I don't remember if we discussed this, and if we made it a no-op for a specific reason. (I don't see any discussion on https://dart-review.googlesource.com/c/sdk/+/238086.) Maybe @lrhn remembers.

cc @rakudrama @sigmundch for Finalizer on JS backend.

@dart-github-bot
Copy link
Collaborator

Summary: The issue proposes that calling detach on a NativeFinalizer or Finalizer when nothing is attached should throw an error instead of being a no-op. This change would provide more informative feedback to developers, but it could also introduce breaking changes and potential race conditions.

@dart-github-bot dart-github-bot added area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug labels Sep 2, 2024
@mkustermann
Copy link
Member

Also, it would mean that code racing with the GC to detach/run the finalizer if you only hold on to the detach key would start throwing if the finalizer is run first. Arguably, such code is likely broken.

Ah, the finalizer detach API doesn't require the object that has a finalizer attached, so that makes this probably not possible.

@lrhn
Copy link
Member

lrhn commented Sep 2, 2024

Agree. The detach-token object is not the trigger-object, so the entry can go away while you're still holding the detach token. You can't know ahead of time whether there is an entry with that detach token or not (at least not unless you do something to count in the callback).

We don't want the finalizer to retain any state after the trigger object has been GC'ed and the callback invoked.
(Also, it's how the JS finalizer works, so we don't want to work differently. And they do it for all the same reasons.)

And if we wanted to fail if removing something that wasn't there, it would just mean we had to have a way to check,
and the safe detach code would become if (finalizer.hasDetach(key)) finalizer.detach(key);.
That doesn't bring me joy.

@dcharkes dcharkes closed this as completed Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants