{"record":{"id":"ccb6464e3a6e95c6","repo":"tui-cs/Terminal.Gui","slug":"failed-to-flush-input-queue-error-code-getlaste","errorCode":null,"errorMessage":"Failed to flush input queue, error code: {GetLastError()}.","messagePattern":"Failed to flush input queue, error code: (.+?)\\.","errorType":"exception","errorClass":"ApplicationException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/Drivers/WindowsHelpers/NetWinVTConsole.cs","lineNumber":81,"sourceCode":"\n        _originalOutputConsoleMode = mode;\n\n        if ((mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0)\n        {\n            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 ();","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/Drivers/WindowsHelpers/NetWinVTConsole.cs#L63-L99","documentation":"This error is thrown by NetWinVTConsole.Cleanup() when FlushConsoleInputBuffer fails on the input handle during driver shutdown/teardown. Cleanup is responsible for restoring the console to its pre-initialization state: flushing pending input, then restoring input and output modes. A failure to flush means stale input events could leak into the console after the app exits.","triggerScenarios":"Thrown at NetWinVTConsole.cs:81 when FlushConsoleInputBuffer(_inputHandle) returns false during Cleanup(). This can happen if the input handle has become invalid between init and cleanup (e.g., console was closed, stdin was detached).","commonSituations":"The console window was closed (ctrl+close button) while the app was running, the process is being torn down during a crash, stdin handle was replaced or closed by another thread, or running under a debugger that manipulates console handles.","solutions":["Ensure normal shutdown via app.Dispose() / the using pattern rather than killing the process.","Avoid closing the console window while the TUI app is running.","If the console may be in an unstable state at teardown, wrap Cleanup() in a try/catch to log but not mask the original error.","Check that no other code closes or redirects stdin during the app lifetime."],"exampleFix":"// before -- process killed leaves console in bad state\napp.Run<MyWindow>();\n\n// after -- dispose properly so Cleanup runs in order\nusing (IApplication app = Application.Create().Init()) { app.Run<MyWindow>(); } // Dispose calls Cleanup","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { app.Dispose(); }\ncatch (ApplicationException ex) when (ex.Message.Contains(\"flush input queue\"))\n{ Logging.Warning($\"Console cleanup warning: {ex.Message}\"); }","preventionTips":["Use the using/Dispose pattern for normal teardown.","Avoid closing the console window abruptly while the app runs.","Exit via the app's quit mechanism, not by killing the process.","Wrap disposal in try/catch in environments where console state is not critical."],"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"}