tui-cs/Terminal.Gui · error · ApplicationException

Failed to restore input console mode, error code: {GetLastEr

Error message

Failed to restore input console mode, error code: {GetLastError()}.

What it means

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.

Source

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

            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 ();

    [DllImport ("kernel32.dll", SetLastError = true)]
    private static extern nint GetStdHandle (int nStdHandle);

    [DllImport ("kernel32.dll")]

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Use the using/Dispose pattern so Cleanup runs during normal teardown, not during abnormal process exit.
  2. If Cleanup failures are expected in your environment (headless tests), wrap app disposal in try/catch and log the warning.
  3. Avoid forcefully killing the process; use Ctrl+C or the app's quit command.
  4. Ensure no other code modifies stdin console mode concurrently.

Example fix

// before -- abnormal exit leaves console mode unrestored
Environment.Exit(0);

// after -- proper disposal restores console state
using (IApplication app = Application.Create().Init()) { app.Run<MyWindow>(); }
Defensive patterns

Strategy: try-catch

Try / catch

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

Prevention

When it happens

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

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

Related errors


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