-2

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.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • Please be careful not to misuse tags. Most tags have descriptions that you can read by hovering over them, and the descriptions usually outline cases where you should or shouldn't use the tags. For example, the `[visual-studio]` question should only be used for questions about the Visual Studio application, as opposed to code authored within the VS environment. I've removed that tag for you. – ProgrammingLlama Jun 10 '21 at 01:50
  • _"when I attempt to add the key-value pair into the Dictionary"_ - if it's a standard dictionary then see [this answer](https://stackoverflow.com/a/42959321/3181933) on the question I've marked yours a duplicate of. If that's the case, you probably want a `ConcurrentDictionary` instead. – ProgrammingLlama Jun 10 '21 at 01:52
  • Thank you @Llama, I believe the Concurrent Dictionary would fix the problem, but that doesn't necessarily answer my question. I want to know why does the Dictionary throw these exceptions as inconsistently as it does. – Reginald Brown Jun 10 '21 at 01:57
  • I'm not sure why my question would be closed as it's not a duplicate or downvoted for that matter. With all due respect, it almost seems like you didn't comprehend what the question was asking @Llama – Reginald Brown Jun 10 '21 at 01:59
  • I didn't downvote your question, and I did understand your question. You want to know why it throws this exception, right? It's because Dictionary isn't thread safe, as the linked answer explains. Look at the stack trace you get with the error and you will see that the exception is coming from _inside_ the dictionary. – ProgrammingLlama Jun 10 '21 at 02:14
  • And, with all due respect, you didn't provide any code here. Even if I hadn't closed it as a duplicate of _that question_, your question would have been closed for lacking debugging details because you've provided no [mcve]. – ProgrammingLlama Jun 10 '21 at 02:15
  • 1
    Here's a better question to mark yours as a duplicate of then: https://stackoverflow.com/questions/1320621/throw-a-nullreferenceexception-while-calling-the-set-item-method-of-a-dictionary – ProgrammingLlama Jun 10 '21 at 02:17
  • 1
    Thank you @Llama, again. I wasn't saying that you downvoted or closed the question, so my apologies If that is how I came across. I think the question provided would be better suited as the duplicate. – Reginald Brown Jun 10 '21 at 02:27
  • OK, sorry about misunderstanding that. – ProgrammingLlama Jun 10 '21 at 02:30

1 Answers1

0

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.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104