PYTHON-5671: Use fine-grained locks in the connection pool to reduce contention#2804
Open
sophiayangDB wants to merge 7 commits into
Open
PYTHON-5671: Use fine-grained locks in the connection pool to reduce contention#2804sophiayangDB wants to merge 7 commits into
sophiayangDB wants to merge 7 commits into
Conversation
sleepyStick
reviewed
May 15, 2026
| - Steven Silvester (blink1073) | ||
| - Noah Stapp (NoahStapp) | ||
| - Cal Jacobson (cj81499) | ||
| - Sophia Yang (sophiayangDB) |
Contributor
There was a problem hiding this comment.
yay!! first pymongo change!!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PYTHON-5671
Changes in this PR
Based on investigation in PYTHON-5007, using fine-grained locks improved thread efficiency by about 20% in a small read scenario. This PR seeks to implement the suggestions.
Previously, code in
pool.pywas protected by the sole lockself.lockand its two conditionals. Now, there are six individual locks that are the sole protectors of the following:self._conns_lockProtects
self.connsself._active_contexts_lockProtects
self.active_contextsas well as the subsequent updates toself.__pinned_sockets,self.active_sockets,next_connection_id,self.ncursors, andself.ntxnsself.size_condProtects
self.requestsandself.genself._max_connecting_condProtects
self._pendingself._operation_count_lockProtects
self.operation_countself.lockProtects
self.stateOther strategies used to mitigate lock contention include:
update_is_writable()as its sole caller already has the lockself.active_contextsto update its references when additional additions/deletions to the set should not be operated upon or their absence does not modify behavior, avoids errors that arise if calling on the original and it's length/size changesTest Plan
Tests already exist in
main/test/test_pooling.pyandtest/asynchronous/test_pooling.py. Since this was only a code refactor, no additional tests were added.Checklist
Checklist for Author
Checklist for Reviewer