BigPizzaV3/CodexPlusPlus · error · anyhow::Error

failed to wait for Windows process id

Error message

failed to wait for Windows process id {process_id}

What it means

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.

Solutions

  1. Retry: check whether the process still exists (OpenProcess again) and treat already-exited as success instead of an error
  2. Ensure the process handle is opened with SYNCHRONIZE right so WaitForSingleObject is valid
  3. Log GetLastError() from WaitForSingleObject to identify the precise Win32 failure and handle it explicitly

Example fix

// before
let wait_result = WaitForSingleObject(handle, INFINITE);
let _ = CloseHandle(handle);
if wait_result == WAIT_FAILED { anyhow::bail!("failed to wait for Windows process id {process_id}"); }
// after
let wait_result = WaitForSingleObject(handle, INFINITE);
let _ = CloseHandle(handle);
if wait_result == WAIT_FAILED {
    let err = unsafe { windows::Win32::Foundation::GetLastError() };
    if err == ERROR_INVALID_HANDLE { return Ok(()); } // process already gone
    anyhow::bail!("failed to wait for Windows process id {process_id}: {err:?}");
}
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure the process is still alive before waiting
let handle = unsafe { OpenProcess(PROCESS_SYNCHRONIZE, false, process_id) }?;
if handle.is_invalid() { return Ok(()); } // already exited

Type guard

fn process_handle_valid(h: HANDLE) -> bool { !h.is_invalid() }

Try / catch

match wait_for_windows_process_id(pid).await {
    Ok(()) => {},
    Err(e) if e.to_string().contains("failed to wait") => {
        // treat as non-fatal if the process already exited
        if !is_process_alive(pid) { /* ok */ } else { return Err(e); }
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: `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.

Common situations: Waiting on a short-lived process that already exited (race between open and wait); antivirus/permission restrictions limiting process access; PID reuse invalidating assumptions.

Related errors


AI-assisted analysis of BigPizzaV3/CodexPlusPlus@b1ed92e5e4 (2026-09-19). Data as JSON: /api/errors/f53572ba51ed5620. Report an issue: GitHub.

Appendix: source

Thrown at crates/codex-plus-core/src/launcher.rs:3153

#[cfg(windows)]
fn wait_for_windows_process_id_blocking(process_id: u32) -> anyhow::Result<()> {
    use windows::Win32::Foundation::{CloseHandle, WAIT_FAILED};
    use windows::Win32::System::Threading::{
        INFINITE, OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION, PROCESS_SYNCHRONIZE,
        WaitForSingleObject,
    };

    unsafe {
        let handle = OpenProcess(
            PROCESS_SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION,
            false,
            process_id,
        )
        .with_context(|| format!("failed to open Windows process id {process_id}"))?;
        let wait_result = WaitForSingleObject(handle, INFINITE);
        let _ = CloseHandle(handle);
        if wait_result == WAIT_FAILED {
            anyhow::bail!("failed to wait for Windows process id {process_id}");
        }
    }
    Ok(())
}

#[cfg(windows)]
fn terminate_windows_process_id_blocking(process_id: u32) -> anyhow::Result<()> {
    use windows::Win32::Foundation::CloseHandle;
    use windows::Win32::System::Threading::{
        OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION, PROCESS_TERMINATE, TerminateProcess,
    };

    unsafe {
        let handle = OpenProcess(
            PROCESS_TERMINATE | PROCESS_QUERY_LIMITED_INFORMATION,
            false,
            process_id,
        )

View on GitHub (pinned to b1ed92e5e4)