Skip to content

PYTHON-6182 Disable TLS session reuse on Python 3.11 due to memory leak - #3154

Open
NoahStapp wants to merge 1 commit into
mongodb:mainfrom
NoahStapp:PYTHON-6182
Open

NoahStapp wants to merge 1 commit into
mongodb:mainfrom
NoahStapp:PYTHON-6182

Conversation

@NoahStapp

Copy link
Copy Markdown
Contributor

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

  • Did you update the changelog (if necessary)?
  • Is there test coverage?
  • Is any followup work tracked in a JIRA ticket? If so, add link(s).

Checklist for Reviewer

  • Does the title of the PR reference a JIRA Ticket?
  • Do you fully understand the implementation? (Would you be comfortable explaining how this code works to someone else?)
  • Is all relevant documentation (README or docstring) updated?

@NoahStapp
NoahStapp requested a review from a team as a code owner October 27, 2026 19:16
@NoahStapp
NoahStapp requested review from aclark4life and a balanced review from Copilot October 27, 2026 19:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread pymongo/pool_shared.py
Comment on lines +90 to +92
# 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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree

@NoahStapp NoahStapp Oct 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@NoahStapp NoahStapp Oct 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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. 🙏

Comment thread pymongo/pool_shared.py
Comment on lines +90 to +92
# 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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants