Search code examples
c#.net-coretask-parallel-librarytpl-dataflow

TPL ActionBlock not handling messages after an exception


I've setup an ActionBlock<T> like so:

 private readonly ActionBlock<PresentationListNotification> _actionBlock;
    
    ...
    
  _actionBlock = new ActionBlock<PresentationListNotification>(HandleNotification);

Each time i wish to invoke the actionblock i do so like this:

try
{
    _actionBlock.Post(notification);
}
catch (AggregateException aggException)
{
    foreach (Exception ex in aggException.InnerExceptions)
    {
        _logger.LogError(ex,"Exception {Message} occurred processing notification", ex.Message);
    }
}

This all works fine, but I've noticed that when an exception occurred in the HandleNotification delegate (the actual implementation isn't probably important but it occurred when I was trying the set the result on a TaskCompletionSource which was in a faulted state), the actionblock would fail to post subsequent messages to my handler. In the debugger I could see that the IsCompleted and IsDecliningPermanently properties were True. How do I recover when the actionblock is in this state?


Solution

  • How do I recover when the actionblock is in this state?

    You can't. When a dataflow block has faulted, it will remain in this state forever. There is no recovering after that. You could consider:

    1. Prevent the block from failing, by try-catching any exception thrown inside the action.
    2. After the failure, replace the faulted block with a new one.