{"record":{"id":"df9de7ce9dc8acc3","repo":"tui-cs/Terminal.Gui","slug":"failed-to-restore-output-console-mode-error-code","errorCode":null,"errorMessage":"Failed to restore output console mode, error code: {GetLastError()}.","messagePattern":"Failed to restore output console mode, error code: (.+?)\\.","errorType":"exception","errorClass":"ApplicationException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/Drivers/WindowsHelpers/NetWinVTConsole.cs","lineNumber":91,"sourceCode":"            }\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\")]\n    private static extern bool SetConsoleMode (nint hConsoleHandle, uint dwMode);\n\n    [DllImport (\"kernel32.dll\", SetLastError = true)]\n    private static extern bool FlushConsoleInputBuffer (nint hConsoleInput);\n}","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/Drivers/WindowsHelpers/NetWinVTConsole.cs#L73-L109","documentation":"This error is thrown by NetWinVTConsole.Cleanup() when SetConsoleMode fails while restoring the original output console mode during teardown. This is the last step of Cleanup -- after flushing input and restoring input mode -- to return ENABLE_VIRTUAL_TERMINAL_PROCESSING to its original state. Failure leaves the console with VT processing enabled after exit.","triggerScenarios":"Thrown at NetWinVTConsole.cs:91 when SetConsoleMode(_outputHandle, _originalOutputConsoleMode) returns false during Cleanup(). The output handle has become invalid or the console was detached.","commonSituations":"Console window closed while the app ran, stdout redirected or closed before teardown, process crash during shutdown, or IDE/debugger altering the output handle.","solutions":["Use using/Dispose for normal teardown so Cleanup runs in the correct sequence.","Avoid closing the console window abruptly; exit via the app's quit mechanism.","Guard disposal in a try/catch in environments where console state is not critical (headless tests) to avoid masking errors.","Ensure stdout is not redirected or closed during the session."],"exampleFix":"// before -- crash leaves VT output enabled\napp.Run<MyWindow>();\n// process exits without cleanup\n\n// after -- Dispose restores the original output mode\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 output console mode\"))\n{ Logging.Warning($\"Console cleanup warning: {ex.Message}\"); }","preventionTips":["Use using/Dispose for proper teardown.","Avoid closing the console window or redirecting stdout during the session.","Exit via the app's quit command.","Wrap disposal in try/catch for headless test environments."],"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"}