{"record":{"id":"37fdda2c86d508f1","repo":"sipeed/picoclaw","slug":"failed-to-redirect-stderr-to-file-v","errorCode":null,"errorMessage":"Failed to redirect stderr to file: %v","messagePattern":"Failed to redirect stderr to file: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"pkg/logger/panic_win.go","lineNumber":21,"sourceCode":"\npackage logger\n\nimport (\n\t\"fmt\"\n\t\"io\"\n\t\"os\"\n\n\t\"golang.org/x/sys/windows\"\n)\n\nfunc initPanicFile(panicFile string) io.WriteCloser {\n\tfile, err := os.OpenFile(panicFile, os.O_WRONLY|os.O_CREATE|os.O_SYNC|os.O_APPEND, 0o600)\n\tif err != nil {\n\t\tpanic(fmt.Sprintf(\"error in open panic: %v\", err))\n\t}\n\terr = windows.SetStdHandle(windows.STD_ERROR_HANDLE, windows.Handle(file.Fd()))\n\tif err != nil {\n\t\tpanic(fmt.Sprintf(\"Failed to redirect stderr to file: %v\", err))\n\t}\n\tos.Stderr = file\n\treturn file\n}\n","sourceCodeStart":3,"sourceCodeEnd":26,"githubUrl":"https://github.com/sipeed/picoclaw/blob/49183d7e8daed0dba89ddbb6fcb60089401d9680/pkg/logger/panic_win.go#L3-L26","documentation":"Windows-only startup panic: windows.SetStdHandle(STD_ERROR_HANDLE, file) failed after the panic log was created. The code redirects the process's Win32 stderr handle to the file (then reassigns os.Stderr) so Go runtime panics land in launcher_panic.log; when SetStdHandle is denied or the handle arrangement is invalid, crash capture cannot be set up and the process aborts.","triggerScenarios":"The process has no valid STD_ERROR_HANDLE — GUI-subsystem launch with no console and no redirected stderr, or a service wrapper that started the process with standard handles stripped; sandbox/job-object restrictions on handle replacement; the file handle from OpenFile somehow invalid.","commonSituations":"Launching the GUI binary through wrappers/tools that strip standard handles; service hosts that provide no std handles; post-injection handle tampering by third-party DLLs.","solutions":["Launch with explicit stderr redirection (run from a console or pipe stderr to a file) so STD_ERROR_HANDLE exists","If running as a service, configure the service wrapper to provide std handles","Reproduce from cmd.exe — if the binary starts there, the wrapper configuration is the culprit"],"exampleFix":"# before\nstart /b launcher.exe   # no std handles attached\n\n# after\nlauncher.exe 2>launch.err.log","handlingStrategy":"validation","validationCode":"if os.Stderr == nil || os.Stderr.Fd() == uintptr(windows.InvalidHandle) {\n    return errors.New(\"no valid stderr handle; launch with stderr redirected before logger.InitPanic\")\n}","typeGuard":null,"tryCatchPattern":"Startup abort before app code runs: fix the launch environment (provide std handles) rather than catching anything in-process; verify by running the same binary from a console.","preventionTips":["Always launch with stderr redirected to a console or file","Test the launcher under the exact service/wrapper configuration","Keep the -console flag available as a diagnostic path"],"tags":["logger","panic","windows","setstdhandle","stderr","startup"],"backgroundTag":null,"analyzedSha":"49183d7e8daed0dba89ddbb6fcb60089401d9680","analyzedAt":"2026-08-15T21:55:41.315Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}