{"record":{"id":"3bc6f480a1ba60a6","repo":"Hmbown/CodeWhale","slug":"getstdhandle-std-error-handle-failed","errorCode":null,"errorMessage":"GetStdHandle(STD_ERROR_HANDLE) failed","messagePattern":"GetStdHandle\\(STD_ERROR_HANDLE\\) failed","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/tui/src/runtime_log.rs","lineNumber":322,"sourceCode":"\n#[cfg(windows)]\nfn redirect_stderr_to(\n    file: &File,\n) -> Result<(\n    windows::Win32::Foundation::HANDLE,\n    windows::Win32::Foundation::HANDLE,\n)> {\n    use std::os::windows::io::AsRawHandle;\n    use windows::Win32::Foundation::{CloseHandle, DUPLICATE_SAME_ACCESS, DuplicateHandle, HANDLE};\n    use windows::Win32::System::Console::{GetStdHandle, STD_ERROR_HANDLE, SetStdHandle};\n    use windows::Win32::System::Threading::GetCurrentProcess;\n\n    // SAFETY: GetStdHandle is always available; returns INVALID_HANDLE_VALUE\n    // on failure or null-like handles for console-less processes.\n    let saved =\n        unsafe { GetStdHandle(STD_ERROR_HANDLE) }.context(\"GetStdHandle(STD_ERROR_HANDLE)\")?;\n    if saved.is_invalid() {\n        return Err(anyhow::anyhow!(\"GetStdHandle(STD_ERROR_HANDLE) failed\"));\n    }\n\n    // Duplicate the file handle so the redirected stderr owns an\n    // independent HANDLE — mirroring the Unix path's `libc::dup`.\n    // Without this, `_file` and stderr would alias the same HANDLE;\n    // a rogue `CloseHandle` on stderr would silently invalidate `_file`.\n    let raw = HANDLE(file.as_raw_handle());\n    let process = unsafe { GetCurrentProcess() };\n    let mut dup = HANDLE::default();\n    unsafe {\n        DuplicateHandle(\n            process,\n            raw,\n            process,\n            &mut dup,\n            0,\n            false,\n            DUPLICATE_SAME_ACCESS,","sourceCodeStart":304,"sourceCodeEnd":340,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/0c42157ee52f9d55af2b506d71b46249910f77d3/crates/tui/src/runtime_log.rs#L304-L340","documentation":"On Windows, GetStdHandle(STD_ERROR_HANDLE) failed while setting up the stderr redirect for runtime logging — the process has no usable standard error handle (e.g. detached console-less context), so redirection cannot proceed.","triggerScenarios":"Thrown at crates/tui/src/runtime_log.rs:322 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Run the binary from a normal console or redirect context that provides std handles","Run with stderr redirected to a file"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"0c42157ee52f9d55af2b506d71b46249910f77d3","analyzedAt":"2026-08-20T21:50:45.477Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}