{"record":{"id":"fe6fb5d0b46f9ad6","repo":"git-ecosystem/git-credential-manager","slug":"failed-to-read-initial-console-mode-on-consoleinn","errorCode":null,"errorMessage":"Failed to read initial console mode on {ConsoleInName}.","messagePattern":"Failed to read initial console mode on (.+?)\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/Core/Interop/Windows/WindowsAnsiConsoleInput.cs","lineNumber":85,"sourceCode":"        _handle = Kernel32.CreateFile(\n            fileName: ConsoleInName,\n            desiredAccess: NativeFileAccess.GenericRead | NativeFileAccess.GenericWrite,\n            shareMode: NativeFileShare.Read | NativeFileShare.Write,\n            securityAttributes: IntPtr.Zero,\n            creationDisposition: FileCreationDisposition.OpenExisting,\n            flagsAndAttributes: NativeFileAttributes.Normal,\n            templateFile: IntPtr.Zero);\n\n        if (_handle.IsInvalid)\n        {\n            _handle.Dispose();\n            throw new IOException($\"Failed to open {ConsoleInName} for reading.\");\n        }\n\n        if (!Kernel32.GetConsoleMode(_handle, out _originalMode))\n        {\n            _handle.Dispose();\n            throw new IOException($\"Failed to read initial console mode on {ConsoleInName}.\");\n        }\n\n        // The console is left in its normal (cooked) mode except during an\n        // active keystroke read (see ReadKey). That is what keeps Ctrl+C working\n        // when GCM is doing other work — MSAL polling, a GUI, the network — and\n        // not just while a Spectre prompt happens to be reading: with\n        // ProcessedInput on, Ctrl+C raises CTRL_C_EVENT and the runtime's\n        // default handler terminates the process.\n    }\n\n    // Switch the console into raw mode (no echo, no line input, no system Ctrl+C\n    // handling) for the duration of a read. Caller must hold _modeLock.\n    private void EnterRawMode()\n    {\n        ConsoleMode rawMode = _originalMode\n            & ~(ConsoleMode.LineInput | ConsoleMode.EchoInput | ConsoleMode.ProcessedInput);\n\n        if (!Kernel32.SetConsoleMode(_handle, rawMode))","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/git-ecosystem/git-credential-manager/blob/e8ce762cd04b4100ae637b5fbf39ef9d0a96561e/src/Core/Interop/Windows/WindowsAnsiConsoleInput.cs#L67-L103","documentation":"After opening CONIN$, WindowsAnsiConsoleInput calls Kernel32.GetConsoleMode to capture the original (cooked) console mode so it can be restored later. If GetConsoleMode fails, the handle does not refer to a console, so the constructor throws IOException to avoid entering/exiting raw mode on a non-console device.","triggerScenarios":"The CONIN$ handle opened successfully but GetConsoleMode returned false — input is redirected to a file/pipe rather than an actual console buffer, or the handle was closed concurrently.","commonSituations":"Running git with stdin redirected from a file or pipe (e.g. `git fetch < input.txt`, piping into git) while GCM tries to prompt for input; running under wrappers that fake console handles; unusual terminal emulators (e.g. mintty without winpty) providing pipes instead of console handles.","solutions":["Run git with a genuine console stdin (don't redirect stdin when interactive auth may occur).","Under mintty/MSYS shells, use winpty (winpty git push) so a real Windows console is provided.","Set GCM_INTERACTIVE=never or pre-provision credentials (wincredman store, PAT in config) so no console input is attempted.","Pre-cache credentials with an interactive run once, then use non-interactive runs in scripts/pipes."],"exampleFix":"// before\ntype input.txt | git push   // stdin is a pipe -> GetConsoleMode fails\n// after\nwinpty git push             # or: git config credential.credentialStore wincredman","handlingStrategy":"validation","validationCode":"if (!Kernel32.GetConsoleMode(consoleIn, out _))\n    throw new InvalidOperationException(\"stdin is not a real console; interactive prompt unavailable.\");","typeGuard":"static bool IsConsoleInput(IntPtr handle) =>\n    Kernel32.GetConsoleMode(handle, out _); // pipes/files have no console mode","tryCatchPattern":"try { using var input = new WindowsAnsiConsoleInput(); ... }\ncatch (IOException ex) when (ex.Message.Contains(\"initial console mode\"))\n{ // stdin redirected: fall back to winpty or non-interactive store\n  return NonInteractiveFallback(); }","preventionTips":["Avoid piping/redirecting stdin into git commands that may prompt for credentials.","Under mintty/MSYS, prefix with winpty to get a real console.","Pre-cache credentials so prompts never occur in scripts.","Set GCM_INTERACTIVE=never in automation."],"tags":["windows","console","io","raw-mode"],"backgroundTag":"file-open-failed","analyzedSha":"e8ce762cd04b4100ae637b5fbf39ef9d0a96561e","analyzedAt":"2026-09-11T17:15:08.753Z","contentChangedAt":"2026-09-11T17:15:08.753Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}