Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-2205

Connection pool can return connections from previous generations

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.11
    • Component/s: CMAP, Connection Management
    • Labels:
      None

      Description

      There is a race between Pool.return_socket and Pool.reset that can cause the pool to return connections from previous generations (pool_id).

      1. Assume the current pool generation is 1.
      2. Thread A calls pool.return_socket to return a connection with generation 1 and pauses while attempting to acquire the lock: https://github.com/mongodb/mongo-python-driver/blob/3.10.1/pymongo/pool.py#L1224
      3. Thread B calls pool.reset which clears the pool and increments the generation to 2.
      4. Thread A resumes execution, acquires the lock, and adds the connection (with generation 1) to the pool: https://github.com/mongodb/mongo-python-driver/blob/3.10.1/pymongo/pool.py#L1225
      5. The pool with generation 2 now has a connection with generation 1.
      6. A subsequent checkout will return the stale connection.

      To fix:

      1. Fix the race condition by checking the generation after acquiring the lock in pool.return_socket.
      2. And a sanity check that discards stale connections during checkout.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              shane.harvey Shane Harvey
              Reporter:
              shane.harvey Shane Harvey
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: