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

Async Console Read Cannot Be Cancelled #100308

Open
grochoge opened this issue Mar 26, 2024 · 5 comments
Open

Async Console Read Cannot Be Cancelled #100308

grochoge opened this issue Mar 26, 2024 · 5 comments
Labels
area-System.Console blocked Issue/PR is blocked on something - see comments
Milestone

Comments

@grochoge
Copy link

grochoge commented Mar 26, 2024

Description

After beginning an asynchronous stream read of standard input (on Windows at least) with a cancellation token, cancelling the token does not cancel the read.

Reproduction Steps

using System.Threading;
var cancel = new CancellationTokenSource();
var stdin = Console.OpenStandardInput();
var buffer = new byte[1024];
var read = stdin.ReadAsync( buffer, cancel.Token );
cancel.Cancel();
await read;

Expected behavior

Read is cancelled and a cancellation exception is thrown.

Actual behavior

The await call blocks forever.

Regression?

No response

Known Workarounds

No response

Configuration

.NET 8.0.3
Windows 10 19045.4046

Other information

A stack trace shows the program calling the regular console read method, not an asynchronous version:

 	System.Console.dll!Interop.Kernel32.ReadFile(nint handle, byte* bytes, int numBytesToRead, out int numBytesRead, nint mustBeZero) Line 412	C#
 	System.Console.dll!System.ConsolePal.WindowsConsoleStream.ReadFileNative(nint hFile, System.Span<byte> buffer, bool isPipe, out int bytesRead, bool useFileAPIs) Line 1150	C#
 	System.Console.dll!System.ConsolePal.WindowsConsoleStream.Read(System.Span<byte> buffer) Line 1108	C#
>	System.Console.dll!System.IO.ConsoleStream.Read(byte[] buffer, int offset, int count) Line 34	C#
 	[Async] Program.Main(string[] aArgs) Line 73	C#
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-console
See info in area-owners.md if you want to be subscribed.

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Mar 26, 2024
@grochoge
Copy link
Author

Searching further, I guess this is more of a feature request and/or a documentation bug/feature request.

The documentation states ReadAsync "monitors cancellation requests", implying that the read can be cancelled while in progress. However the actual (intended) behavior is the cancellation token is checked before reading but ReadAsync will not be cancelled after that unless the behavior is overridden in a derived class.

@reflectronic
Copy link
Contributor

Console I/O cannot be canceled reliably on Windows: microsoft/terminal#12143

@alexrp
Copy link
Contributor

alexrp commented Apr 30, 2024

Console I/O cannot be canceled reliably on Windows: microsoft/terminal#12143

I would very much appreciate upvotes on that issue by anyone who cares about it, by the way. It's hard to convince the team to dedicate time to it if I seem like the only person affected by it.

@adamsitnik adamsitnik added blocked Issue/PR is blocked on something - see comments and removed untriaged New issue has not been triaged by the area owner labels Jun 25, 2024
@adamsitnik adamsitnik added this to the Future milestone Jun 25, 2024
@adamsitnik
Copy link
Member

Console I/O cannot be canceled reliably on Windows: microsoft/terminal#12143

I've marked it as blocking, as soon as Windows makes it possible we can give it a try (currently the handle is open for sync IO)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Console blocked Issue/PR is blocked on something - see comments
Projects
None yet
Development

No branches or pull requests

4 participants