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

Connection pool can return connections from previous generations

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major - P3
    • Resolution: Fixed
    • None
    • 3.11
    • CMAP, Connection Mgmt
    • 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

              shane.harvey@mongodb.com Shane Harvey
              shane.harvey@mongodb.com Shane Harvey
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: