tui-cs/Terminal.Gui · error · ApplicationException

Failed to flush input queue, error code: {GetLastError()}.

Error message

Failed to flush input queue, error code: {GetLastError()}.

What it means

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.

Source

Thrown at Terminal.Gui/Drivers/WindowsHelpers/NetWinVTConsole.cs:81

        _originalOutputConsoleMode = mode;

        if ((mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0)
        {
            mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;

            if (!SetConsoleMode (_outputHandle, mode))
            {
                throw new ApplicationException ($"Failed to set output console mode, error code: {GetLastError ()}.");
            }
        }
    }

    public void Cleanup ()
    {
        if (!FlushConsoleInputBuffer (_inputHandle))
        {
            throw new ApplicationException ($"Failed to flush input queue, error code: {GetLastError ()}.");
        }

        if (!SetConsoleMode (_inputHandle, _originalInputConsoleMode))
        {
            throw new ApplicationException ($"Failed to restore input console mode, error code: {GetLastError ()}.");
        }

        if (!SetConsoleMode (_outputHandle, _originalOutputConsoleMode))
        {
            throw new ApplicationException ($"Failed to restore output console mode, error code: {GetLastError ()}.");
        }
    }

    [DllImport ("kernel32.dll")]
    private static extern bool GetConsoleMode (nint hConsoleHandle, out uint lpMode);

    [DllImport ("kernel32.dll")]
    private static extern uint GetLastError ();

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Ensure normal shutdown via app.Dispose() / the using pattern rather than killing the process.
  2. Avoid closing the console window while the TUI app is running.
  3. 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.
  4. Check that no other code closes or redirects stdin during the app lifetime.

Example fix

// before -- process killed leaves console in bad state
app.Run<MyWindow>();

// after -- dispose properly so Cleanup runs in order
using (IApplication app = Application.Create().Init()) { app.Run<MyWindow>(); } // Dispose calls Cleanup
Defensive patterns

Strategy: try-catch

Try / catch

try { app.Dispose(); }
catch (ApplicationException ex) when (ex.Message.Contains("flush input queue"))
{ Logging.Warning($"Console cleanup warning: {ex.Message}"); }

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of tui-cs/Terminal.Gui@2e47b11478 (2026-08-13). Data as JSON: /api/errors/ccb6464e3a6e95c6. Report an issue: GitHub.