question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

System.ArgumentException: An item with the same key has already been added from ExpressionTreeOptimizationContext

See original GitHub issue

ArgumentException sometimes happens in a multithreading system

Exception message:
System.ArgumentException: An item with the same key has already been added.
Stack trace: 
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at LinqToDB.Linq.Builder.ExpressionTreeOptimizationContext.IsServerSideOnly(Expression expr)
   at LinqToDB.Linq.Builder.ExpressionTreeOptimizationContext.CanBeCompiledFind(CanBeCompiledContext context, Expression ex)
   at LinqToDB.Expressions.FindVisitor`1.Find(Expression expr)
   at LinqToDB.Linq.Builder.ExpressionTreeOptimizationContext.CanBeCompiled(Expression expr)
   at LinqToDB.Linq.Builder.ExpressionBuilder.RegisterParameter(Expression expression)
   at LinqToDB.Linq.Builder.EagerLoading.<>c.<PrepareParameters>b__41_0(ValueTuple`4 context, Expression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(LambdaExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(UnaryExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.Transform[T](IList`1 source)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(MethodCallExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.Transform[T](IList`1 source)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(MethodCallExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.Transform[T](IList`1 source)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(MethodCallExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.Transform[T](IList`1 source)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(MethodCallExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.Transform[T](IList`1 source)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(MethodCallExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.Transform[T](IList`1 source)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(MethodCallExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.Transform[T](IList`1 source)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(MethodCallExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.Transform[T](IList`1 source)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(MethodCallExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.Transform[T](IList`1 source)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(MethodCallExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.Transform[T](IList`1 source)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(MethodCallExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.Transform[T](IList`1 source)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(MethodCallExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.Transform[T](IList`1 source)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(MethodCallExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.Transform[T](IList`1 source)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(MethodCallExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.Transform[T](IList`1 source)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(MethodCallExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.Transform[T](IList`1 source)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(MethodCallExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.Transform[T](IList`1 source)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(MethodCallExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.Transform[T](IList`1 source)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(MethodCallExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.Transform[T](IList`1 source)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(MethodCallExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.Transform[T](IList`1 source)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(MethodCallExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.Transform[T](IList`1 source)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(MethodCallExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.Transform[T](IList`1 source)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(MethodCallExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Expressions.TransformVisitor`1.Transform[T](IList`1 source)
   at LinqToDB.Expressions.TransformVisitor`1.TransformX(MethodCallExpression e)
   at LinqToDB.Expressions.TransformVisitor`1.Transform(Expression expr)
   at LinqToDB.Linq.Builder.EagerLoading.PrepareParameters(Expression expr, ExpressionBuilder builder, ParameterContainer& container, Expression& correctedExpression)
   at LinqToDB.Linq.Builder.EagerLoading.<>c__DisplayClass43_0`2.<<RegisterPreambles>b__1>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at LinqToDB.Linq.Builder.ExpressionBuilder.<>c__DisplayClass212_0`1.<<RegisterPreamble>b__1>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at LinqToDB.Linq.Query.<InitPreamblesAsync>d__34.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at LinqToDB.Linq.ExpressionQuery`1.<LinqToDB-Async-IQueryProviderAsync-ExecuteAsync>d__15`1.MoveNext()

Steps to reproduce

Nothing special. The problem that simple Dictionary was used for caching

https://github.com/linq2db/linq2db/blob/master/Source/LinqToDB/Linq/Builder/ExpressionTreeOptimizationContext.cs

Dictionary<Expression, bool> _isServerSideOnlyCache = new ();

Possible solutions: Replace Dictionary<Expression, bool> with ConcurrentDictionary<Expression, bool> or use Set instead of Add in 361 line

_isServerSideOnlyCache[expr] = result;

Environment details

linq2db version: 3.4.3 Database Server: Azure ms sql Database Provider: SqlServer Operating system: Windows .NET Framework: 4.8

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Oct 7, 2021

Well, I need to check. Suspicious place is found.

0reactions
tralalablblblcommented, Oct 7, 2021

I’ve checked the whole stack trace, and there are no forgotten awaits We saw such exceptions in different places, usually after starting the application.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Item with Same Key has already been added
The error throws an ArgumentException saying,. "An item with the same key has already been added." I am unsure after several Google searches...
Read more >
An item with the same key has already been added. ...
ArgumentException : An item with the same key has already been added. Key: Param_0" - when I try to run query second and...
Read more >
ERROR An item with the same key has already been added
Your errors occurs when fetching the rendering parameters of the LanguageSelector component. The "Item with the same key" refers to the fact ...
Read more >
Release notes for SQL Server Management Studio (SSMS)
Azure Data Studio is automatically installed alongside SSMS. ... or an error similar to "An item with the same key has already been...
Read more >
Changelog | Burst | 1.8.8
Fixed "An item with the same key has already been added" compiler error that could occur when duplicate field names were present in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found