dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.57k stars 4.55k forks source link

ThreadLocals and Finalization/GC #101839

Open benaadams opened 2 months ago

benaadams commented 2 months ago

When a thread exits; all its thread locals end up on the finalization queue (perhaps unsurprisingly)

However the runtime is controling when threads are exited; would it be possible it instead dispose all these locals as part of the thread exit so they don't all end up on the finalization queue?

dotnet-policy-service[bot] commented 2 months ago

Tagging subscribers to this area: @mangod9 See info in area-owners.md if you want to be subscribed.

dotnet-policy-service[bot] commented 2 months ago

Tagging subscribers to this area: @dotnet/gc See info in area-owners.md if you want to be subscribed.

KalleOlaviNiemitalo commented 2 months ago

The thread-local field might not be the only thing that refers to the same instance. In that case, it would be wrong to automatically dispose or finalize the referenced instance when the thread exits, unless the thread-local field is opted in somehow. (Perhaps a new bool DisposeValueOnThreadExit property on ThreadStaticAttribute and ThreadLocal<T>; the IDisposable.Dispose implementation could then call GC.SuppressFinalize.)

dotnet-policy-service[bot] commented 2 months ago

Tagging subscribers to this area: @mangod9 See info in area-owners.md if you want to be subscribed.

mangod9 commented 1 week ago

Adding @davidwrighton who has been doing work in this area.