{"record":{"id":"f9206751656d5223","repo":"wezterm/wezterm","slug":"setconsolemode-failed","errorCode":null,"errorMessage":"SetConsoleMode failed: {}","messagePattern":"SetConsoleMode failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"termwiz/src/terminal/windows.rs","lineNumber":92,"sourceCode":"        dy: i16,\n        attr: u16,\n    ) -> Result<()>;\n}\n\nstruct InputHandle {\n    handle: FileDescriptor,\n}\n\nimpl Read for InputHandle {\n    fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {\n        self.handle.read(buf)\n    }\n}\n\nimpl ConsoleInputHandle for InputHandle {\n    fn set_input_mode(&mut self, mode: u32) -> Result<()> {\n        if unsafe { SetConsoleMode(self.handle.as_raw_handle(), mode) } == 0 {\n            bail!(\"SetConsoleMode failed: {}\", IoError::last_os_error());\n        }\n        Ok(())\n    }\n\n    fn get_input_mode(&mut self) -> Result<u32> {\n        let mut mode = 0;\n        if unsafe { GetConsoleMode(self.handle.as_raw_handle(), &mut mode) } == 0 {\n            bail!(\"GetConsoleMode failed: {}\", IoError::last_os_error());\n        }\n        Ok(mode)\n    }\n\n    fn set_input_cp(&mut self, cp: u32) -> Result<()> {\n        if unsafe { SetConsoleCP(cp) } == 0 {\n            bail!(\"SetConsoleCP failed: {}\", IoError::last_os_error());\n        }\n        Ok(())\n    }","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/wezterm/wezterm/blob/b99b1ca2cc0a77e97600410726cdf5e3750bd89b/termwiz/src/terminal/windows.rs#L74-L110","documentation":"On Windows, InputHandle::set_input_mode calls SetConsoleMode on the console input handle to install the mode flags termwiz needs (raw input, virtual terminal input, etc.). The API returns 0 on failure and the code bails with GetLastError(). It fails when the handle is not a console input handle, is closed, or the mode bits are invalid for that handle type.","triggerScenarios":"Entering raw mode / setting input modes when stdin is redirected (pipe or file instead of the console), when the console handle was closed, when the process has no attached console (some service/session setups), or when invalid mode flag combinations are passed.","commonSituations":"Running a termwiz/wezterm-based TUI with stdin redirected (`app < file`), under CI on Windows without a console, detached services, or legacy consoles where VT input flags (ENABLE_VIRTUAL_TERMINAL_INPUT) are unsupported.","solutions":["Run with stdin attached to a real console (interactive cmd/PowerShell/Windows Terminal)","Verify the handle is a console before entering raw mode: call GetConsoleMode first; if it fails, redirect scenarios must be handled separately","On Windows 10+ ensure the console supports VT; use Windows Terminal or ConPTY rather than legacy conhost","When stdin is not a console, use a pty layer (ConPTY / portable-pty) instead of the console APIs"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// probe whether stdin is a console before touching console modes\nlet mut mode = 0u32;\nlet is_console = unsafe {\n    consoleapi::GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE) as *mut _, &mut mode)\n} != 0;\nif !is_console { /* use non-console input path */ }","typeGuard":"fn is_console_handle(handle: HANDLE) -> bool {\n    let mut mode = 0u32;\n    unsafe { consoleapi::GetConsoleMode(handle as *mut _, &mut mode) } != 0\n}","tryCatchPattern":"if let Err(e) = input.set_input_mode(mode) {\n    if !is_console_handle(handle) {\n        // stdin is redirected: fall back to plain stream reading\n        return setup_non_console_input();\n    }\n    return Err(e);\n}","preventionTips":["Probe console availability once at startup and branch to a non-console code path immediately","Keep stdin unredirected for interactive builds; offer a --plain mode for piped runs","Require Windows 10+ / Windows Terminal for VT-input-dependent features"],"tags":["termwiz","windows","win32","console","setconsolemode","raw-mode"],"backgroundTag":"win32-console-api-failed","analyzedSha":"b99b1ca2cc0a77e97600410726cdf5e3750bd89b","analyzedAt":"2026-09-05T19:07:56.543Z","contentChangedAt":"2026-09-05T19:07:56.543Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}