-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Improve ANCM file watcher thread management #49696
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
Improve ANCM file watcher thread management #49696
Conversation
|
||
while (!m_fThreadExit && dwRetryCounter > 0) | ||
while (!m_fThreadExit && dwRetryCounter-- > 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if the thread doesn't exit after the loop completes?
And, should we log in that case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One option if we want to de-risk this in .NET 8 is to enable the termination behavior behind a flag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the thread doing that could be "bad" if it's still running?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Offline discussion: The thread is holding onto state that can have undefined behavior if accessed when the class is destructed. e.g. FILE_WATCHER*
and the m_pShutdownEvent
and m_hCompletionPort
events.
Which brings up another thing, those events are never closed via CloseHandle
it looks like.
else | ||
{ | ||
// Wait for the thread to exit. | ||
WaitForSingleObject(m_hChangeNotificationThread, INFINITE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WaitForSingleObject(m_hChangeNotificationThread, INFINITE); | |
LOG_INFO(L"Waiting for File watcher thread to exit."); | |
WaitForSingleObject(m_hChangeNotificationThread, INFINITE); |
So we know if it's hanging. The thread itself logs when it exits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a log should be added
e549315
to
6382c2b
Compare
I'm trying to get rid of
TerminateThread
usage in ANCM and decided to tackle the file watcher thread first. This change includes various improvements to the file watcher thread management and shutdown mechanisms.m_pShutdownEvent
that provides a more explicit and straightforward way to signal the thread to exit.FILE_WATCHER::ChangeNotificationThread
to useWaitForMultipleObjects
to wait on either the completion port or the new shutdown event, removing the need for theFILE_WATCHER_SHUTDOWN_KEY
mechanism.FILE_WATCHER::WaitForMonitor
by removing calls toGetExitCodeThread
andTerminateThread
, relying onWaitForSingleObject
to handle thread state checking and waiting.m_fThreadExit
with astd::atomic<bool>
Contributes towards #45066