question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ReadAsync blocks for .NET Core

See original GitHub issue

Hi,

this is a follow up issue to #114 and to the PR #115.

First of all, thanks for integrating the PR #115 and providing a new NuGet version.

Today, we tried to upgrade the SerialPortsStream library to the new version 2.3.0 in our project and found out that we still have issues with .NET Core 3.1 and using the ReadAsync and WriteAsync methods.

We could reproduce the issue with a pretty straight-forward unit test: We set up writing to the stream on COM1 and reading from COM2 (COM1 and COM2 are connected). For the .NET Core 3.1, the ReadAsync blocks and never reads the bytes from the stream whereas for .NET 4.7.2 everything works as expected.

public async Task WriteAsync()
{
	using (var serialPortStreamWrite = new SerialPortStream("COM1", 9600, 8, Parity.None, StopBits.One))
	using (var serialPortStreamRead = new SerialPortStream("COM2", 9600, 8, Parity.None, StopBits.One))
	{
		serialPortStreamWrite.Open();
		serialPortStreamRead.Open();

		var buffer = new byte[1024];
		var readTask = Task.Run(async () => await serialPortStreamRead.ReadAsync(buffer, 0, buffer.Length));

		var bytes = new byte[] { 0x01, 0x02, 0x03 };
		await serialPortStreamWrite.WriteAsync(bytes, 0, bytes.Length);
		await serialPortStreamWrite.FlushAsync();

		// ReadAsync blocks here even if something gets written and flushed to the underlying COM device.
		// Fails for netcoreapp3.1, works with net472
		await readTask;

		Assert.That(buffer[0], Is.EqualTo(0x01));
		Assert.That(buffer[1], Is.EqualTo(0x02));
		Assert.That(buffer[2], Is.EqualTo(0x03));
	}
}

Since there are no unit tests specific to NETSTANDARD in this repo, I added the unit test in my own test repo: https://github.com/meinsiedler/SerialPortStreamTest

Do you have any idea or hint, what the problem could be and how to fix it?

Best regards

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
jcurlcommented, Apr 19, 2021

OK, I’ll update the versions to 2.3.1 and prepare a new release and close this ticket when it’s in NuGet. Thanks all for your testing.

0reactions
mjhaddencommented, Apr 19, 2021

Hello @jcurl, I also had an issue with no data seen at the ReadAsync. I have tested your attached beta package and also found that it now works as expected. Thank you for the quick turn around.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stream.ReadAsync Method (System.IO)
Asynchronously reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read....
Read more >
c# - FileStream.ReadAsync blocks UI if useAsync is true ...
I would expect that when asynchronous I/O is enabled, the ReadAsync() method would complete asynchronously, allowing the UI thread to update the ...
Read more >
FileStream.ReadAsync Method (System.IO)
The ReadAsync method enables you to perform resource-intensive file operations without blocking the main thread. This performance consideration is particularly ...
Read more >
StreamReader.ReadAsync() does not propagate ...
StreamReader.ReadAsync() indefinitely blocks. Excpected Behavior. OperationCanceledException is thrown. Environment .NET Core SDK 3.1.100- ...
Read more >
proc.StandardOutput.ReadAsync doesn't cancel properly if ...
This method throws on .net core, because Thread. ... and the process still blocks (i.e. the cancellation token never cancel the ReadAsync() ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found