Skip to content

PYTHON-5875 - Wrap SSL handshake errors before labeling with overload#2866

Open
NoahStapp wants to merge 2 commits into
mongodb:masterfrom
NoahStapp:PYTHON-5875
Open

PYTHON-5875 - Wrap SSL handshake errors before labeling with overload#2866
NoahStapp wants to merge 2 commits into
mongodb:masterfrom
NoahStapp:PYTHON-5875

Conversation

@NoahStapp

@NoahStapp NoahStapp commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

PYTHON-5875

Changes in this PR

Brings the driver into backpressure spec alignment for SSL handshake errors caused by the connection establishment rate limiter. The raised error by such a rejection can be a raw SSLError or OSError that would be rejected for the correct labeling, causing the test failure described in the ticket. This fixes that by correctly wrapping the raised error first so that the proper labeling can occur.

Test Plan

Existing 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?

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@NoahStapp NoahStapp marked this pull request as ready for review June 11, 2026 14:08
@NoahStapp NoahStapp requested a review from a team as a code owner June 11, 2026 14:08
@NoahStapp NoahStapp requested review from aclark4life and Copilot June 11, 2026 14:08

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.

Pull request overview

This PR adjusts connection-establishment error handling in the connection pool so that certain connection failures are first wrapped into AutoReconnect/NetworkTimeout and then processed for SystemOverloadedError labeling, ensuring the label is applied to the exception that is ultimately propagated to callers.

Changes:

  • In both sync and async pools, wrap (IOError, OSError, *SSLErrors) via _raise_connection_failure(...) before calling _handle_connection_error(...).
  • Ensure _handle_connection_error(...) runs on the wrapped AutoReconnect/NetworkTimeout (so any overload labels are attached to the raised exception).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
pymongo/synchronous/pool.py Updates connect() error path to label the wrapped connection failure exception.
pymongo/asynchronous/pool.py Mirrors the same connect() error-path change for async pooling.

Comment on lines 1064 to +1073
if isinstance(error, (IOError, OSError, *SSLErrors)):
details = _get_timeout_details(self.opts)
_raise_connection_failure(self.address, error, timeout_details=details)
# Wrap to AutoReconnect/NetworkTimeout BEFORE labeling so the
# SystemOverloadedError label lands on the propagated exception.
try:
_raise_connection_failure(self.address, error, timeout_details=details)
except (AutoReconnect, NetworkTimeout) as wrapped:
self._handle_connection_error(wrapped)
raise
self._handle_connection_error(error)

@NoahStapp NoahStapp Jun 11, 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.

Per the relevant part of the CMAP spec, the only types of establishment errors that MUST be excluded from having the backpressure labels applied are the following:

  1. DNS lookup failures: excluded by the socket.gaierror check in _handle_connection_error.
  2. Non-I/O TLS errors (certificate validation, hostname mismatch failures): excluded the _CertificateError, SSLErrors check in `_handle_connection_errors.
  3. SOCKS5 errors: PyMongo does not currently support SOCKS5.

This change then correctly addresses a bug in the driver where errors that MUST be labeled as possible overload errors are not.

Comment thread pymongo/asynchronous/pool.py
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