Skip to content

PYTHON-6168 Add thread sanitizer testing support - #3235

Draft
blink1073 wants to merge 30 commits into
mongodb:mainfrom
blink1073:PYTHON-6168
Draft

blink1073 wants to merge 30 commits into
mongodb:mainfrom
blink1073:PYTHON-6168

Conversation

@blink1073

@blink1073 blink1073 commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

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.
  • A new test-sanitizer-tsan Evergreen 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.
  • New concurrency tests cover shared zero-copy RawBSONDocument reads, buffer lifetime after the source bytes are dropped, and concurrent batched message building in the C extension.

Test Plan

  • New sync and async tests in test/asynchronous/test_monitor.py assert that close() leaves no live monitor threads or tasks. They need a live server.
  • CI runs the new concurrency tests under both sanitizer tasks on every PR.
  • Verified locally: the test_bson and test_message suites pass (162 tests), and just synchro reports no drift.

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?

@blink1073 blink1073 changed the title PYTHON-6168 Add a thread sanitizer test PYTHON-6168 Add thread sanitizer testing support Nov 19, 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.

🟡 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

Comment thread pymongo/asynchronous/monitor.py Resolved
Comment thread test/asynchronous/test_monitor.py Resolved
Comment thread test/test_raw_bson_shared.py Resolved

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

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

Comment thread pymongo/asynchronous/topology.py Resolved
Comment thread pymongo/asynchronous/server.py Resolved
Comment thread pymongo/asynchronous/topology.py Resolved
Comment thread pymongo/asynchronous/topology.py Resolved

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

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

Comment thread pymongo/asynchronous/topology.py Resolved
Comment thread pymongo/asynchronous/topology.py Resolved
Comment thread pymongo/asynchronous/topology.py Resolved

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

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

Comment thread pymongo/asynchronous/topology.py Resolved
Comment thread pymongo/asynchronous/topology.py Resolved

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.

🔵 Needs a closer look

Human assessment is needed for a cleanup-lock handoff race that can leave monitors queued and delay socket cleanup.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

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 executor shutdown path can deadlock during garbage collection or after a fork, while reopening a live executor can cause continuous execution without respecting its interval.

3 open issues

Review effort: Balanced

Comment thread pymongo/periodic_executor.py Resolved
Comment thread pymongo/periodic_executor.py Resolved
Comment thread pymongo/periodic_executor.py Resolved

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

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.

🔵 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

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

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.

🔵 Needs a closer look

Human assessment is needed to evaluate shutdown race conditions, unreliable cleanup-test coverage, and polling overhead affecting application connections.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

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.

🔵 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

@blink1073

Copy link
Copy Markdown
Member Author

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.

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 receive_data loop (CPU per blocked connection, per second of blocked time):

_POLL_TIMEOUT sync blocked read async poll task (per connection)
0.05 0.10% of a core 0.011%
0.1 0.05% 0.0055%
0.5 0.005% 0.001%

The cost applies only while a read is blocked on the server. Idle checked-out connections and promptly served reads poll zero times. maxPoolSize (default 100) caps the worst case at about 10% of one core per client.

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: MongoClient.close() took 0.5s. At 0.05, close takes 0.05s.

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.

2 participants