Skip to content

PYTHON-5821 - Fix ordering issue between event publish and logging for Pool monitoring tests - #2796

Merged
NoahStapp merged 1 commit into
mongodb:masterfrom
NoahStapp:PYTHON-5821
May 7, 2026
Merged

NoahStapp merged 1 commit into
mongodb:masterfrom
NoahStapp:PYTHON-5821

Conversation

@NoahStapp

Copy link
Copy Markdown
Contributor

PYTHON-5821

Changes in this PR

Emit Pool debug log messages before Pool events.

Before this change, Pool events publish before the corresponding logs emit. In the synchronous API, this can cause the following ordering issue in tests such as test.test_connection_logging.TestConnectionLoggingConnectionPoolOptions.test_waitQueueTimeoutMS_should_be_included_in_connection_pool_created_message_when_specified:

  1. Test calls wait_until to poll for the event being published.
  2. Pool publishes the event from the monitor thread.
  3. Before the log can be published, the thread running wait_until preempts the Pool's thread and sees that the event has been published. This causes the test to run its assertions, which fail since the log has not been emitted.

Test Plan

Existing connection logging + monitoring 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 May 7, 2026 15:01
@NoahStapp
NoahStapp requested review from Copilot and sleepyStick May 7, 2026 15:01
@NoahStapp NoahStapp changed the title PYTHON-5821 - Fix ordering issue between event publish and logging for Pool PYTHON-5821 - Fix ordering issue between event publish and logging for Pool monitoring tests May 7, 2026

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 PyMongo’s connection pool lifecycle instrumentation so that Pool debug log messages are emitted before the corresponding CMAP pool events, reducing nondeterministic ordering between logs and event publication.

Changes:

  • Reordered publish_pool_created to occur after the POOL_CREATED debug log.
  • Reordered publish_pool_ready, publish_pool_closed, and publish_pool_cleared to occur after their corresponding debug logs.
  • Applied the same ordering changes to both synchronous and asynchronous pool implementations.

Reviewed changes

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

File Description
pymongo/synchronous/pool.py Reorders pool lifecycle log emission ahead of CMAP pool events (created/ready/cleared/closed).
pymongo/asynchronous/pool.py Mirrors the same log-before-event ordering changes for the async pool.

self.opts._event_listeners.publish_pool_created(
self.address, self.opts.non_default_options
)
# Log before publishing event to prevent potential listener preemption in tests
Comment thread pymongo/asynchronous/pool.py Resolved
@codecov-commenter

codecov-commenter commented May 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@NoahStapp
NoahStapp merged commit 8dc7efa into mongodb:master May 7, 2026
90 of 93 checks passed
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.

4 participants