Repository navigation
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The version guard leaves TLS session reuse enabled on affected Python 3.12.0–3.12.6 and 3.13.0 releases.
1 open issue
What changed in this PR
This PR disables TLS session reuse on Python 3.11 in PyMongo’s shared connection pool to avoid a CPython memory leak.
Changes:
- Prevents session-cache allocation on Python versions below 3.12.
- Adds cache-disabled assertions and skips reuse tests on affected runtimes.
- Documents the workaround and upstream fix versions.
| File | Description |
|---|---|
test/test_ssl.py |
Updates generated synchronous TLS cache tests. |
test/asynchronous/test_ssl.py |
Adds version-dependent TLS cache assertions and skips. |
pymongo/pool_shared.py |
Gates session-cache allocation by Python version. |
doc/changelog.rst |
Records the workaround and CPython fix versions. |
Review effort: Balanced
| # Reading SSLSocket.session / SSLObject.session leaks a copy of the session on | ||
| # every call in CPython 3.11, so disable TLS session reuse on that version. | ||
| _TLS_SESSION_REUSE_LEAKS = sys.version_info < (3, 12) |
There was a problem hiding this comment.
We specifically said in our meeting that we do not distinguish between patch versions of CPython. If users want to avoid this bug on Python 3.12 or 3.13, they will have to upgrade patch versions.
There was a problem hiding this comment.
In that case, we should include that info in the changelog e.g.:
Disabled TLS session reuse on Python 3.11 due to a CPython memory leak bug (
python/cpython#116810 <https://github.com/python/cpython/issues/116810>). The bug was fixed in Python 3.12.7 and Python 3.13.1. Python 3.12.0 through 3.12.6 and 3.13.0 still contain the bug; users on those versions should upgrade Python. (PYTHON-6182)
There was a problem hiding this comment.
The changelog already says "The bug was fixed in Python 3.12.7 and Python 3.13.1", which implies that Python 3.12.0 through 3.12.6 and 3.13.0 contain the bug.
There was a problem hiding this comment.
The changelog already says "The bug was fixed in Python 3.12.7 and Python 3.13.1", which implies that Python 3.12.0 through 3.12.6 and 3.13.0 contain the bug.
The changelog should explicitly state that we are not addressing the bug on 3.12 - 3.12.6 and 3.13 and that "Python 3.12.0 through 3.12.6 and 3.13.0 still contain the bug; users on those versions should upgrade Python." (PYTHON-6182)" if for no other reason than Copilot may not have flagged this had we done that to begin with 😂
In other words, "You can use 3.12 with PyMongo just make sure you upgrade your Python." Otherwise, the implicit warning to not use PyMongo with those versions is more likely to go unnoticed.
Ideally we would track patch versions of Python but that's a different issue and the least we can do here is be super-clear about what's going on. 🙏
| # Reading SSLSocket.session / SSLObject.session leaks a copy of the session on | ||
| # every call in CPython 3.11, so disable TLS session reuse on that version. | ||
| _TLS_SESSION_REUSE_LEAKS = sys.version_info < (3, 12) |

PYTHON-6182
Changes in this PR
Disable TLS session reuse on Python 3.11 due to a memory leak bug in CPython.
Test Plan
New tests.
Checklist
Checklist for Author
Checklist for Reviewer