Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Troubleshooting random Cannot read keys when either application does not have a console or when console input has been redirected errors #23979

Open
5 tasks done
JamesSkemp opened this issue Jun 21, 2024 · 4 comments
Labels
Needs-Triage The issue is new and needs to be triaged by a work group.

Comments

@JamesSkemp
Copy link

Prerequisites

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

image

@JamesSkemp JamesSkemp added the Needs-Triage The issue is new and needs to be triaged by a work group. label Jun 21, 2024
@237dmitry
Copy link

  1. Try to unload oh-my-posh.
  2. Are there errors in other terminals?

@JamesSkemp
Copy link
Author

  1. :( 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.
  2. No. No errors, and other terminals are fine.

@JamesSkemp
Copy link
Author

JamesSkemp commented Jun 21, 2024

oh-my-posh disabled, still happened (about 3 minutes ago according to Event Viewer):

image

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.

@JamesSkemp
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs-Triage The issue is new and needs to be triaged by a work group.
Projects
None yet
Development

No branches or pull requests

2 participants