Skip to content

Commit 3dc04b1

Browse files
bmehta001Copilot
andcommitted
Prevent network shutdown queue starvation
Use a kernel stop event with MsgWaitForMultipleObjects so callback refresh traffic cannot prevent listener termination or make Stop hang after a failed PostThreadMessage. Verified at: - lib/pal/desktop/NetworkDetector.cpp - lib/pal/desktop/NetworkDetector.hpp Files changed: - lib/pal/desktop/NetworkDetector.cpp - lib/pal/desktop/NetworkDetector.hpp Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent fa09281 commit 3dc04b1

2 files changed

Lines changed: 52 additions & 16 deletions

File tree

‎lib/pal/desktop/NetworkDetector.cpp‎

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
#include "DebugEvents.hpp"
1515
#include "pal/PAL.hpp"
1616

17-
#define NETDETECTOR_STOP WM_USER+1
18-
#define NETDETECTOR_REFRESH WM_USER+2
17+
#define NETDETECTOR_REFRESH WM_USER+1
1918

2019
namespace MAT_NS_BEGIN
2120
{
@@ -224,21 +223,41 @@ namespace MAT_NS_BEGIN
224223
cv.notify_all();
225224
}
226225

227-
while (GetMessage(&msg, NULL, 0, 0) > 0)
226+
while (true)
228227
{
228+
const DWORD waitResult = MsgWaitForMultipleObjects(
229+
1,
230+
&stopEvent,
231+
FALSE,
232+
INFINITE,
233+
QS_ALLINPUT);
234+
if (waitResult == WAIT_OBJECT_0)
235+
{
236+
break;
237+
}
238+
if (waitResult == WAIT_FAILED)
239+
{
240+
LOG_ERROR("Unable to wait for network detector events.");
241+
return false;
242+
}
243+
if (!PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
244+
{
245+
continue;
246+
}
247+
if (msg.message == WM_QUIT)
248+
{
249+
break;
250+
}
251+
229252
switch (msg.message)
230253
{
231254
case NETDETECTOR_REFRESH:
232255
GetCurrentNetworkCost();
233256
break;
234-
case NETDETECTOR_STOP:
235-
PostQuitMessage(0);
236-
break;
237257
default:
238-
break;
258+
TranslateMessage(&msg);
259+
DispatchMessage(&msg);
239260
}
240-
TranslateMessage(&msg);
241-
DispatchMessage(&msg);
242261
}
243262
return true;
244263
}
@@ -337,6 +356,14 @@ namespace MAT_NS_BEGIN
337356
startupState = StartupState::Starting;
338357
stopRequested = false;
339358
networkStatusCallbackState = std::make_shared<CallbackState>();
359+
stopEvent = CreateEventW(nullptr, TRUE, FALSE, nullptr);
360+
if (stopEvent == nullptr)
361+
{
362+
LOG_ERROR("Unable to create the network detector stop event.");
363+
startupState = StartupState::Failed;
364+
networkStatusCallbackState.reset();
365+
return false;
366+
}
340367
isRunning = true;
341368
}
342369

@@ -382,6 +409,13 @@ namespace MAT_NS_BEGIN
382409
{
383410
netDetectThread.join();
384411
}
412+
if (!started)
413+
{
414+
std::lock_guard<std::mutex> lock(m_lock);
415+
CloseHandle(stopEvent);
416+
stopEvent = nullptr;
417+
networkStatusCallbackState.reset();
418+
}
385419
return started;
386420
}
387421
};
@@ -400,16 +434,17 @@ namespace MAT_NS_BEGIN
400434
{
401435
networkStatusCallbackState->listenerThreadId.store(0, std::memory_order_release);
402436
}
403-
if (startupState == StartupState::Ready &&
404-
!PostThreadMessage(m_listener_tid, NETDETECTOR_STOP, 0, NULL))
437+
if (!SetEvent(stopEvent))
405438
{
406-
LOG_WARN("NetworkDetector stop message could not be posted.");
439+
LOG_ERROR("Unable to signal the network detector stop event.");
407440
}
408441
}
409442

410443
netDetectThread.join();
411444

412445
std::lock_guard<std::mutex> lock(m_lock);
446+
CloseHandle(stopEvent);
447+
stopEvent = nullptr;
413448
startupState = StartupState::Stopped;
414449
stopRequested = false;
415450
networkStatusCallbackState.reset();

‎lib/pal/desktop/NetworkDetector.hpp‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,15 @@ namespace MAT_NS_BEGIN
7272
bool GetNetworkInfoStats();
7373

7474
std::mutex m_lock;
75-
std::condition_variable cv;
76-
std::atomic<bool> isRunning{ false };
77-
std::thread netDetectThread;
75+
std::condition_variable cv;
76+
std::atomic<bool> isRunning{false};
77+
std::thread netDetectThread;
7878
StartupState startupState = StartupState::Stopped;
7979
bool stopRequested = false;
80+
HANDLE stopEvent = nullptr;
8081

8182
/// <summary>
82-
///
83+
///
8384
/// </summary>
8485
void run();
8586

0 commit comments

Comments
 (0)