Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The collection's state is no longer correct Exception #10

Open
Coder3333 opened this issue Feb 25, 2019 · 6 comments
Open

The collection's state is no longer correct Exception #10

Coder3333 opened this issue Feb 25, 2019 · 6 comments
Labels
needs author feedback Needs feedback from the author to be closed or worked on.

Comments

@Coder3333
Copy link

I randomly get this exception when calling Evaluate from the middleware of my ASP.Net Core web application. All I do is pass a string into the expression. I cannot think of any way that I can be affecting any collections in a non-concurrent way. Any suggestions?

Sometimes I get this error:

NCalc.EvaluationException: 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. at NCalc.Expression.Evaluate()

Sometimes the error looks like this:

NCalc.EvaluationException: Collection was modified; enumeration operation may not execute. at NCalc.Expression.Evaluate()

            var eap = new NCalc.Expression(expression);
            return eap.Evaluate();
@yallie
Copy link
Member

yallie commented Feb 26, 2019

Could you please create a unit test to demonstrate the issue?
I'd be glad to accept a PR.

@Coder3333
Copy link
Author

The expression I am using is something as simple as the string "400 = 200", which I expect to return false. I have never been able to replicate the issue in a unit tests. I only see it in production, which makes me think there is a static collection somewhere in the code that is being shared by threads, but I have not been able to find it.

@Coder3333
Copy link
Author

Coder3333 commented Mar 13, 2019

I am starting to think this is a bug in .Net Core. I am going to update to Microsoft.AspNetCore.All 2.2.3 from 2.0.0 and see if the issue goes away.

I did find a static Dictionary in the NCalc codebase that is likely what is throwing the exception. Without having spent too much time figuring out what this is for, I am wondering if this would be better handled with a ConcurrentDictionary, so the ReaderWriterLock would not need to be used.

private static Dictionary<string, WeakReference> _compiledExpressions = new Dictionary<string, WeakReference>();

@yallie
Copy link
Member

yallie commented Mar 14, 2019

I did find a static Dictionary in the NCalc codebase that is likely what is throwing the exception.

Yes, that looks suspicious.
By the way, does the issue reproduce when you set CacheEnabled = false?

@codermrrob
Copy link

I am also getting this error in production. It happens rarely and I have been unable to reproduce it in a dev/testing environment.

Expressions are always simple. The most recent fail was evaluating 403 * -1

My suspicions have also been on the static dictionary. I will disable the cache, and it seems like that will stop using the static dictionary.

@gumbarros
Copy link
Member

@codermrrob we are now using a ConcurrentDictionary at v3.12, now the cache is truly thread-safe. Feel free to close the issue if everything is working or more details if the error still happens.

@gumbarros gumbarros added the needs author feedback Needs feedback from the author to be closed or worked on. label Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs author feedback Needs feedback from the author to be closed or worked on.
Projects
None yet
Development

No branches or pull requests

4 participants