@@ -42,43 +42,46 @@ FILE_WATCHER::FILE_WATCHER() :
4242FILE_WATCHER::~FILE_WATCHER ()
4343{
4444 StopMonitor ();
45- WaitForMonitor ( 20 ); // wait for 1 second total
45+ WaitForWatcherThreadExit ();
4646}
4747
48- void FILE_WATCHER::WaitForMonitor (DWORD dwRetryCounter )
48+ void FILE_WATCHER::WaitForWatcherThreadExit ( )
4949{
5050 if (m_hChangeNotificationThread == nullptr )
5151 {
5252 return ;
5353 }
5454
55- while (!m_fThreadExit && dwRetryCounter-- > 0 )
55+ if (m_fRudeThreadTermination )
5656 {
57- // Check if the thread has exited.
58- DWORD result = WaitForSingleObject (m_hChangeNotificationThread, 0 );
59- if (result == WAIT_OBJECT_0)
57+ // This is the old behavior, which is now opt-in using an environment variable. Wait for
58+ // the thread to exit, but if it doesn't exit soon enough, terminate it.
59+ const int totalWaitTimeMs = 10000 ;
60+ const int waitIntervalMs = 50 ;
61+ const int iterations = totalWaitTimeMs / waitIntervalMs;
62+ for (int i = 0 ; i < iterations && !m_fThreadExit; i++)
6063 {
61- // The thread has exited.
62- m_fThreadExit = true ;
63- break ;
64- }
65- else
66- {
67- // The thread is still alive. Wait for 50ms.
68- WaitForSingleObject (m_hChangeNotificationThread, 50 );
64+ // Check if the thread has exited.
65+ DWORD result = WaitForSingleObject (m_hChangeNotificationThread, waitIntervalMs) ;
66+ if (result == WAIT_OBJECT_0)
67+ {
68+ // The thread has exited.
69+ m_fThreadExit = true ;
70+ break ;
71+ }
6972 }
70- }
71-
72- if (!m_fThreadExit)
73- {
74- LOG_INFO (L" File watcher thread didn't seem to exit." );
7573
76- if (m_fRudeThreadTermination )
74+ if (!m_fThreadExit )
7775 {
78- LOG_INFO (L" File watcher thread was terminated ." );
76+ LOG_INFO (L" File watcher thread did not exit. Forcing termination ." );
7977 TerminateThread (m_hChangeNotificationThread, 1 );
8078 }
8179 }
80+ else
81+ {
82+ // Wait for the thread to exit.
83+ WaitForSingleObject (m_hChangeNotificationThread, INFINITE);
84+ }
8285}
8386
8487HRESULT
@@ -466,7 +469,7 @@ FILE_WATCHER::StopMonitor()
466469
467470 // Signal the file watcher thread to exit
468471 SetEvent (m_pShutdownEvent);
469- WaitForMonitor ( 200 );
472+ WaitForWatcherThreadExit ( 10000 );
470473
471474 if (m_fShadowCopyEnabled)
472475 {
0 commit comments