Search code examples
c#httpwebrequestwebclientparallel.foreach

Inconsistent exceptions when running loop


Recently, I created a Web Request application to retrieve news articles on a website based on a date given. I created a collection of dates i.e. [06-07-2021, 06-08-2021, 06-01-2021, etc.], and have a for loop that runs on the collection and returns the news article for that date. In order to speed up this process, I created a Parallel.ForEach loop that runs on the collection and it stores the news articles in a Dictionary where the key is the date and the value is the article(s). Inside the Parallel loop, I receive a *System.InvalidOperationException when I attempt to add the key-value pair into the Dictionary. It doesn't happen all the time, so I can typically resolve the issue by restarting the program. Another exception I receive is the **System.NullReferenceException which I think is strange because every variable is assigned a value even my methods return a value, albeit an empty string if there is a problem with the request. This exception is also resolved by restarting the application.

My question is why is there an inconsistency in exceptions when running this application?

*Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct.

**Object reference not set to an instance of an object.


Solution

  • When a class which is not documented explicitly to be thread-safe is accessed by multiple threads concurrently, its behavior becomes undefined. Which means that "anything" can happen, and filing a bug report complaining for the undesirable behavior will be ineffective, because whatever happens will not be considered a bug by the manufacturer of the class.

    "Anything" includes not only random noisy exceptions, but also silent data corruption or lost updates.

    You could delve into the source code of the Dictionary<TKey, TValue> class and try to understand the source of the specific exceptions, but it would be an exercise to futility IMHO. The knowledge that you'll get is unlikely to be applicable in any real-life scenario.