-
Notifications
You must be signed in to change notification settings - Fork 8.5k
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
.NET Console.WriteLine throws 'The handle is invalid.' on large output when using ENABLE_VIRTUAL_TERMINAL_PROCESSING #3765
Comments
If I add a |
i've the same issue, but have no idea how to solve it. |
@amd4dnow I have no idea either, but for what it's worth: (A non-recommended way to fix this issue that will cause long-term application stability issues follows.)What I found out is that when using Windows Terminal, Cmder or ConEmu, I can send VT sequences and they get processes even without setting ENABLE_VIRTUAL_TERMINAL_PROCESSING flag. So my current code detects those terminals (so far, reading WT_SESSION or ConEmuANSI environment variables) and only then send VT sequences. |
ConPTY can't pass raw escape sequences in a way that the terminal can differentiate from escape sequences that are in the text buffer, and if they're in the text buffer they're subject to wrapping and splitting. |
Thanks for the information @DHowett-MSFT . Do you have any insight about how to workaround the |
We'll have to have a look-- sorry, things have been slow on account of the impending holidays 😄 |
I am getting the same error when writing to console in an ASP.NET core 3.0 app after a long running process. It appears to be related to high memory usage but not sure. |
Happens to me when I try to interact with console in native DLL: DLL code init:
DLL code shutdown:
|
For your C # application to work properly, you need to make sure that no one else touches the console. There "somewhere else" you can use GetConsoleWindow function. |
@gerardog this should definitely work. Setting the console output handle (retrieved from You may need to fiddle with the I just got your reproducer under a debugger, and the strangest thing is happening.
It looks like the SafeHandle that you used to set the output mode is being finalized when its scope ends on line 40. It's closing the only output handle when it does so. If you replace the use of Chalking this one up to user error :) |
Many thanks @DHowett-MSFT. You really nailed this one! |
Environment
Steps to reproduce
Create a simple C# Process that set console mode ENABLE_VIRTUAL_TERMINAL_PROCESSING, and then writes unlimited output to StdOut.
Eventually StdOut gets broken and throws System.IO.IOException: The handle is invalid.
C# Repro code:
Expected behavior
The app should not throw IOException.
Actual behavior
This is the output:
The exception is only thrown with ENABLE_VIRTUAL_TERMINAL_PROCESSING console mode.
Either launching the app from a Windows CMD/ConHost, or the new Windows Terminal/PowerShell.
Thanks!
The text was updated successfully, but these errors were encountered: