jbevain/cecil — Issue #85: Race condition in type resolver
Author: jack-pappas
Created: 2011-11-20 19:45:22+00:00
State: open

Hi JB,

Over the past few months, I've had some intermittent crashes in my code which uses Cecil; since Cecil normally works great, I always just restarted the application and it generally worked fine the next time. A few days ago, I decided to look into the issue some more, and I noticed the problem:

If my assembly-manipulating code loads an AssemblyDefinition, then very quickly gets some TypeReference (e.g., the parameter type of a method defined by a type in the assembly), the TypeReference is returned with all of its property values set to defaults (0, false, null, etc.). This happens even if the TypeReference is to some primitive type like System.Double.

At first, I thought it might be due to some issue with the deferred resolving (I was loading the AssemblyDefinition with ReadingMode.Deferred, the default), but when I set the ReadingMode to Immediate, the bug occurs every time I run my program (instead of intermittently).

The only cause for this I could think of was some kind of race condition, but I spent a little time looking through the code which loads the assembly, resolves the types, etc., and didn't notice anything obvious which could cause such a race condition. Any thoughts?

Thanks,
Jack

P.S. -- It didn't fix the problem (I tried), but you may consider adding a lock() to the methods in DefaultAssemblyResolver which read/update the cache dictionary; or, if you use a preprocessor macro, you could simply use the built-in concurrent dictionary in .NET 4.0.