{"record":{"id":"12e191c2d6949e0b","repo":"gravitational/teleport","slug":"failed-to-set-stdin-mode-w","errorCode":null,"errorMessage":"failed to set stdin mode: %w","messagePattern":"failed to set stdin mode: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/client/terminal/terminal_windows.go","lineNumber":88,"sourceCode":"\t\tnewInMode &^= winterm.ENABLE_PROCESSED_INPUT\n\n\t\tnewInMode |= winterm.ENABLE_EXTENDED_FLAGS\n\t\tnewInMode |= winterm.ENABLE_INSERT_MODE\n\t\tnewInMode |= winterm.ENABLE_QUICK_EDIT_MODE\n\t\tnewInMode |= winterm.ENABLE_VIRTUAL_TERMINAL_INPUT\n\n\t\terr = winterm.SetConsoleMode(uintptr(stdinFd), newInMode)\n\t\tif err != nil {\n\t\t\t// Attempt to reset the stdout mode before returning.\n\t\t\terr = winterm.SetConsoleMode(uintptr(stdoutFd), oldOutMode)\n\t\t\tif err != nil {\n\t\t\t\tlog.ErrorContext(context.Background(), \"Failed to reset terminal output mode\",\n\t\t\t\t\t\"original_output_mode\", oldOutMode,\n\t\t\t\t\t\"error\", err,\n\t\t\t\t)\n\t\t\t}\n\n\t\t\treturn func() {}, fmt.Errorf(\"failed to set stdin mode: %w\", err)\n\t\t}\n\t}\n\n\treturn func() {\n\t\terr := winterm.SetConsoleMode(uintptr(stdoutFd), oldOutMode)\n\t\tif err != nil {\n\t\t\tlog.ErrorContext(context.Background(), \"Failed to reset terminal output mode\",\n\t\t\t\t\"original_output_mode\", oldOutMode,\n\t\t\t\t\"error\", err,\n\t\t\t)\n\t\t}\n\n\t\tif input {\n\t\t\terr = winterm.SetConsoleMode(uintptr(stdinFd), oldInMode)\n\t\t\tif err != nil {\n\t\t\t\tlog.ErrorContext(context.Background(), \"Failed to reset terminal input mode\",\n\t\t\t\t\t\"original_input_mode\", oldInMode,\n\t\t\t\t\t\"error\", err,","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/gravitational/teleport/blob/1283425b60ec5f60d509ba4c791183d452923ff7/lib/client/terminal/terminal_windows.go#L70-L106","documentation":"When input capture is requested, initTerminal reconfigures the stdin console mode (raw input, VT input, no echo/line processing). If SetConsoleMode(stdin) fails, the code first attempts to restore the original stdout mode (logging if that restore also fails) and then returns this wrapped error. The terminal session setup is aborted; stdout mode restoration is best-effort.","triggerScenarios":"Calling Terminal.InitRaw(true) on Windows when SetConsoleMode(stdinFd, newInMode) fails: stdin is not a writable-mode console handle, the handle was closed or redirected between GetConsoleMode and SetConsoleMode, or the console host rejects ENABLE_VIRTUAL_TERMINAL_INPUT / ENABLE_EXTENDED_FLAGS combinations.","commonSituations":"Piping or redirecting stdin (e.g. `echo cmd | tsh ...`) while requesting interactive raw input; running under CI/agent harnesses with fake stdin; legacy console hosts that do not support ENABLE_VIRTUAL_TERMINAL_INPUT; a race where another process (e.g. a concurrent session) altered the stdin mode.","solutions":["Ensure stdin is an interactive console before requesting input=true; check term.IsTerminal(os.Stdin.Fd()) first.","Call InitRaw(false) when stdin is redirected — output-only raw mode avoids touching the stdin console mode entirely.","Avoid piping input when an interactive session is expected; use the tool's non-interactive batch mode instead.","Update to a Windows 10 1607+ / Windows Terminal host if the failure is due to unsupported VT input flags."],"exampleFix":"// before\nerr := t.InitRaw(true)\n\n// after\nif term.IsTerminal(os.Stdin.Fd()) {\n    err = t.InitRaw(true)\n} else {\n    err = t.InitRaw(false)\n}\nif err != nil {\n    return trace.Wrap(err)\n}","handlingStrategy":"validation","validationCode":"import \"github.com/moby/term\"\n\nif !term.IsTerminal(os.Stdin.Fd()) {\n    // request output-only raw mode; skips the stdin SetConsoleMode path entirely\n    return t.InitRaw(false)\n}","typeGuard":"func canCaptureRawInput() bool {\n    return term.IsTerminal(os.Stdin.Fd())\n}","tryCatchPattern":"cleanup, err := t.InitRaw(input)\nif err != nil && strings.Contains(err.Error(), \"failed to set stdin mode\") {\n    log.Warn(\"raw stdin capture failed; retrying output-only\")\n    cleanup, err = t.InitRaw(false)\n    if err != nil {\n        return trace.Wrap(err)\n    }\n}","preventionTips":["Only pass input=true when stdin is a genuine interactive console.","Use non-interactive/batch mode when stdin is redirected or piped.","Avoid concurrent sessions mutating console modes of the same handles.","Prefer Windows 10 1607+ hosts that support ENABLE_VIRTUAL_TERMINAL_INPUT."],"tags":["windows","terminal","console-api","stdin","raw-mode"],"backgroundTag":"set-console-mode-failed","analyzedSha":"1283425b60ec5f60d509ba4c791183d452923ff7","analyzedAt":"2026-09-02T04:06:41.601Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}