From cfe6b9e5946452858de40d16aa2b6f01c2d0d275 Mon Sep 17 00:00:00 2001 From: Luca Fontanot Date: Wed, 20 May 2026 11:53:50 +0200 Subject: [PATCH] fix: Thread detach before teardown is race-prone. --- electron/native/wgc-capture/src/cursor-sampler.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/electron/native/wgc-capture/src/cursor-sampler.cpp b/electron/native/wgc-capture/src/cursor-sampler.cpp index 0a77392..21558c7 100644 --- a/electron/native/wgc-capture/src/cursor-sampler.cpp +++ b/electron/native/wgc-capture/src/cursor-sampler.cpp @@ -22,6 +22,7 @@ static HHOOK g_mouseHook = nullptr; static DWORD g_mainThreadId = 0; static std::atomic g_leftDownCount{0}; static std::atomic g_leftUpCount{0}; +static std::atomic g_stop{false}; static std::mutex g_stdoutMtx; static LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam) { @@ -314,7 +315,7 @@ static std::string buildAssetJson( static void runSamplingLoop(int intervalMs, HWND targetWindow, const CLSID& pngClsid) { HCURSOR lastCursor = nullptr; - while (true) { + while (!g_stop.load(std::memory_order_relaxed)) { const int downCount = g_leftDownCount.exchange(0, std::memory_order_relaxed); const int upCount = g_leftUpCount.exchange(0, std::memory_order_relaxed); @@ -473,7 +474,8 @@ int main(int argc, char* argv[]) { DispatchMessage(&msg); } - sampler.detach(); + g_stop.store(true, std::memory_order_relaxed); + if (sampler.joinable()) sampler.join(); UnhookWindowsHookEx(g_mouseHook); Gdiplus::GdiplusShutdown(gdipToken); return 0;