Bug 44546 - System.InvalidOperationException : System.Reflection.Emit.DynamicMethod:create_dynamic_method
Summary: System.InvalidOperationException : System.Reflection.Emit.DynamicMethod:creat...
Status: NEW
Alias: None
Product: Runtime
Classification: Mono
Component: Reflection ()
Version: 4.4.2 (C7SR1)
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2016-09-20 21:36 UTC by Aaron Stannard
Modified: 2017-02-09 15:30 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 44546 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Aaron Stannard 2016-09-20 21:36:24 UTC
We have a bug we've been able to reproduce only occasionally inside the Akka.NET codebase on Mono, and it appears to be an issue where the expression compiler can intermittently fail when creating a delegeate.

Stack trace:

System.InvalidOperationException : 
  at (wrapper managed-to-native) System.Reflection.Emit.DynamicMethod:create_dynamic_method (System.Reflection.Emit.DynamicMethod,System.Reflection.Emit.DynamicMethod)
  at System.Reflection.Emit.DynamicMethod.CreateDynMethod () <0x407042a0 + 0x0019e> in <filename unknown>:0 
  at System.Reflection.Emit.DynamicMethod.CreateDelegate (System.Type delegateType, System.Object target) <0x40704200 + 0x0003a> in <filename unknown>:0 
  at System.Linq.Expressions.Compiler.LambdaCompiler.CreateDelegate () <0x40704090 + 0x00082> in <filename unknown>:0 
  at System.Linq.Expressions.Compiler.LambdaCompiler.Compile (System.Linq.Expressions.LambdaExpression lambda, System.Runtime.CompilerServices.DebugInfoGenerator debugInfoGenerator) <0x406f2f90 + 0x000b7> in <filename unknown>:0 
  at System.Linq.Expressions.Expression`1[TDelegate].Compile () <0x406f2f40 + 0x0000f> in <filename unknown>:0 
  at Akka.Util.Reflection.ExpressionExtensions.GetArguments (System.Linq.Expressions.NewExpression newExpression) <0x406f1170 + 0x0011b> in <filename unknown>:0 
  at Akka.Actor.Props.Create[TActor] (System.Linq.Expressions.Expression`1 factory, Akka.Actor.SupervisorStrategy supervisorStrategy) <0x406f0f60 + 0x000d3> in <filename unknown>:0 
  at Akka.Actor.LocalActorRefProvider.CreateSystemGuardian (Akka.Actor.LocalActorRef rootGuardian, System.String name, Akka.Actor.LocalActorRef userGuardian) <0x406ed070 + 0x001db> in <filename unknown>:0 
  at Akka.Actor.LocalActorRefProvider.Init (Akka.Actor.Internal.ActorSystemImpl system) <0x406ddb20 + 0x00297> in <filename unknown>:0 
  at Akka.Actor.Internal.ActorSystemImpl.Start () <0x406dd3d0 + 0x000e8> in <filename unknown>:0 

Example of one of the offending blocks of code:


Full build logs and report available here:


Issue appears to be racy - doesn't usually happen, but when it does it occurs repeatedly and temporarily. Affected 11 tests total out of a large suite otherwise.
Comment 1 Zoltan Varga 2016-09-21 20:48:55 UTC
That exception is thrown when System.Reflection.Emit.DynamicMethod.CreateDelegate () is called while the runtime is shutting down.
Comment 2 Aaron Stannard 2016-09-23 15:58:10 UTC
Why would the runtime be shutting down during the middle of an XUnit test run?
Comment 3 Zoltan Varga 2016-09-23 16:33:11 UTC
Looking at the code, that seems to be the condition which makes that method throw that exception, maybe there is some other code path which does it.
Comment 4 Roman 2017-02-09 15:30:20 UTC
Ha ha this bug has happened 2 times in my case, but there I used Newtonsoft.Json

Newtonsoft.Json.JsonSerializationException: Error getting value from 'Cheque' on 'Terminal.Entities.Payment'. ---> System.InvalidOperationException
  at (wrapper managed-to-native) System.Reflection.Emit.DynamicMethod:create_dynamic_method (System.Reflection.Emit.DynamicMethod,System.Reflection.Emit.DynamicMethod)
  at System.Reflection.Emit.DynamicMethod.CreateDynMethod () [0x000c5] in /tmp/buildd/mono- 
  at System.Reflection.Emit.DynamicMethod.CreateDelegate (System.Type delegateType) [0x00029] in /tmp/buildd/mono- 
  at Newtonsoft.Json.Utilities.DynamicReflectionDelegateFactory.CreateGet[T] (System.Reflection.PropertyInfo propertyInfo) [0x00047] in <c5b8b1476b514bcaad468ae001c1fb99>:0 
  at Newtonsoft.Json.Utilities.ReflectionDelegateFactory.CreateGet[T] (System.Reflection.MemberInfo memberInfo) [0x00010] in <c5b8b1476b514bcaad468ae001c1fb99>:0 
  at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue (System.Object target) [0x00008] in <c5b8b1476b514bcaad468ae001c1fb99>:0 
   --- End of inner exception stack trace ---
  at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue (System.Object target) [0x00054] in <c5b8b1476b514bcaad468ae001c1fb99>:0 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonContainerContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonProperty property, Newtonsoft.Json.Serialization.JsonContract& memberContract, System.Object& memberValue) [0x00066] in <c5b8b1476b514bcaad468ae001c1fb99>:0 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract collectionContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty) [0x0003c] in <c5b8b1476b514bcaad468ae001c1fb99>:0
Comment 5 Roman 2017-02-09 15:30:54 UTC
4.6.2 mono version Linux mint