runtime icon indicating copy to clipboard operation
runtime copied to clipboard

A zero-length ReadAsync on the serial base stream never completes on Windows.

Open wjohnstonsolventum opened this issue 1 month ago • 1 comments

Description

In WIndows, if you open a SerialPort, obtain BaseStream, and then call ReadAsync with a zero length memory object, the call hangs forever. This works fine on Linux and macOS.

Reproduction Steps

#:property OutputType=Exe
#:property TargetFramework=net10.0
#:property ImplicitUsings=enable
#:property Nullable=enable
#:property SupportedOSPlatformVersion=windows
#:package [email protected]

using System.IO.Ports;

const int WAIT_TIME_SECONDS = 3;

var portName = args.Length > 0 ? args[0] : "COM7";

using var port = new SerialPort(portName, 115200);
port.Open();

// A zero-length ReadAsync on the serial base stream never completes.
var read = port.BaseStream.ReadAsync(Memory<byte>.Empty).AsTask();
var finished = await Task.WhenAny(read, Task.Delay(TimeSpan.FromSeconds(WAIT_TIME_SECONDS)));

Console.WriteLine(finished == read
    ? "completed"
    : $"hung: zero-length ReadAsync did not complete within {WAIT_TIME_SECONDS} s");

// A pending serial read blocks dispose, so exit explicitly.
Environment.Exit(0);

Expected behavior

Should return immediately.

Actual behavior

Hangs until we kill the task.

Regression?

No response

Known Workarounds

No response

Configuration

I've tested this on Windows 11 with .Net 10 and macOS with .Net 10. It looks like this code hasn't changed in .Net 11 or .Net 12, but it's tricky for me to test those versions.

Other information

No response

wjohnstonsolventum avatar Oct 16 '26 18:10 wjohnstonsolventum

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