{"record":{"id":"0c323c0d79ee81ce","repo":"microsoft/aspire","slug":"failed-to-invoke-getexitcodeprocess-while-starting-tracked","errorCode":null,"errorMessage":"Failed to invoke GetExitCodeProcess while starting tracked browser CDP pipe.","messagePattern":"Failed to invoke GetExitCodeProcess while starting tracked browser CDP pipe\\.","errorType":"exception","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Browsers/BrowserLogsPipeBrowserProcessLauncher.Windows.cs","lineNumber":227,"sourceCode":"            builder.Append(character);\n        }\n\n        builder.Append('\"');\n    }\n\n    private static async Task<BrowserLogsProcessResult> WaitForWindowsProcessAsync(SafeWaitHandle processHandle)\n    {\n        return await Task.Run(() =>\n        {\n            var waitResult = WaitForSingleObject(processHandle.DangerousGetHandle(), INFINITE);\n            if (waitResult != WAIT_OBJECT_0)\n            {\n                throw CreateWindowsException(\"WaitForSingleObject\");\n            }\n\n            if (!GetExitCodeProcess(processHandle.DangerousGetHandle(), out var exitCode))\n            {\n                throw CreateWindowsException(\"GetExitCodeProcess\");\n            }\n\n            return new BrowserLogsProcessResult(unchecked((int)exitCode));\n        }).ConfigureAwait(false);\n    }\n\n    private static Win32Exception CreateWindowsException(string operation) =>\n        new(Marshal.GetLastWin32Error(), $\"Failed to invoke {operation} while starting tracked browser CDP pipe.\");\n\n    private static void CloseWindowsHandle(IntPtr handle)\n    {\n        if (handle != IntPtr.Zero && !CloseHandle(handle))\n        {\n            throw CreateWindowsException(\"CloseHandle\");\n        }\n    }\n\n    private static void TryKillProcessTree(int processId)","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Browsers/BrowserLogsPipeBrowserProcessLauncher.Windows.cs#L209-L245","documentation":"After the tracked browser process exits, the launcher reads its exit code via GetExitCodeProcess. If that OS call fails, this Win32Exception-named error is thrown. It means the exit status of the browser process could not be retrieved, so the CDP pipe session result is unknown.","triggerScenarios":"WaitForWindowsProcessAsync calls GetExitCodeProcess(processHandle.DangerousGetHandle(), out exitCode) and it returns FALSE - the handle is no longer valid or the OS call fails for handle/access reasons.","commonSituations":"Handle closed between the successful wait and the exit-code read; process cleanup racing with the launcher; restricted token lacking PROCESS_QUERY_INFORMATION; corrupted handle from an earlier failure.","solutions":["Check the inner NativeErrorCode for the OS reason (typically ERROR_INVALID_HANDLE=6).","Avoid closing/disposing the process handle while the launcher session is active.","Retry the browser launch; the session result is unrecoverable.","Verify the hosting account has rights to query process information.","Update Aspire.Hosting.Browsers for potential handle-lifecycle fixes."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"if (processHandle.IsClosed || processHandle.IsInvalid)\n    throw new InvalidOperationException(\"Browser process handle is no longer valid; exit code unavailable.\");","typeGuard":null,"tryCatchPattern":"try\n{\n    var result = await WaitForBrowserAsync(processHandle);\n}\ncatch (Win32Exception ex) when (ex.Message.Contains(\"GetExitCodeProcess\"))\n{\n    logger.LogWarning(ex, \"Could not read browser exit code (Win32 {Code}); relaunching.\", ex.NativeErrorCode);\n    await RelaunchTrackedBrowserAsync();\n}","preventionTips":["Keep the process handle alive until the launcher finishes reading the exit code.","Ensure the hosting account can query process information.","Relaunch the session when the exit status is unrecoverable."],"tags":["windows","process-exit-code","win32","browser"],"backgroundTag":"process-wait-failed","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T09:17:21.228Z"}