Skip to content

[GC] Add support for asking GC if a destructor is being run from a finalizer #426

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
merged 1 commit into from
May 27, 2025

Conversation

schveiguy
Copy link
Contributor

This mirrors the PR on the symgc project:

symmetryinvestments/symgc#16

The inFinalizer support has become necessary due to some latent bugs in phobos that were recently fixed.

Names up for debate. I'm bad at names.

…nalizer. This provides support for druntime GC.inFinalizer method.
@deadalnix
Copy link
Contributor

I don't really understand what problem does this solve. In general, I'd be suspicious of state addition. Why are we doing this?

@schveiguy
Copy link
Contributor Author

The GC.inFinalizer call is used to tell when in a destructor, whether it is being called from a finalizer in the GC.

Why might you want to know this? Well, you are not allowed to allocate (in the current GC), so that is one reason. But the main reason I see is, a struct that is on the heap may use references that it thinks are thread-local, but it might be run from another thread. It also may be trying to access GC allocated data that has already been collected. There are some examples on the docs for inFinalizer.

See the note on dlang.org:

Important: The order in which the garbage collector calls destructors for unreferenced objects is not specified. This means that when the garbage collector calls a destructor for an object of a class that has members which are references to garbage collected objects, those references may no longer be valid. This means that destructors cannot reference sub objects.

this actually was happening in Phobos, when running the new GC on windows, and I had to fix it. The fix is to only use the referenced data if not inside a finalizer. See the fix here: dlang/phobos#10788

So the fix needs to use inFinalizer, but this was not supported on the new GC, so I had to add it (see PR above).

@deadalnix
Copy link
Contributor

How does this relates to inFinalizer?

@schveiguy
Copy link
Contributor Author

The druntime hook needs a method to call on the SDC gc to get the correct information.

See how it's done in symgc (which doesn't have to go through an extern(C) interface): https://github.com/symmetryinvestments/symgc/blob/823e265f1823e10cc084efdc9ff690b1256b3cc0/source/symgc/gcobj.d#L370

@schveiguy
Copy link
Contributor Author

Note, I did add this in a gap that would be unallocated due to alignment. So this shouldn't add any size to the ThreadCache.

@deadalnix deadalnix merged commit df910c8 into snazzy-d:master May 27, 2025
5 checks passed
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.

2 participants