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.

Issue with ProtocolReader.ReadAsync and CancellationTokens

See original GitHub issue

I’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:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Aengecommented, Apr 18, 2021

Thanks for the fast response. Our cancellations are working as expected now. 👍

0reactions
davidfowlcommented, Apr 18, 2021

Thanks for using the library!

Read more comments on GitHub >

github_iconTop 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 >

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