Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-1613

Race condition on DBPort usage during write commands

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.12.5
    • Affects Version/s: 2.12.0
    • Component/s: Connection Management
    • Labels:
      None

      A DBPort maintains a usageCount which is incremented upon each usage of a DBPort. In the case of an UNACKNOWLEDGED write, this usageCount is utilized by WriteResult to indicate when a subsequent call to getLastError() is unsafe because its DBPort has already been checked out and used by another thread. (To be fair, a deprecated pattern as of 2.13.0.)

      The race is that while non-write command operations using DBPort.say() and DBPort.call() do ensure the usageCount is safely incremented, write commands go through DBTCPConnector and do not increment this counter.

      Therefore it's possible that a write command on one thread may end up sharing a DBPort for a getLastError() call on another thread, resulting in undefined behavior.

      Available Workaround
      Use ACKNOWLEDGED writes whenever a getLastError() result is required, such as for a WriteResult.getUpsertedId().

      Credit: jeff.yemin for analysis and discovery.

            Assignee:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Reporter:
            john.morales@mongodb.com John Morales (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: