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
- 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.
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
- 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.
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
- Failed to flush input queue, error code: {GetLastError()}.
- Failed to restore output console mode, error code: {GetLastE
- Failed to get screenBuffer console mode, error code: {Marsha
- Failed to set screenBuffer console mode, error code: {Marsha
- Failed to get input console mode, error code: {GetLastError(
AI-assisted analysis of tui-cs/Terminal.Gui@2e47b11478 (2026-08-13).
Data as JSON: /api/errors/5cbb1aa11d4fbb2e.
Report an issue: GitHub.