When does a dictionary throw an IndexOutOfRangeException on Add or ContainsKey?

折月煮酒 提交于 2020-01-21 02:34:49

问题


On a busy ASP .NET website, I have a Dictionary, which acts as a cache, basically storing key/value pairs for later retrieval.

On high load, the Dictionary some times get into a state, where it always throws an IndexOutOfRangeException whenever i call the ContainsKey or Add method. The exception happens inside the private FindEntry method.

I am suspecting that this might be due to a synchronization issue, but I am not sure.

Can anyone tell me under which circumstances this can happen ? My goal is to gather enough information so that I can reproduce the issue in the dev environment.


回答1:


The documentation for Dictionary states:

Any instance members are not guaranteed to be thread safe.

To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

If you are not synchronizing access to the Dictionary, then you might get issues like those you describe (presumably because the internal state is no longer valid). If you want to try and reproduce this in your development environment, then try creating a program that uses multiple threads to continuously read and write from a Dictionary without synchronization.




回答2:


I agree that this is almost certainly a synchronisation issue.

I'm not aware of any documentation that describes exactly when and how this can happen - the behaviour is undefined if you use a dictionary in a non-threadsafe manner.

For testing in your dev environment, I'd suggest running some parallel threads that randomly insert, remove, update etc in a continuous loop (basically a "concentrated" version of what's happening in your production environment).




回答3:


Here's a thread-safe dictionary http://devplanet.com/blogs/brianr/archive/2008/09/26/thread-safe-dictionary-in-net.aspx also check out this SO question.



来源:https://stackoverflow.com/questions/1920864/when-does-a-dictionary-throw-an-indexoutofrangeexception-on-add-or-containskey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!