You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A few people and I have been trying to track down a somewhat elusive multi-threading issue in Castle DynamicProxy and/or the runtime (castleproject/Core#193). We're getting to the point where we could use some expert knowledge from the CLR-savvy folks.
DynamicProxy performs lots of dynamic type generation using System.Reflection.Emit. The code that uses ModuleBuilder, TypeBuilder etc. to generate the types is guarded with an exclusive lock that only one thread may enter at a time. (This because, as far as we know, System.Reflection.Emit does not give any official thread-safety guarantees.)
However, the Types returned by TypeBuilder.CreateType() are instantiated using Activator.CreateInstanceoutside of any lock.
We've noticed that our elusive threading issue appears to occur significantly less often if Activator.CreateInstance is called while a lock is being held that prevents the dynamic module from being modified.
So we're wondering: Is a call to Activator.CreateInstance(type) officially (thread-) safe if the ModuleBuilder that contains type is being mutated by concurrent System.Reflection.Emit calls?
Note: the 'type' and any of it's references are not modified while Activator.CreateInstance(type) is called. However, other types are concurrently emitted to the same Module.
Is a call to Activator.CreateInstance(type) officially (thread-) safe if the ModuleBuilder that contains type is being mutated by concurrent System.Reflection.Emit calls?
@jkotas, thanks for the fast response, great to hear! I'm assuming it would then also be safe to invoke not-yet-JIT-ted methods on the type while other additional types are emitted into the dynamic module?
A few people and I have been trying to track down a somewhat elusive multi-threading issue in Castle DynamicProxy and/or the runtime (castleproject/Core#193). We're getting to the point where we could use some expert knowledge from the CLR-savvy folks.
DynamicProxy performs lots of dynamic type generation using System.Reflection.Emit. The code that uses
ModuleBuilder
,TypeBuilder
etc. to generate the types is guarded with an exclusive lock that only one thread may enter at a time. (This because, as far as we know, System.Reflection.Emit does not give any official thread-safety guarantees.)However, the
Type
s returned byTypeBuilder.CreateType()
are instantiated usingActivator.CreateInstance
outside of any lock.We've noticed that our elusive threading issue appears to occur significantly less often if
Activator.CreateInstance
is called while a lock is being held that prevents the dynamic module from being modified.So we're wondering: Is a call to
Activator.CreateInstance(type)
officially (thread-) safe if theModuleBuilder
that containstype
is being mutated by concurrent System.Reflection.Emit calls?/cc @BrunoJuchli, @TimLovellSmith, @atsushikan
The text was updated successfully, but these errors were encountered: