From 52068dcf166b02c72ecbb810f802737d680afac3 Mon Sep 17 00:00:00 2001 From: LiJianying Date: Fri, 4 Dec 2026 16:03:32 +0800 Subject: [PATCH] fix(windows): avoid blocking cross-thread GetWindowTextLengthW in WindowManager::GetCurrent/GetAll --- .../windows/window_manager_windows.cpp | 32 ++++++- tests/CMakeLists.txt | 4 + ...indow_manager_get_current_windows_test.cpp | 94 +++++++++++++++++++ 3 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 tests/window_manager_get_current_windows_test.cpp diff --git a/src/platform/windows/window_manager_windows.cpp b/src/platform/windows/window_manager_windows.cpp index 5791881..30bae4b 100644 --- a/src/platform/windows/window_manager_windows.cpp +++ b/src/platform/windows/window_manager_windows.cpp @@ -1281,6 +1281,26 @@ std::shared_ptr WindowManager::Get(WindowId id) { return WindowRegistry::GetInstance().Get(id); } +// Queries a same-process window's title length without blocking indefinitely +// when the window belongs to another thread that is not pumping messages +// (e.g. a WinRT MediaPlayer SMTC helper window owned by a blocked worker +// thread). Calling GetWindowTextLengthW across threads in the same process +// dispatches WM_GETTEXTLENGTH via synchronous SendMessageW and can deadlock. +static int TryGetWindowTextLength(HWND hwnd) { + if (!hwnd || !IsWindow(hwnd)) { + return 0; + } + if (GetWindowThreadProcessId(hwnd, nullptr) == GetCurrentThreadId()) { + return GetWindowTextLengthW(hwnd); + } + DWORD_PTR length = 0; + if (!SendMessageTimeoutW(hwnd, WM_GETTEXTLENGTH, 0, 0, + SMTO_ABORTIFHUNG | SMTO_NORMAL, 50, &length)) { + return 0; + } + return static_cast(length); +} + // Callback for EnumWindows to collect all top-level windows static BOOL CALLBACK EnumWindowsCallback(HWND hwnd, LPARAM lParam) { auto* windows = reinterpret_cast*>(lParam); @@ -1288,7 +1308,7 @@ static BOOL CALLBACK EnumWindowsCallback(HWND hwnd, LPARAM lParam) { // Only include this process's visible windows that have a title (filters out // helper/background windows and every other application's windows). if (IsOwnProcessWindow(hwnd) && IsWindowVisible(hwnd)) { - int length = GetWindowTextLengthW(hwnd); + int length = TryGetWindowTextLength(hwnd); if (length > 0) { // Check if it's a normal window (not tool window, etc.) LONG exStyle = GetWindowLong(hwnd, GWL_EXSTYLE); @@ -1348,11 +1368,15 @@ BOOL CALLBACK FindCurrentWindowCandidates(HWND hwnd, LPARAM data) { if (ex_style & (WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE)) { return TRUE; } - if (!GetPropW(hwnd, kWindowIdProperty) && GetWindowTextLengthW(hwnd) <= 0) { + auto* candidates = reinterpret_cast(data); + const bool visible = IsWindowVisible(hwnd) != FALSE; + if (!visible && (candidates->first_visible || candidates->first_hidden)) { return TRUE; } - auto* candidates = reinterpret_cast(data); - if (IsWindowVisible(hwnd)) { + if (!GetPropW(hwnd, kWindowIdProperty) && TryGetWindowTextLength(hwnd) <= 0) { + return TRUE; + } + if (visible) { if (!candidates->first_visible) { candidates->first_visible = hwnd; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2e004b2..faf1451 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -352,6 +352,10 @@ if(WIN32) target_link_libraries(window_mouse_policy_windows_test PRIVATE nativeapi) add_test(NAME window_mouse_policy_windows_test COMMAND window_mouse_policy_windows_test) set_tests_properties(window_mouse_policy_windows_test PROPERTIES TIMEOUT 10) + add_executable(window_manager_get_current_windows_test window_manager_get_current_windows_test.cpp) + target_link_libraries(window_manager_get_current_windows_test PRIVATE nativeapi) + add_test(NAME window_manager_get_current_windows_test COMMAND window_manager_get_current_windows_test) + set_tests_properties(window_manager_get_current_windows_test PROPERTIES TIMEOUT 10) add_executable(window_system_menu_windows_test window_system_menu_windows_test.cpp) target_link_libraries(window_system_menu_windows_test PRIVATE nativeapi) # Explicit desktop regression; captures only a pixel inside its own windows. diff --git a/tests/window_manager_get_current_windows_test.cpp b/tests/window_manager_get_current_windows_test.cpp new file mode 100644 index 0000000..7e425fd --- /dev/null +++ b/tests/window_manager_get_current_windows_test.cpp @@ -0,0 +1,94 @@ +// Regression test for libnativeapi/nativeapi#76: +// WindowManager::GetCurrent() and WindowManager::GetAll() must not deadlock +// when another thread in the same process owns a top-level HWND (such as +// WinRT's MediaPlayer SMTC helper window) and is blocked without pumping +// Win32 messages. +#include + +#include +#include +#include +#include + +#include "nativeapi.h" + +namespace { +int g_failures = 0; + +void Check(bool ok, const char* label) { + std::cout << (ok ? "PASS " : "FAIL ") << label << std::endl; + g_failures += !ok; +} +} // namespace + +int main() { + HINSTANCE hinst = GetModuleHandleW(nullptr); + + WNDCLASSW cls = {}; + cls.lpfnWndProc = DefWindowProcW; + cls.hInstance = hinst; + cls.lpszClassName = L"NativeApiGetCurrentCrossThreadTestWindow"; + RegisterClassW(&cls); + + HANDLE worker_ready = CreateEventW(nullptr, TRUE, FALSE, nullptr); + HANDLE worker_release = CreateEventW(nullptr, TRUE, FALSE, nullptr); + std::atomic blocked_hwnd{nullptr}; + + // Worker thread creates a hidden top-level HWND (with no WS_EX_TOOLWINDOW, + // matching WinRT's MediaPlayer SMTC window) and then blocks without pumping + // messages. A synchronous cross-thread GetWindowTextLengthW would deadlock. + std::thread worker([&] { + HWND hwnd = CreateWindowExW( + 0, cls.lpszClassName, L"Blocked Worker Window", WS_OVERLAPPEDWINDOW, + 0, 0, 200, 100, nullptr, nullptr, hinst, nullptr); + blocked_hwnd.store(hwnd); + SetEvent(worker_ready); + WaitForSingleObject(worker_release, INFINITE); + if (hwnd) { + DestroyWindow(hwnd); + } + }); + + WaitForSingleObject(worker_ready, 5000); + Check(blocked_hwnd.load() != nullptr, "created blocked worker-thread HWND"); + + // Create the app's main window on the calling thread, shown without + // activation so GetActiveWindow() is null and GetCurrent() must take the + // EnumWindows fallback path. + HWND main_hwnd = CreateWindowExW( + 0, cls.lpszClassName, L"Main App Window", WS_OVERLAPPEDWINDOW, + 100, 100, 400, 300, nullptr, nullptr, hinst, nullptr); + Check(main_hwnd != nullptr, "created main-thread app HWND"); + ShowWindow(main_hwnd, SW_SHOWNOACTIVATE); + + const auto start = std::chrono::steady_clock::now(); + auto current = nativeapi::WindowManager::GetInstance().GetCurrent(); + auto all = nativeapi::WindowManager::GetInstance().GetAll(); + const auto elapsed_ms = + std::chrono::duration_cast( + std::chrono::steady_clock::now() - start) + .count(); + + Check(elapsed_ms < 2000, + "GetCurrent() and GetAll() return promptly despite blocked worker HWND"); + Check(current != nullptr && current->GetNativeObject() == main_hwnd, + "GetCurrent() resolves the main app window"); + + bool found_main = false; + for (const auto& w : all) { + if (w && w->GetNativeObject() == main_hwnd) { + found_main = true; + break; + } + } + Check(found_main, "GetAll() includes the visible main app window"); + + SetEvent(worker_release); + worker.join(); + CloseHandle(worker_ready); + CloseHandle(worker_release); + if (main_hwnd) { + DestroyWindow(main_hwnd); + } + return g_failures ? 1 : 0; +}