{"record":{"id":"5cbb1aa11d4fbb2e","repo":"tui-cs/Terminal.Gui","slug":"failed-to-restore-input-console-mode-error-code","errorCode":null,"errorMessage":"Failed to restore input console mode, error code: {GetLastError()}.","messagePattern":"Failed to restore input console mode, error code: (.+?)\\.","errorType":"exception","errorClass":"ApplicationException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/Drivers/WindowsHelpers/NetWinVTConsole.cs","lineNumber":86,"sourceCode":"            mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;\n\n            if (!SetConsoleMode (_outputHandle, mode))\n            {\n                throw new ApplicationException ($\"Failed to set output console mode, error code: {GetLastError ()}.\");\n            }\n        }\n    }\n\n    public void Cleanup ()\n    {\n        if (!FlushConsoleInputBuffer (_inputHandle))\n        {\n            throw new ApplicationException ($\"Failed to flush input queue, error code: {GetLastError ()}.\");\n        }\n\n        if (!SetConsoleMode (_inputHandle, _originalInputConsoleMode))\n        {\n            throw new ApplicationException ($\"Failed to restore input console mode, error code: {GetLastError ()}.\");\n        }\n\n        if (!SetConsoleMode (_outputHandle, _originalOutputConsoleMode))\n        {\n            throw new ApplicationException ($\"Failed to restore output console mode, error code: {GetLastError ()}.\");\n        }\n    }\n\n    [DllImport (\"kernel32.dll\")]\n    private static extern bool GetConsoleMode (nint hConsoleHandle, out uint lpMode);\n\n    [DllImport (\"kernel32.dll\")]\n    private static extern uint GetLastError ();\n\n    [DllImport (\"kernel32.dll\", SetLastError = true)]\n    private static extern nint GetStdHandle (int nStdHandle);\n\n    [DllImport (\"kernel32.dll\")]","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/Drivers/WindowsHelpers/NetWinVTConsole.cs#L68-L104","documentation":"This error is thrown by NetWinVTConsole.Cleanup() when SetConsoleMode fails while restoring the original input console mode during teardown. After flushing input, Cleanup attempts to set the input handle back to _originalInputConsoleMode. A failure here means the console input is left with VT input enabled after the app exits, potentially affecting subsequent console applications.","triggerScenarios":"Thrown at NetWinVTConsole.cs:86 when SetConsoleMode(_inputHandle, _originalInputConsoleMode) returns false during Cleanup(). The input handle may have become invalid, or another process/thread may have taken over the console.","commonSituations":"Console window closed mid-session, process termination during exception handling, stdin handle invalidated by parent process changes, or concurrent console access by debugger/IDE output redirection.","solutions":["Use the using/Dispose pattern so Cleanup runs during normal teardown, not during abnormal process exit.","If Cleanup failures are expected in your environment (headless tests), wrap app disposal in try/catch and log the warning.","Avoid forcefully killing the process; use Ctrl+C or the app's quit command.","Ensure no other code modifies stdin console mode concurrently."],"exampleFix":"// before -- abnormal exit leaves console mode unrestored\nEnvironment.Exit(0);\n\n// after -- proper disposal restores console state\nusing (IApplication app = Application.Create().Init()) { app.Run<MyWindow>(); }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { app.Dispose(); }\ncatch (ApplicationException ex) when (ex.Message.Contains(\"restore input console mode\"))\n{ Logging.Warning($\"Console cleanup warning: {ex.Message}\"); }","preventionTips":["Use using/Dispose for proper teardown sequence.","Exit via the app's quit mechanism rather than Environment.Exit or process kill.","Ensure no other code modifies the stdin console mode concurrently.","In headless tests, wrap disposal in try/catch to avoid masking test failures."],"tags":["windows","console","driver","win32","cleanup","teardown"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}