Deadlock during concurrent BsonClassMap initialization

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Fixed
    • Priority: Blocker - P1
    • 3.7.0
    • 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.

        1. image-2026-02-24-12-10-17-454.png
          image-2026-02-24-12-10-17-454.png
          691 kB
        2. image-2026-02-24-12-09-37-654.png
          image-2026-02-24-12-09-37-654.png
          557 kB
        3. image-2026-02-20-15-18-46-140.png
          image-2026-02-20-15-18-46-140.png
          169 kB

            Assignee:
            Oleksandr Poliakov
            Reporter:
            flibustier seas
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: