`UsePipeWriter` throws or completes non-deterministically on cancellation
Closed this issue · 2 comments
Code path 1: If cancellation token is canceled at the beginning of the while loop, the pipe reader is completed without exception.
Code path 2: If cancellation token is canceled while waiting for a read, OperationCanceledException
is thrown and the pipe reader is completed with that exception.
Code trying to write to the pipe writer will then encounter different exceptions when writing to the pipe depending on which code path had been executed. For same scenario, the exception encountered by the code trying to write can be either of two options.
Knowledge gap: I'm learning pipes for the first time, using your code as an example of how to use them. I'm not aware of exactly how the pipe writer would throw, so I might be wrong in my anticipation of exceptions and their content that would be thrown by the pipe writer.
To avoid ambiguous behaviour, in the while condition check, I would use cancellationToken.ThrowIfCancelled()
instead.
PS: Is readResults.ScrubAfterAdvanceTo() still necessary? It looks (to me) like an artifact of old code, I can't see that it does anything useful there.
I appreciate your attention to detail, and you brought up a very real bug in your commit comment elsewhere which I just fixed (#172), so thanks for that.
ScrubAfterAdvanceTo
is not necessary, but it helps avoid accidental code bugs from reusing ReadResult
after AdvanceTo
is called, by clearing members of that struct that must no longer be referenced.