Closed Feroks closed 4 years ago
This is a great question by which the subtleties of I have struggled greatly!
Keep in mind that with your code, await Finish()
is called for every entry until there if a fault.
There are some tests to prove the policy, but basically:
DoA()
will continue to fill up the channel that DoB()
is reading from and is unaware of any downstream faults.DoB()
throws for any reason, the underlying read marks complete the subsequent writer with the exception thrown. https://github.com/Open-NET-Libraries/Open.ChannelExtensions/blob/master/Open.ChannelExtensions/Extensions.Pipe.cs#L105So to summarize:
.Pipe
calls on a single reader.Am i supposed to try/catch all exceptions in Pipe methods and then return some faulty code that is handled by Filter method? It depends.
The only real consequence is that the most upstream channel will continue iterating without being notified of a problem. This would also be true for Dataflow blocks.
Here's a pattern you could and probably should use in a complex situation like the one you provided:
using var ts = new CancellationTokenSource();
try
{
await .....
.ToChannel()
.PipeAsync(async x =>
{
return await DoA();
}, cancellationToken: ts.Token)
.PipeAsync(async x =>
{
return await DoB();
}, cancellationToken: ts.Token)
.ReadAllAsync(x =>
{
await Finish();
});
}
catch
{
ts.Cancel(); // Any fault will halt (cancel) the above pipe operations.
throw;
}
That way any downstream fault signals that the upstream reads/transforms are simply wasting processes.
@Feroks lemme know if this answers your question or if you need more info. 😄
@electricessence Thank you very much for detailed answer. It covers all my questions 🙂
I would like to clarify if following behaviour is by design or not. Imagine that channel is created from
AsyncEnumerable
. IfDoB()
throws an exception, then all next produced values will only be handled inPipeAsync
that callsDoA()
. They will never reachPipeAsync
withDoB()
orReadAllAsync()
Am i supposed to
try/catch
all exceptions inPipe
methods and then return some faulty code that is handled byFilter
method?