Skip to content

fix(windows): avoid blocking cross-thread GetWindowTextLengthW in WindowManager::GetCurrent/GetAll - #116

Merged
lijy91 merged 1 commit into
mainfrom
fix/windows-get-current-cross-thread-hang-issue-76
Dec 4, 2026
Merged

lijy91 merged 1 commit into
mainfrom
fix/windows-get-current-cross-thread-hang-issue-76

Conversation

@lijy91

@lijy91 lijy91 commented Dec 4, 2026

Copy link
Copy Markdown
Member

Fixes libnativeapi/nativeapi#76.

Root Cause

  • In WindowManager::GetCurrent() (FindCurrentWindowCandidates) and WindowManager::GetAll() (EnumWindowsCallback), calling GetWindowTextLengthW(hwnd) on a same-process window owned by a different thread dispatches WM_GETTEXTLENGTH via synchronous SendMessageW.
  • If that other thread is blocked and not pumping Win32 messages (for example, a WinRT MediaPlayer SMTC window helper HWND on a worker thread blocked in WaitOnAddress), the main UI thread deadlocks indefinitely.

Fix

  • Added TryGetWindowTextLength(HWND) which uses GetWindowTextLengthW only for same-thread windows and falls back to bounded SendMessageTimeoutW(hwnd, WM_GETTEXTLENGTH, 0, 0, SMTO_ABORTIFHUNG | SMTO_NORMAL, 50, &length) for cross-thread windows, skipping unresponsive candidates.
  • In FindCurrentWindowCandidates, skip hidden candidates once a visible or hidden candidate has already been found so extra hidden helper windows are not queried unnecessarily.
  • Added regression test window_manager_get_current_windows_test creating a blocked worker-thread top-level HWND alongside a main-thread app window and verifying GetCurrent() and GetAll() return promptly.

Verification

  • Built and passed ctest -C Release -R window_manager_get_current_windows_test on Windows (0.10s).

@lijy91
lijy91 merged commit e337121 into main Dec 4, 2026
11 checks passed
@lijy91
lijy91 deleted the fix/windows-get-current-cross-thread-hang-issue-76 branch December 4, 2026 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using a single-instance lock for hot restart in debug mode will cause the application to become unresponsive.

1 participant