Skip to content

Ensure that tls_destructionMonitor is initialized if a thread is attached to the runtime. - #65358

Merged
VSadov merged 4 commits into
dotnet:mainfrom
VSadov:ensureTlsMonitor
Feb 15, 2022
Merged

VSadov merged 4 commits into
dotnet:mainfrom
VSadov:ensureTlsMonitor

Conversation

@VSadov

@VSadov VSadov commented Feb 15, 2022 •

Copy link
Copy Markdown
Member

Fixes:#64509

I have verified that

  • the repro scenario does cause a slow but steady memory leak (dotnet 7.0 on Ubuntu)
  • there is no leak after this fix is applied.

@ghost ghost added the area-VM-coreclr label Feb 15, 2022
@ghost ghost assigned VSadov Feb 15, 2022
@VSadov
VSadov marked this pull request as ready for review February 15, 2022 03:13
@VSadov
VSadov requested review from janvorli and jkotas February 15, 2022 03:14

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@janvorli

Copy link
Copy Markdown
Member

@VSadov most of the runtime tests are failing with the assert in your new code: _ASSERTE(m_thread == NULL || m_thread == thread);

@jkotas

jkotas commented Feb 15, 2022

Copy link
Copy Markdown
Member

I guess that the problem is that we are setting up the destructor during shutdown when we are setting the thread to NULL. We should not be setting up the destructor for that case.

@VSadov

VSadov commented Feb 15, 2022

Copy link
Copy Markdown
Member Author

Right, the assert is too strong since threads can detach before terminating.
For our purposes here we only require that thread == GetThreadNULLOk(). I will relax the assert.

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you also want to call EnsureTlsDestructionMonitor only when there is something to cleanup. It is unnecessary and potentially problematic to call it when we are clearing the TLS.

Comment thread src/coreclr/vm/threads.cpp Outdated
LIMITED_METHOD_CONTRACT

gCurrentThreadInfo.m_pThread = t;
EnsureTlsDestructionMonitor(t);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
EnsureTlsDestructionMonitor(t);
if (t != NULL)
EnsureTlsDestructionMonitor(t);

@VSadov VSadov Feb 15, 2022 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

With this change, the destructor would need to fetch the thread via GetThreadNULLOk() to make sure we are not touching a thread that is dead (not sure if what we do is safe in such case, we change COOP mode, for example).

That might be a good idea though if we want a minimal change for 6.0 porting purposes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

~TlsDestructionMonitor calls DetachThread. DetachThread calls SetThread(NULL). I think it would be a good idea to avoid calling EnsureTlsDestructionMonitor from SetThread(NULL) on this path to avoid attempt to re-register object that is being destructed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, I just mean we should not rely on storing thread in m_thread and should fetch it via GetThreadNULLOk().

If I use m_thread I get a lot of asserts when switching mode when there is no current thread any more.

Comment thread src/coreclr/vm/ceemain.cpp Outdated
// clean up for us instead).

Thread* thread = GetThreadNULLOk();
Thread* thread = m_thread;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we delete the ThreadDetach call from DeleteThread and always depend on this path to cleanup the thread?

It would improve the test coverage for the thread cleanup logic - there won't be difference between thread cleanup for threads created by the runtime and threads that wandered into the runtime.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Should we delete the ThreadDetach call from DeleteThread and always depend on this path to cleanup the thread?

Perhaps in a separate change that we would not port to 6.0 ?
just to manage the risk of regressesing something.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sounds reasonable.

Comment thread src/coreclr/vm/ceemain.cpp Outdated

~TlsDestructionMonitor()
{
// Don't destroy threads here if we're in shutdown (shutdown will

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This comment does not look relevant. (It was not relevant even before this change.)

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks

@VSadov

VSadov commented Feb 15, 2022

Copy link
Copy Markdown
Member Author

Thanks!!

@VSadov
VSadov merged commit 7d9fc7a into dotnet:main Feb 15, 2022
@VSadov
VSadov deleted the ensureTlsMonitor branch February 15, 2022 22:33
@VSadov

VSadov commented Feb 15, 2022

Copy link
Copy Markdown
Member Author

/backport to release/6.0

@github-actions

Copy link
Copy Markdown
Contributor

@VSadov
VSadov restored the ensureTlsMonitor branch February 15, 2022 22:51
@VSadov
VSadov deleted the ensureTlsMonitor branch February 15, 2022 22:52
@ghost ghost locked as resolved and limited conversation to collaborators Mar 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants