dotnet/Nerdbank.Streams

Trying to call ReadAsync twice on an SSL Stream PipeReader throws an Exception

thomhurst opened this issue · 4 comments

I'm using the Extension: SSLStream.UsePipeReader();

When calling ReadAsync the first time is successful, however on the second call this exception is thrown:

System.NotSupportedException : The ReadAsync method cannot be called when another read operation is pending. at System.Net.Security.SslStreamInternal.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory1 buffer)
at Nerdbank.Streams.PipeExtensions.<>c__DisplayClass5_0.<b__1>d.MoveNext() in D:\a\1\s\src\Nerdbank.Streams\PipeExtensions.cs:line 92
--- End of stack trace from previous location where exception was thrown ---
at System.IO.Pipelines.PipeCompletion.ThrowLatchedException()
at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result)
at System.IO.Pipelines.Pipe.GetReadAsyncResult()`

Switching to System.Pipelines.Unofficial implementation StreamConnection.GetDuplex(SSLStream) I was able to call ReadAsync the second time successfully.

Are you calling AdvanceTo after the first ReadAsync call?

Can you send me a snippet of your code that throws the error? Preferably everything from when you call UsePipeReader to where it throws. And I hope I can assume you don't have any other code that's trying to read from the stream simultaneously.
You could try UseStrictPipeReader which turns every PipeReader call directly to a Stream.ReadyAsync call. The regular UsePipeReader has a dedicated async state machine that continually reads from the stream. I would not expect either to malfunction so long as you're not trying to read from the stream any more after attaching the pipe reader, and that you're not reading from the pipe reader without calling AdvanceTo after each time.

Closing due to no response.