{"record":{"id":"3ae5518c8c03bbf2","repo":"git-ecosystem/git-credential-manager","slug":"failed-to-open-consoleinname-for-reading","errorCode":null,"errorMessage":"Failed to open {ConsoleInName} for reading.","messagePattern":"Failed to open (.+?) for reading\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/Core/Interop/Windows/WindowsAnsiConsoleInput.cs","lineNumber":79,"sourceCode":"    private int _rawDepth;\n\n    public WindowsAnsiConsoleInput()\n    {\n        PlatformUtils.EnsureWindows();\n\n        _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.","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/git-ecosystem/git-credential-manager/blob/e8ce762cd04b4100ae637b5fbf39ef9d0a96561e/src/Core/Interop/Windows/WindowsAnsiConsoleInput.cs#L61-L97","documentation":"WindowsAnsiConsoleInput's constructor opens the console input device (CONIN$) via CreateFile to read keystrokes, then reads the initial console mode. If the returned handle is invalid it disposes it and throws IOException, failing fast instead of reading from a broken handle.","triggerScenarios":"Creating WindowsAnsiConsoleInput when CreateFile on CONIN$ fails: no console attached to the process (windows/gui subsystem app, detached service, redirected standard handles without a console).","commonSituations":"git invoked from a GUI launcher or scheduled task with no console; running under a service account with no interactive session; a terminal whose input handle was closed or redirected (e.g. `git push < NUL` in some hosts).","solutions":["Run git from a real console (Windows Terminal, conhost) so CONIN$ is available.","Use a non-interactive configuration: set GCM_INTERACTIVE=never or choose the Windows credential store (credential.credentialStore=wincredman) so no console input is required.","If launching from a GUI/scheduler, attach or allocate a console (AttachConsole/AllocConsole) or avoid interactive auth by pre-caching credentials.","Check that the host application isn't stripping standard handles before spawning git."],"exampleFix":"// before (scheduled task, no console)\nschtasks /run /tn gitbackup   // IOException: failed to open CONIN$\n// after\nschtasks options -> run only when user is logged on, or set:\nset GCM_INTERACTIVE=never && git config credential.credentialStore wincredman","handlingStrategy":"fallback","validationCode":"var h = Kernel32.CreateFile(\"CONIN$\", ..., ...);\nbool usable = !h.IsInvalid && Kernel32.GetConsoleMode(h, out _);","typeGuard":"static bool HasConsole() =>\n    Kernel32.GetConsoleWindow() != IntPtr.Zero &&\n    Kernel32.GetConsoleMode(Kernel32.CreateFile(\"CONIN$\", 0x80000000, 0, IntPtr.Zero, 3, 0, IntPtr.Zero), out _);","tryCatchPattern":"try { using var input = new WindowsAnsiConsoleInput(); ... }\ncatch (IOException)\n{ // no console: switch to non-interactive credential store\n  cred = winCredManStore.Get(target); }","preventionTips":["Don't redirect/close stdin when an interactive git helper may prompt.","Prefer wincredman credential store for services and scheduled tasks.","For GUI-launched git, attach a console or configure non-interactive auth.","Test scheduled-task scenarios with GCM_INTERACTIVE=never before deploying."],"tags":["windows","console","io","terminal"],"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"}