-
Type:
Bug
-
Resolution: Fixed
-
Priority:
Blocker - P1
-
Affects Version/s: None
-
Component/s: None
-
None
-
None
-
Fully Compatible
-
Dotnet Drivers
-
Not Needed
-
None
-
None
-
None
-
None
-
None
-
None
Summary
We have a service with many assemblies containing domain types that need to be registered with MongoDB driver for serialization. During service startup, we scan all assemblies in parallel and register each type through BsonClassMap.LookupClassMap(type) method.
After upgrading MongoDB.Driver from version 2.30.0 to 3.5.2, deadlocks started occurring when multiple threads concurrently call BsonClassMap.LookupClassMap(type) for type registration.
The deadlock happens between:
- Thread A: Holds write lock on BsonSerializer.ConfigLock, waits for Lazy<IBsonSerializer<T>>._state lock
- Thread B: Holds Lazy<IBsonSerializer<T>>._state lock, waits for read lock on BsonSerializer.ConfigLock
This appears to be a lock ordering issue introduced in the newer driver version. The same code worked without issues in version 2.30.0.
How to Reproduce
<PackageReference Include="MongoDB.Bson" Version="3.5.2" />
<PackageReference Include="MongoDB.Driver" Version="3.5.2" />

Additional Background
Please provide any additional background information that may be helpful in diagnosing the bug.