Issue with ProtocolReader.ReadAsync and CancellationTokens
See original GitHub issueI’m writing a server that uses a heartbeat packet. After 10s of inactivity on the socket, the server sends a PING to the client. If using a ProtocolReader, when the PONG comes in, an exception is thrown.
while (true) {
try
{
//Wait for a packet
ProtocolReadResult<ReadOnlyMemory<byte>> result = await
protocolReader.ReadAsync(authProtocol, new CancellationTokenSource(10000).Token);
//Check for FIN
if (result.IsCompleted || result.IsCanceled)
return;
protocolReader.Advance();
}
catch (OperationCanceledException exception)
{
SendPing(connection);
}
}
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'startIndex')
at System.ThrowHelper.ThrowArgumentValidationException[T](ReadOnlySequenceSegment`1 startSegment, Int32 startIndex, ReadOnlySequenceSegment`1 endSegment)
at System.Buffers.ReadOnlySequence`1..ctor(ReadOnlySequenceSegment`1 startSegment, Int32 startIndex, ReadOnlySequenceSegment`1 endSegment, Int32 endIndex)
at System.IO.Pipelines.StreamPipeReader.GetCurrentReadOnlySequence()
at System.IO.Pipelines.StreamPipeReader.ReadAsync(CancellationToken cancellationToken)
at Bedrock.Framework.Protocols.ProtocolReader.ContinueDoAsyncRead[TReadMessage](ValueTask`1 readTask, Nullable`1 maximumMessageSize, IMessageReader`1 reader, CancellationToken cancellationToken)
at Auth.AuthHandler.OnConnectedAsync(ConnectionContext connection) in C:\Users\redacted\AuthHandler.cs:line 44
Line 44 is the protocolReader.ReadAsync instruction exactly as shown above. For testing, I tried sending the PING outside of the exception handler, and was able to receive the PONG with no issues. It seems to be related to the operation cancellation. Also, if I replace the protocol reader with a standard pipe reader, this issue does not occur.
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Why Bedrock ProtocolReader ignores CancellationToken?
It seems that my Custom ProtocolReader ignores CancellationToken ... ReadAsync is checking one time if the token you passed to reader.
Read more >Document that CancellationToken in Stream.ReadAsync ...
If the operation is canceled before it completes, the returned task contains the Canceled value for the Status property. As described in #19867, ......
Read more >SqlDataReader.ReadAsync(CancellationToken) Method
The cancellation token can be used to request that the operation be abandoned before the command timeout elapses. Exceptions will be reported via...
Read more >A Deep Dive into C#'s CancellationToken | by Mitesh Shah
Once the IsCancellationRequested property in a cancellation token is set to true , it can't be set to false again and you cant...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the fast response. Our cancellations are working as expected now. 👍
Thanks for using the library!