[release/6.0] Ensure that tls_destructionMonitor is initialized if a thread is attached to the runtime. #65406
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Backport of #65358 to release/6.0
Fixes:#64509
/cc @VSadov
Customer Impact
In a case when a thread has not been created by the runtime, but instead "wandered" into managed code - by the means of reverse p-invoke or the like, we do not have an entry point frame that would perform pre-mortem cleanup for the data that VM associates with the thread when the thread exits.
Instead we rely on c++ destructor for a thread-static variable that calls cleanup.
The issue here is that thread-static variable could be initialized lazily (it was observed on libc Linux) - if there is no "use" of the variable, there is no initialization and thus there is no destruction.
As a result the cleanup code is not called and we have a small, but steady leak every time a native thread that has seen managed code is subsequently terminated.
This change "uses" the thread-static sentinel variable whenever a thread is attached to the runtime, thus making the destructor called reliably.
Testing
I have verified manually that
Risk
Low. We use the same cleanup strategy as before.
The change primarily ensures that destructor call cannot be optimized away if a thread was attached to the runtime.