Question

I use a BroadcastBlock in TPL Dataflow which links to ActionBlocks. However, I cannot find a bug in my code. I post items to the BroadcastBlock via SendAsync and when I print the items in the ActionBlocks I find that some are missing and mysteriously others are duplicates. I keep track of the order of processed items and thus found that just at the occasion when an item is missing the previous or subsequent items is duplicated (fed to the same Actionblock via LinkTo). I have no idea what is going on. Here are couple of my code snippets that are relevant:

Here the instantiation of the BroadCastBlock:

buffer = new BroadcastBlock<Quote>(quote => quote);

Here's how I link to action blocks:

//Filter for incoming quotes
Predicate<Quote> quoteBroadCastFilter = new Predicate<Quote>(newQuote =>
{
    if (symbol.symbolID == newQuote.symbol.symbolID)
    {
        return true;
    }
    else
    {
        return false;
    }
});

//Link Strategy up to BroadCastBlocks
buffer.LinkTo<Quote>(newStrategy.GetStrategyQuoteBuffer(), new DataflowLinkOptions { PropagateCompletion = true }, quoteBroadCastFilter);

This is how I submit items (quote) to the BroadCastBlock:

buffer.SendAsync(quote);

The problem is that when I print out tracking info right before sending via SendAsync everything looks perfectly fine. When I print the same information in the ActionBlock that BroadCastBlock links I observe infrequent errors. I noticed that the missing/duplicate items occur when items are fed to the BroadCastBlock very quickly (via EventHandler of an API)

Any idea what I am doing wrong?

Was it helpful?

Solution

I answered my own question as I found the issue:

Before the item is sent to BroadCastBlock, the item (of ref type) is read from a concurrent collection, updated, and then sent. So, it happened that sometimes the next item was already read from the collection and updated while the previous item still points to the same object which is now in the process of being updated. This in short caused the problems with items that were already in the BroadCastBlock. There was no problem post BroadCastBlock clone but before with items in the input queue.

I had to perform a deep copy of the Quote object before sending it via Post or SendAsync to the broad cast block.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top