{"record":{"id":"f53572ba51ed5620","repo":"BigPizzaV3/CodexPlusPlus","slug":"failed-to-wait-for-windows-process-id-process-id","errorCode":null,"errorMessage":"failed to wait for Windows process id {process_id}","messagePattern":"failed to wait for Windows process id (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/codex-plus-core/src/launcher.rs","lineNumber":3153,"sourceCode":"#[cfg(windows)]\nfn wait_for_windows_process_id_blocking(process_id: u32) -> anyhow::Result<()> {\n    use windows::Win32::Foundation::{CloseHandle, WAIT_FAILED};\n    use windows::Win32::System::Threading::{\n        INFINITE, OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION, PROCESS_SYNCHRONIZE,\n        WaitForSingleObject,\n    };\n\n    unsafe {\n        let handle = OpenProcess(\n            PROCESS_SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION,\n            false,\n            process_id,\n        )\n        .with_context(|| format!(\"failed to open Windows process id {process_id}\"))?;\n        let wait_result = WaitForSingleObject(handle, INFINITE);\n        let _ = CloseHandle(handle);\n        if wait_result == WAIT_FAILED {\n            anyhow::bail!(\"failed to wait for Windows process id {process_id}\");\n        }\n    }\n    Ok(())\n}\n\n#[cfg(windows)]\nfn terminate_windows_process_id_blocking(process_id: u32) -> anyhow::Result<()> {\n    use windows::Win32::Foundation::CloseHandle;\n    use windows::Win32::System::Threading::{\n        OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION, PROCESS_TERMINATE, TerminateProcess,\n    };\n\n    unsafe {\n        let handle = OpenProcess(\n            PROCESS_TERMINATE | PROCESS_QUERY_LIMITED_INFORMATION,\n            false,\n            process_id,\n        )","sourceCodeStart":3135,"sourceCodeEnd":3171,"githubUrl":"https://github.com/BigPizzaV3/CodexPlusPlus/blob/b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6/crates/codex-plus-core/src/launcher.rs#L3135-L3171","documentation":"On Windows, waiting for a child/external process to exit uses WaitForSingleObject on an opened process handle. WAIT_FAILED means the wait call itself failed (invalid handle, rights problem, or the process/handle became invalid), so the launcher cannot guarantee the process has exited and surfaces the error with the PID.","triggerScenarios":"`wait_for_windows_process_id` is called with a PID whose opened handle yields WAIT_FAILED from WaitForSingleObject — e.g. the process exited and the handle became invalid between OpenProcess and the wait, or the handle lacks SYNCHRONIZE access.","commonSituations":"Waiting on a short-lived process that already exited (race between open and wait); antivirus/permission restrictions limiting process access; PID reuse invalidating assumptions.","solutions":["Retry: check whether the process still exists (OpenProcess again) and treat already-exited as success instead of an error","Ensure the process handle is opened with SYNCHRONIZE right so WaitForSingleObject is valid","Log GetLastError() from WaitForSingleObject to identify the precise Win32 failure and handle it explicitly"],"exampleFix":"// before\nlet wait_result = WaitForSingleObject(handle, INFINITE);\nlet _ = CloseHandle(handle);\nif wait_result == WAIT_FAILED { anyhow::bail!(\"failed to wait for Windows process id {process_id}\"); }\n// after\nlet wait_result = WaitForSingleObject(handle, INFINITE);\nlet _ = CloseHandle(handle);\nif wait_result == WAIT_FAILED {\n    let err = unsafe { windows::Win32::Foundation::GetLastError() };\n    if err == ERROR_INVALID_HANDLE { return Ok(()); } // process already gone\n    anyhow::bail!(\"failed to wait for Windows process id {process_id}: {err:?}\");\n}","handlingStrategy":"try-catch","validationCode":"// ensure the process is still alive before waiting\nlet handle = unsafe { OpenProcess(PROCESS_SYNCHRONIZE, false, process_id) }?;\nif handle.is_invalid() { return Ok(()); } // already exited","typeGuard":"fn process_handle_valid(h: HANDLE) -> bool { !h.is_invalid() }","tryCatchPattern":"match wait_for_windows_process_id(pid).await {\n    Ok(()) => {},\n    Err(e) if e.to_string().contains(\"failed to wait\") => {\n        // treat as non-fatal if the process already exited\n        if !is_process_alive(pid) { /* ok */ } else { return Err(e); }\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Open the process handle with SYNCHRONIZE access right","Handle the already-exited race (open-then-wait) explicitly","Capture GetLastError for WAIT_FAILED diagnostics","Avoid waiting on PIDs captured long ago (PID reuse)"],"tags":["rust","windows","win32","process-wait"],"backgroundTag":"process-wait-failed","analyzedSha":"b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6","analyzedAt":"2026-09-19T23:35:21.129Z","contentChangedAt":"2026-09-19T23:35:21.129Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}