PowerShell
PowerShell copied to clipboard
Troubleshooting random Cannot read keys when either application does not have a console or when console input has been redirected errors
Prerequisites
- [X] Write a descriptive title.
- [X] Make sure you are able to repro it on the latest released version
- [X] Search the existing issues.
- [X] Refer to the FAQ.
- [X] Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
Currently experiencing on PowerShell 7.4.3 on Windows, running inside Windows Terminal, but have also experienced with 7.4.2 and 7.4.1.
It's hard to lock down exactly what causes the issue, but I'm using Windows Terminal (1.20.11381.0) with multiple PowerShell tabs. A lot of the commands I'm running are git or ng or npm related. This is also happening with a co-worker and on a personal machine of mine.
I am importing posh-git (although not on another machine this fails on), Terminal-Icons, and oh-my-posh.
Upon giving the window focus or starting to type, the exception noted in a field below is thrown. I'm unable to proceed with the instance that crashed, but my other tabs and Windows Terminal PowerShell instance are fine.
Expected behavior
PowerShell doesn't randomly crash during the day or there's more information to troubleshoot what's causing the crash.
Actual behavior
When navigating back to a Windows Terminal tab or starting to type in a tab the following will randomly be output:
An error has occurred that was not properly handled. Additional information is shown below. The PowerShell process will exit.
Unhandled exception. System.InvalidOperationException: Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read.
at System.ConsolePal.ReadKey(Boolean intercept)
at Microsoft.PowerShell.Internal.VirtualTerminal._TryIgnoreIOE[T](Func`1 f)
at Microsoft.PowerShell.PSConsoleReadLine.ReadOneOrMoreKeys()
at Microsoft.PowerShell.PSConsoleReadLine.ReadKeyThreadProc()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
Event Viewer is essentially the same:
Application: pwsh.exe
CoreCLR Version: 8.0.624.26715
.NET Version: 8.0.6
Description: The process was terminated due to an unhandled exception.
Exception Info: System.InvalidOperationException: Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read.
at System.ConsolePal.ReadKey(Boolean intercept)
at Microsoft.PowerShell.Internal.VirtualTerminal._TryIgnoreIOE[T](Func`1 f)
at Microsoft.PowerShell.PSConsoleReadLine.ReadOneOrMoreKeys()
at Microsoft.PowerShell.PSConsoleReadLine.ReadKeyThreadProc()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
### Error details
_No response_
### Environment data
```powershell
Name Value
---- -----
PSVersion 7.4.3
PSEdition Core
GitCommitId 7.4.3
OS Microsoft Windows 10.0.19045
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Visuals
- Try to unload oh-my-posh.
- Are there errors in other terminals?
- :( but done. Will see if it triggers a failure in the next few days. Been in use for almost 3 years on work machine, and I don't recall any updates to the module immediately before the crashing started, but ... good call to disable it.
- No. No errors, and other terminals are fine.
oh-my-posh disabled, still happened (about 3 minutes ago according to Event Viewer):
Didn't start typing in the window, machine didn't get locked or have a screensaver kick in. Other tabs are still fine.
I suppose I can run it outside Windows Terminal.
Edit
First time this happened, the process actually exited out. Above had two lines added. Full console message below:
An error has occurred that was not properly handled. Additional information is shown below. The PowerShell process will exit.
Unhandled exception. System.InvalidOperationException: Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read.
at System.ConsolePal.ReadKey(Boolean intercept)
at Microsoft.PowerShell.Internal.VirtualTerminal._TryIgnoreIOE[T](Func`1 f)
at Microsoft.PowerShell.PSConsoleReadLine.ReadOneOrMoreKeys()
at Microsoft.PowerShell.PSConsoleReadLine.ReadKeyThreadProc()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
[process exited with code 3762504530 (0xe0434352)]
You can now close this terminal with Ctrl+D, or press Enter to restart.
At this point too early to tell for sure, but:
On my personal machine I kept the tab open that had crashed (in Windows Terminal) and wasn't experiencing an issue with PowerShell crashing again. Was using the terminal within VS Code as well and it wasn't crashing within there. oh-my-posh was still enabled. Interestingly, it was the first tab in Terminal that crashed. Not 100% that that's been the case for all crashes or not, but definitely was for the majority I can remember.
I tend to use the first tab pretty consistently for git gui and git push commands. Moving my workflow to the second tab might be a good test.
oh-my-posh disabled + running PowerShell outside Windows Terminal = no issues on my work machine today. Will be enabling oh-my-posh and continuing to use PowerShell without Terminal to see if that trend continues.
I was also way slower without multi-tab support, so could be a speed issue. Should be hitting it harder in the next couple of days.
This issue is due to https://github.com/dotnet/runtime/issues/88697. This InvalidOperationException happens when there are multiple processes attached to the same console, both waiting for input, and one dies or is killed.
The _TryIgnoreIOE method you see in the stack trace is already a way to mitigate the issue (see https://github.com/PowerShell/PSReadLine/issues/3744) but when it happens too much in a row, the retries will be exhausted and the exception will be thrown out eventually.
The fix to the .NET issue will be available in .NET 9, but I'm not sure if it's already included in the existing .NET 9 previews.
Changing my workflow to use a combination of Terminal and PowerShell seems to have potentially narrowed it down.
If I only run git gui and the majority of my git commands from a non-Terminal PowerShell instance, I'm not experiencing PowerShell randomly crashing in Terminal.
I'll ... and I just saw you posted a comment as I was writing this. So, I guess I have a workaround.
Which is to say, thanks for determining the root cause. :)
Just in case, this does appear to be linked to using PowerShell inside of Terminal and starting a 'long running' instance of git gui from said Terminal instance.
Just staging commits and committing them, without doing any pushes via the UI, will eventually result in PowerShell crashing.
I'm here because I got the exception this running a CLI app in Linux (WSL), over a local network, where it was OK running locally. I switched to running it in Powershell, and it worked. Good enough for me, without getting to the root of it.
Seeing this once or twice a day. Not necessarily a long-running instance either. Started Terminal today and 10 minutes later it was killed.
Application: pwsh.exe
CoreCLR Version: 9.0.124.61010
.NET Version: 9.0.1
Description: The process was terminated due to an unhandled exception.
Exception Info: System.InvalidOperationException: Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read.
Faulting application name: pwsh.exe, version: 7.5.0.500, time stamp: 0x67580000
Faulting module name: KERNELBASE.dll, version: 10.0.26100.3470, time stamp: 0xcdfc8fa1
Exception code: 0xe0434352
Fault offset: 0x00000000000cab6a
Faulting process id: 0x3698
Faulting application start time: 0x1DB9EA615451BA2
Faulting application path: C:\Program Files\PowerShell\7\pwsh.exe
Faulting module path: C:\WINDOWS\System32\KERNELBASE.dll
Report Id: 0883b5ce-9d7f-4bea-a248-89da19e2f488
Faulting package full name:
Faulting package-relative application ID: