Skip to content

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

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

PYTHON-5821 - Fix ordering issue between event publish and logging for Pool monitoring tests#2796
NoahStapp merged 1 commit intomongodb: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
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.94%. Comparing base (f4219bd) to head (5076905).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2796      +/-   ##
==========================================
+ Coverage   82.68%   87.94%   +5.26%     
==========================================
  Files         141      141              
  Lines       24406    24406              
  Branches     4176     4176              
==========================================
+ Hits        20179    21465    +1286     
+ Misses       3328     2047    -1281     
+ Partials      899      894       -5     
Flag Coverage Δ
auth-aws-rhel8-test-auth-aws-rapid-web-identity-python3.14-cov 35.01% <0.00%> (ø)
auth-aws-win64-test-auth-aws-rapid-web-identity-python3.14-cov 35.03% <0.00%> (ø)
auth-enterprise-macos-test-standard-auth-latest-python3.11-auth-ssl-sharded-cluster-cov 43.76% <0.00%> (+<0.01%) ⬆️
auth-enterprise-rhel8-test-standard-auth-latest-python3.11-auth-ssl-sharded-cluster-cov 43.75% <0.00%> (ø)
auth-enterprise-win64-test-standard-auth-latest-python3.11-auth-ssl-sharded-cluster-cov 43.80% <0.00%> (+0.02%) ⬆️
auth-oidc-local-ubuntu-22-test-auth-oidc-default 48.61% <100.00%> (?)
compression-snappy-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.34% <50.00%> (ø)
compression-snappy-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.47% <50.00%> (-0.01%) ⬇️
compression-snappy-rhel8-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.09% <50.00%> (ø)
compression-snappy-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.01% <50.00%> (+<0.01%) ⬆️
compression-zlib-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.35% <50.00%> (+<0.01%) ⬆️
compression-zlib-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.49% <50.00%> (-0.02%) ⬇️
compression-zlib-rhel8-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.09% <50.00%> (ø)
compression-zlib-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.00% <50.00%> (-0.03%) ⬇️
compression-zstd-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.37% <50.00%> (+0.01%) ⬆️
compression-zstd-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.50% <50.00%> (+0.03%) ⬆️
compression-zstd-rhel8-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.09% <50.00%> (ø)
compression-zstd-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.00% <50.00%> (-0.02%) ⬇️
compression-zstd-ubuntu-22-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 58.98% <50.00%> (ø)
coverage-report-coverage-report 87.92% <100.00%> (+5.50%) ⬆️
disable-test-commands-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.37% <50.00%> (+0.02%) ⬆️
disable-test-commands-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.52% <50.00%> (+<0.01%) ⬆️
disable-test-commands-rhel8-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.09% <50.00%> (ø)
disable-test-commands-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.00% <50.00%> (+<0.01%) ⬆️
encryption-crypt_shared-macos-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.72% <75.00%> (?)
encryption-crypt_shared-macos-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.50% <75.00%> (?)
encryption-crypt_shared-macos-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.35% <75.00%> (?)
encryption-crypt_shared-rhel8-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.73% <75.00%> (?)
encryption-crypt_shared-rhel8-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.42% <75.00%> (?)
encryption-crypt_shared-rhel8-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.34% <75.00%> (?)
encryption-crypt_shared-win64-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.59% <75.00%> (?)
encryption-crypt_shared-win64-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.54% <75.00%> (?)
encryption-crypt_shared-win64-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.39% <75.00%> (?)
encryption-macos-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.72% <75.00%> (?)
encryption-macos-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.56% <75.00%> (?)
encryption-macos-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.36% <75.00%> (?)
encryption-pyopenssl-rhel8-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 53.38% <75.00%> (?)
encryption-pyopenssl-rhel8-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 55.11% <75.00%> (?)
encryption-pyopenssl-rhel8-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 55.03% <75.00%> (?)
encryption-rhel8-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.72% <75.00%> (?)
encryption-rhel8-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.41% <75.00%> (?)
encryption-rhel8-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.35% <75.00%> (?)
encryption-win64-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.60% <75.00%> (?)
encryption-win64-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.52% <75.00%> (?)
encryption-win64-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.41% <75.00%> (?)
load-balancer-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 48.39% <100.00%> (?)
mongodb-latest-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 61.48% <50.00%> (+<0.01%) ⬆️
mongodb-latest-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 59.35% <50.00%> (-0.01%) ⬇️
mongodb-latest-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 59.51% <50.00%> (ø)
mongodb-latest-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 59.60% <50.00%> (ø)
mongodb-latest-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 61.41% <50.00%> (-0.01%) ⬇️
mongodb-rapid-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 61.48% <50.00%> (+<0.01%) ⬆️
mongodb-rapid-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 59.35% <50.00%> (ø)
mongodb-rapid-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 59.50% <50.00%> (ø)
mongodb-rapid-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 59.61% <50.00%> (+0.01%) ⬆️
mongodb-rapid-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 61.41% <50.00%> (-0.03%) ⬇️
mongodb-v4.2-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 57.19% <50.00%> (+0.01%) ⬆️
mongodb-v4.2-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 55.62% <50.00%> (+0.01%) ⬆️
mongodb-v4.2-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 57.06% <50.00%> (ø)
mongodb-v4.2-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 57.17% <50.00%> (ø)
mongodb-v4.2-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 57.35% <50.00%> (ø)
mongodb-v4.4-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 59.56% <50.00%> (-0.01%) ⬇️
mongodb-v4.4-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 57.58% <50.00%> (-0.01%) ⬇️
mongodb-v4.4-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 57.57% <50.00%> (ø)
mongodb-v4.4-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 57.65% <50.00%> (ø)
mongodb-v4.4-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 59.37% <50.00%> (+0.02%) ⬆️
mongodb-v5.0-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 59.76% <50.00%> (-0.01%) ⬇️
mongodb-v5.0-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 57.73% <50.00%> (-0.01%) ⬇️
mongodb-v5.0-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 57.77% <50.00%> (ø)
mongodb-v5.0-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 57.88% <50.00%> (ø)
mongodb-v5.0-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 59.58% <50.00%> (-0.01%) ⬇️
mongodb-v6.0-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 59.77% <50.00%> (ø)
mongodb-v6.0-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 57.72% <50.00%> (-0.03%) ⬇️
mongodb-v6.0-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 57.79% <50.00%> (ø)
mongodb-v6.0-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 57.93% <50.00%> (ø)
mongodb-v6.0-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 59.75% <50.00%> (+<0.01%) ⬆️
mongodb-v7.0-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 59.81% <50.00%> (+<0.01%) ⬆️
mongodb-v7.0-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 57.75% <50.00%> (+0.01%) ⬆️
mongodb-v7.0-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 57.84% <50.00%> (ø)
mongodb-v7.0-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 57.93% <50.00%> (ø)
mongodb-v7.0-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 59.72% <50.00%> (-0.03%) ⬇️
mongodb-v8.0-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 61.47% <50.00%> (-0.01%) ⬇️
mongodb-v8.0-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 59.34% <50.00%> (+<0.01%) ⬆️
mongodb-v8.0-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 59.50% <50.00%> (ø)
mongodb-v8.0-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 59.59% <50.00%> (ø)
mongodb-v8.0-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 61.42% <50.00%> (+0.01%) ⬆️
no-c-ext-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 60.55% <50.00%> (+<0.01%) ⬆️
no-c-ext-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 62.71% <50.00%> (+0.02%) ⬆️
no-c-ext-rhel8-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 62.29% <50.00%> (-0.01%) ⬇️
no-c-ext-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 60.20% <50.00%> (-0.03%) ⬇️
ocsp-rhel8-test-ocsp-ecdsa-valid-cert-server-staples-latest-python3.14-cov 34.22% <0.00%> (+0.01%) ⬆️
ocsp-rhel8-test-ocsp-rsa-valid-cert-server-staples-latest-python3.14-cov 34.22% <0.00%> (+0.01%) ⬆️
pyopenssl-macos-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.75% <50.00%> (?)
pyopenssl-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.72% <50.00%> (?)
pyopenssl-win64-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.67% <50.00%> (?)
stable-api-accept-v2-rhel8-auth-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.34% <50.00%> (-0.01%) ⬇️
stable-api-accept-v2-rhel8-auth-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.00% <50.00%> (-0.02%) ⬇️
stable-api-require-v1-rhel8-auth-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.31% <50.00%> (ø)
stable-api-require-v1-rhel8-auth-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 60.95% <50.00%> (ø)
stable-api-require-v1-rhel8-auth-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 58.97% <50.00%> (+<0.01%) ⬆️
storage-inmemory-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.36% <50.00%> (+0.02%) ⬆️
storage-inmemory-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 58.98% <50.00%> (-0.04%) ⬇️
test-macos-arm64-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.31% <50.00%> (-0.02%) ⬇️
test-macos-arm64-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.48% <50.00%> (-0.04%) ⬇️
test-macos-arm64-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.08% <50.00%> (-0.01%) ⬇️
test-macos-arm64-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 58.98% <50.00%> (+0.01%) ⬆️
test-macos-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.33% <50.00%> (+0.01%) ⬆️
test-macos-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.45% <50.00%> (-0.03%) ⬇️
test-macos-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.09% <50.00%> (+<0.01%) ⬆️
test-macos-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 58.99% <50.00%> (+<0.01%) ⬆️
test-numpy-macos-arm64-test-numpy-python3.14-python3.14-cov 32.61% <0.00%> (-0.02%) ⬇️
test-numpy-macos-test-numpy-python3.14-python3.14-cov 32.62% <0.00%> (+0.01%) ⬆️
test-numpy-rhel8-test-numpy-python3.14-python3.14-cov 32.61% <0.00%> (ø)
test-numpy-win32-test-numpy-python3.14-python3.14-cov 32.58% <0.00%> (-0.01%) ⬇️
test-numpy-win64-test-numpy-python3.14-python3.14-cov 32.59% <0.00%> (ø)
test-win32-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.21% <50.00%> (+<0.01%) ⬆️
test-win32-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.43% <50.00%> (-0.01%) ⬇️
test-win32-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.07% <50.00%> (ø)
test-win32-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 58.87% <50.00%> (ø)
test-win64-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.20% <50.00%> (+<0.01%) ⬆️
test-win64-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.43% <50.00%> (-0.01%) ⬇️
test-win64-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.09% <50.00%> (+0.02%) ⬆️
test-win64-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 58.87% <50.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@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