Repository navigation
Closed
Description
This is a very strange error that is happening before my eyes for the second time (the first time I was unable to save any information).
Occurs on a method call:
internal static Func<TIn, TOut> AsFunc(Expression<Func<TIn, TOut>> expression)
=> Cache.GetOrAdd(expression, x => x.CompileFast());
where Cache is private static readonly ConcurrentDictionary<Expression<Func<TIn, TOut>>, Func<TIn, TOut>>.
Callstack from output:
System.ArgumentNullException: Value cannot be null. (Parameter 'meth')
at System.ArgumentNullException.Throw(String paramName)
at System.ArgumentNullException.ThrowIfNull(Object argument, String paramName)
at System.Reflection.Emit.DynamicILGenerator.Emit(OpCode opcode, MethodInfo meth)
at FastExpressionCompiler.ExpressionCompiler.EmittingVisitor.TryEmitConvert(UnaryExpression expr, IReadOnlyList`1 paramExprs, ILGenerator il, ClosureInfo& closure, CompilerFlags setup, ParentFlags parent) in /_/src/FastExpressionCompiler/FastExpressionCompiler.cs:line 2892
at FastExpressionCompiler.ExpressionCompiler.EmittingVisitor.TryEmit(Expression expr, IReadOnlyList`1 paramExprs, ILGenerator il, ClosureInfo& closure, CompilerFlags setup, ParentFlags parent, Int32 byRefIndex) in /_/src/FastExpressionCompiler/FastExpressionCompiler.cs:line 1847
at FastExpressionCompiler.ExpressionCompiler.EmittingVisitor.TryEmitComparison(Expression left, Expression right, ExpressionType nodeType, Type exprType, IReadOnlyList`1 paramExprs, ILGenerator il, ClosureInfo& closure, CompilerFlags setup, ParentFlags parent) in /_/src/FastExpressionCompiler/FastExpressionCompiler.cs:line 4784
at FastExpressionCompiler.ExpressionCompiler.EmittingVisitor.TryEmit(Expression expr, IReadOnlyList`1 paramExprs, ILGenerator il, ClosureInfo& closure, CompilerFlags setup, ParentFlags parent, Int32 byRefIndex) in /_/src/FastExpressionCompiler/FastExpressionCompiler.cs:line 1900
at FastExpressionCompiler.ExpressionCompiler.TryCompileBoundToFirstClosureParam(Type delegateType, Expression bodyExpr, IReadOnlyList`1 paramExprs, Type[] closurePlusParamTypes, Type returnType, CompilerFlags flags) in /_/src/FastExpressionCompiler/FastExpressionCompiler.cs:line 503
at FastExpressionCompiler.ExpressionCompiler.CompileFast[T1,R](Expression`1 lambdaExpr, Boolean ifFastFailedReturnNull, CompilerFlags flags) in /_/src/FastExpressionCompiler/FastExpressionCompiler.cs:line 203
at Test.Extensions.ExpressionEx.CompiledExpressions`2.<>c.<AsFunc>b__1_0(Expression`1 x) in C:\dev\Test\Extensions\ExpressionEx.cs:line 85
Moreover, whether last time or this time, if you call the same section of code again without restarting the solution, then everything works successfully.
Is this some kind of floating bug in the library? Or do you have any ideas about what mistake I am making that can lead to this behaviour?
Activity
dadhi commentedon Jan 15, 2024
Hi @EgoPingvina
expr.ToExpressionString()to see the final expression.For now, given that the ex happened in
TryEmitConvertwith themethmethod being used there.It is either the conversion method itself or the Nullable methods, like
GetValueOrDefault,HasValue,get_Value.So I need to know more about kind of operands in the convert for your case.
EgoPingvina commentedon Jan 15, 2024
expression.ToExpressionString():property
public UserType? UserType { get; set; }contains the value of the enum of the same nameEgoPingvina commentedon Jan 15, 2024
Perhaps this is still a problem with enums, which was solved in the latest updates:
dadhi commentedon Jan 15, 2024
Ok, so if you convert to
ToCSharpString()it will be something like this?First, not relevant to the issue - just a note to myself. But the constant probably should be used as literal constant and not put into a closure.
Second, it seems not related to the
HasFlag, but rather to the nullables in general... they are big PITA.I will create the test case for that and see what I find.
EgoPingvina commentedon Jan 15, 2024
yes,
x => x.UserType != type, where isUserType typeandTest.Message x.dadhi commentedon Jan 15, 2024
Like this:
Just wondering, why is there lowercase
.type?EgoPingvina commentedon Jan 15, 2024
Because of
typeis the name of the method parameter. It's not a value of enum :) sorry for the confusiondadhi commentedon Jan 15, 2024
Ehhh, I don't understand. It seems like a
Fieldto me:Could you actually run the
ToCSharpString()on this thing and paste the output?EgoPingvina commentedon Jan 15, 2024
dadhi commentedon Jan 15, 2024
@EgoPingvina Again about the
type. Is it really of theint?type internally, or there are cases when it is not (just a plain int)?EgoPingvina commentedon Jan 15, 2024
It's always the
UserTypeenum memberadding the test for #386
added other combinations for the #386
one more test for #386
adding the #386 tests to runners
dadhi commentedon Jan 16, 2024
@EgoPingvina
I have added the multiple tests (see the linked commits) with the multiple combination of nullable and not for UserType.
Everything is passing.
So maybe you will have an idea what is missing?
EgoPingvina commentedon Jan 17, 2024
So far, the only thing I have discovered is a method for guaranteed reproduction: I need to restart the project and simultaneously call the method from two places. Only a clean run and multiple simultaneous invokes guarantee an exception throw. I haven't delved deep into your library's source code and I could be wrong, but perhaps non-thread-safe caches are being used somewhere or something?
dadhi commentedon Jan 17, 2024
@EgoPingvina Hey, yeah. This is very helpful!
There are some caches involved. Will check the problems there.
EgoPingvina commentedon Jan 19, 2024
@dadhi Hello! Did you manage to fix this bug?
dadhi commentedon Jan 20, 2024
@EgoPingvina The v4.1.0 with the fix is out on NuGet
EgoPingvina commentedon Jan 22, 2024
Thanks a lot for the notice!
fixed: dadhi#386