{"record":{"id":"a7f76581b0198a29","repo":"dotnet/wpf","slug":"error-sem-timeout-121-wait-timeout-258-error-service-request","errorCode":"ERROR_SEM_TIMEOUT (121) / WAIT_TIMEOUT (258) / ERROR_SERVICE_REQUEST_TIMEOUT (1053) / ERROR_TIMEOUT (1460)","errorMessage":"TimeoutException","messagePattern":"TimeoutException","errorType":"exception","errorClass":"TimeoutException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/Misc.cs","lineNumber":1701,"sourceCode":"\n                // We're getting this in AMD64 when calling RealGetWindowClass; adding this code\n                // to allow the DRTs to pass while we continue investigation.\n                case 87:    //   87 ERROR_INVALID_PARAMETER\n                    throw new ElementNotAvailableException();\n\n                case 8:     //    8 ERROR_NOT_ENOUGH_MEMORY         Not enough storage is available to process this command.\n                case 14:    //   14 ERROR_OUTOFMEMORY               Not enough storage is available to complete this operation.\n                    throw new OutOfMemoryException();\n\n                case 998:   //  998 ERROR_NOACCESS                  Invalid access to memory location.\n                case 5:     //    5 ERROR_ACCESS_DENIED\n                    throw new InvalidOperationException();\n\n                case 121:   //  121 ERROR_SEM_TIMEOUT               The semaphore timeout period has expired.\n                case 258:   //  258 WAIT_TIMEOUT                    The wait operation timed out.\n                case 1053:  // 1053 ERROR_SERVICE_REQUEST_TIMEOUT   The service did not respond to the start or control request in a timely fashion.\n                case 1460:  // 1460 ERROR_TIMEOUT                   This operation returned because the timeout period expired.\n                    throw new TimeoutException();\n\n                default:\n                    // Not sure how to map the reset of the error codes so throw generic Win32Exception.\n                    throw new Win32Exception(errorCode);\n            }\n        }\n\n        internal static bool UnhookWinEvent(IntPtr winEventHook)\n        {\n            // There is no indication in the Windows SDK documentation that UnhookWinEvent()\n            // will set an error to be retrieved with GetLastError\n            return UnsafeNativeMethods.UnhookWinEvent(winEventHook);\n        }\n\n        internal static bool UnionRect(out NativeMethods.Win32Rect rcDst, ref NativeMethods.Win32Rect rc1, ref NativeMethods.Win32Rect rc2)\n        {\n            bool result = SafeNativeMethods.UnionRect(out rcDst, ref rc1, ref rc2);\n            int lastWin32Error = Marshal.GetLastWin32Error();","sourceCodeStart":1683,"sourceCodeEnd":1719,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/Misc.cs#L1683-L1719","documentation":"The Win32 error mapper throws TimeoutException when the native call fails with a timeout-class error: ERROR_SEM_TIMEOUT (121), WAIT_TIMEOUT (258), ERROR_SERVICE_REQUEST_TIMEOUT (1053), or ERROR_TIMEOUT (1460). The target window's UI thread did not answer within the permitted period, so the automation operation times out instead of hanging or returning garbage.","triggerScenarios":"Cross-process SendMessage/accessibility calls to a window whose UI thread is blocked, hung, or in a modal loop, returning native timeout errors 121/258/1053/1460.","commonSituations":"Target app frozen (not pumping messages); a modal drag/resize or debug break blocking the UI thread; slow startup under load; target app waiting on a dialog the automation did not notice.","solutions":["Increase the automation timeout settings and retry — a busy UI thread often becomes responsive","Diagnose the target app's UI thread (dump the process, check for message-loop hangs, debugger breakpoints)","Dismiss modal states (drag loops, menus, dialogs) before issuing further automation calls","Catch TimeoutException per operation and re-walk/re-acquire the element rather than reusing the stale provider"],"exampleFix":"// before\nvar value = element.GetCurrentPropertyValue(AutomationElement.NameProperty); // may throw TimeoutException\n// after\ntry\n{\n    var value = element.GetCurrentPropertyValue(AutomationElement.NameProperty);\n}\ncatch (TimeoutException)\n{\n    // target UI thread not responding: retry with backoff or restart target\n    Thread.Sleep(retryDelay);\n    value = ReacquireAndGet();\n}","handlingStrategy":"retry","validationCode":"// Probe responsiveness before real calls:\nif (!IsHungAppWindow(hwnd)) { Proceed(hwnd); } else { WaitForResponsive(hwnd, timeout); }","typeGuard":null,"tryCatchPattern":"for (int attempt = 0; attempt < maxRetries; attempt++)\n{\n    try { return DoUiaOperation(); }\n    catch (TimeoutException) { Thread.Sleep(backoff); ReacquireElement(); }\n}\nthrow new TimeoutException(\"target UI thread unresponsive\");","preventionTips":["Use IsHungAppWindow or UIA responsiveness checks before issuing calls","Keep automation timeouts generous for slow targets and app startup","Detect and dismiss modal states (drag loops, open menus, stuck dialogs)","Never debug-break the target app while automation is running against it"],"tags":["ui-automation","timeout","hung-ui-thread","win32-error"],"backgroundTag":"request-timeout","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}