Skip to content
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

Closed
stakx opened this issue Jun 25, 2018 · 5 comments

Comments

@stakx
Copy link
Contributor

stakx commented Jun 25, 2018

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.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 the ModuleBuilder that contains type is being mutated by concurrent System.Reflection.Emit calls?

/cc @BrunoJuchli, @TimLovellSmith, @atsushikan

@BrunoJuchli
Copy link
Contributor

BrunoJuchli commented Jun 25, 2018

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.

@jkotas
Copy link
Member

jkotas commented Jun 25, 2018

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?

Yes, it is how it is supposed to work.

@stakx
Copy link
Contributor Author

stakx commented Jun 25, 2018

@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?

@jkotas
Copy link
Member

jkotas commented Jun 25, 2018

Right.

@stakx
Copy link
Contributor Author

stakx commented Jun 25, 2018

Closing this for now, as our question has been answered. Thank you!

@stakx stakx closed this as completed Jun 25, 2018
@msftgits msftgits transferred this issue from dotnet/coreclr Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants