Repository navigation
Conversation
# Conflicts: # .evergreen/scripts/setup_tests.py # test/test_bson.py # test/test_message.py
# Conflicts: # .evergreen/scripts/setup_tests.py # .evergreen/scripts/test_generate_config.py
There was a problem hiding this comment.
🟡 Changes recommended
Shutdown joins can block topology updates and server selection, while tests mishandle unstarted RTT executors and fail to verify still-lazy buffers after garbage collection.
3 open issues
What changed in this PR
Adds ThreadSanitizer coverage for PyMongo’s C extensions and addresses a monitor shutdown race exposed by concurrent testing.
Changes:
- Adds a free-threaded ThreadSanitizer CI task alongside ASan/UBSan, with shared setup and local-run documentation.
- Introduces bounded monitor joins and shutdown regression tests.
- Adds concurrent BSON reads, buffer-lifetime checks, and batched-message builder tests.
| File | Description |
|---|---|
test/test_raw_bson_shared.py |
Adds BSON concurrency, buffer-lifetime, and message-builder tests. |
test/test_monitor.py |
Adds generated synchronous shutdown and join-budget tests. |
test/asynchronous/test_monitor.py |
Adds monitor shutdown and join-budget regression tests. |
pymongo/synchronous/topology.py |
Generates synchronous monitor tracking and cleanup changes. |
pymongo/synchronous/monitor.py |
Generates bounded monitor joins during shutdown. |
pymongo/periodic_executor.py |
Allows fractional join timeouts. |
pymongo/asynchronous/topology.py |
Tracks closed monitors and joins them during cleanup. |
pymongo/asynchronous/monitor.py |
Adds bounded joins and shared heartbeat/RTT timeout budgets. |
doc/changelog.rst |
Documents the monitor shutdown fix. |
CONTRIBUTING.md |
Documents local ThreadSanitizer runs. |
.evergreen/tsan-suppressions.txt |
Adds a CPython memoryview race suppression. |
.evergreen/scripts/utils.py |
Defines the ThreadSanitizer interpreter path. |
.evergreen/scripts/test_generate_config.py |
Checks both sanitizer tasks and their variant wiring. |
.evergreen/scripts/setup_tests.py |
Delegates sanitizer setup to the shared module. |
.evergreen/scripts/setup_sanitizer.py |
Configures sanitizer builds, runtimes, and test selection. |
.evergreen/scripts/generate_config.py |
Defines separate ASan and TSan tasks and their host. |
.evergreen/generated_configs/variants.yml |
Updates the sanitizer variant’s execution host. |
.evergreen/generated_configs/tasks.yml |
Generates separate ASan and TSan task definitions. |
Review effort: Balanced
There was a problem hiding this comment.
🟡 Changes recommended
Cancellation during server shutdown can still corrupt topology bookkeeping, and monitor lifecycle changes introduce mock incompatibility, incomplete SRV shutdown, and delayed socket cleanup.
4 open issues
3 resolved since last review
Review effort: Balanced
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved monitor-cleanup races can lose pending cleanup on cancellation or let shutdown return before it finishes, while joining the SRV monitor under the topology lock can stall server selection.
3 open issues
4 resolved since last review
Review effort: Balanced
There was a problem hiding this comment.
🟡 Changes recommended
Cleanup uses an incompatible lock that breaks synchronous heartbeats and client closure, and shutdown can hold the cleanup lock while joining a monitor that needs it, causing timeouts and leaving the monitor alive.
2 open issues
3 resolved since last review
Review effort: Balanced
# Conflicts: # doc/changelog.rst
There was a problem hiding this comment.
🟡 Changes recommended
The generated synchronous test still runs on Windows, where unavailable lock tracking makes its membership assertion fail; the source skip condition must check for os.register_at_fork before regenerating the test.
1 open issue
3 resolved since last review
Review effort: Balanced
There was a problem hiding this comment.
🔵 Needs a closer look
Human assessment is needed because unpublished findings identify a critical cancellation-related pool leak and a test that does not exercise the claimed fork-reset behavior.
No open issues 🎉
1 resolved since last review
Review effort: Balanced
There was a problem hiding this comment.
🟡 Changes recommended
The test resets all tracked PyMongo locks while worker threads are running, risking lost mutual exclusion or unlocked-lock errors; it must run in an isolated forked child with a bounded wait, and the generated test must be updated.
1 open issue
Review effort: Balanced
There was a problem hiding this comment.
🔵 Needs a closer look
Human assessment is needed because the shared polling interval increases application-read wakeups and async-cancellation timer activity tenfold per waiting connection, creating broader performance risks.
No open issues 🎉
Review effort: Balanced
This is true in the sense that blocked reads poll at 20Hz instead of 2Hz. The tradeoff between polling and shutdown latency seems acceptable. I measured cost, using a socketpair in the
The cost applies only while a read is blocked on the server. Idle checked-out connections and promptly served reads poll zero times. At 0.5s, a monitor's in-flight streamed hello checked for cancellation only at a poll boundary, so each joined monitor cost 0.4 to 0.5s: |


PYTHON-6168
Changes in this PR
Adds a ThreadSanitizer task to the sanitizer suite. The concurrent tests it needs surfaced a shutdown race (PYTHON-1634): monitor threads could still read the socket after
MongoClient.close()returned, so close now joins them first.MongoClient.close()now joins its monitor threads with a bounded timeout before returning, in both the sync and async drivers.test-sanitizer-tsanEvergreen task runs the sanitizer suite under a TSan-instrumented, free-threaded CPython with halt-on-error, alongside the existing ASan task; the contributing guide documents local runs.RawBSONDocumentreads, buffer lifetime after the source bytes are dropped, and concurrent batched message building in the C extension.Test Plan
test/asynchronous/test_monitor.pyassert thatclose()leaves no live monitor threads or tasks. They need a live server.test_bsonandtest_messagesuites pass (162 tests), andjust synchroreports no drift.Checklist
Checklist for Author
Checklist for Reviewer