-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Q: Is Activator.CreateInstance(typeFromDynamicModule)
safe to call while same dynamic module is being mutated?
#10569
Comments
Note: the 'type' and any of it's references are not modified while |
Yes, it is how it is supposed to work. |
@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? |
Right. |
Closing this for now, as our question has been answered. Thank you! |
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: