Skip to content

PYTHON-5428 - @flaky calls cleanup between attempts - #3006

Merged
NoahStapp merged 2 commits into
mongodb:mainfrom
NoahStapp:PYTHON-5428
Aug 28, 2026
Merged

PYTHON-5428 - @flaky calls cleanup between attempts#3006
NoahStapp merged 2 commits into
mongodb:mainfrom
NoahStapp:PYTHON-5428

Conversation

@NoahStapp

Copy link
Copy Markdown
Contributor

PYTHON-5428

Changes in this PR

Our @flaky decorator did not call test cleanups between attempts. This resulted in resources such as clients remaining open and interfering with event listening on later retry attempts. This PR adds a cleanup step to @flaky that ensures all registered cleanups are called between attempts.

Test Plan

N/A

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?

Copilot AI lite review requested due to automatic review settings August 25, 2026 15:24
@NoahStapp
NoahStapp requested a review from a team as a code owner August 25, 2026 15:24
@NoahStapp
NoahStapp requested a review from aclark4life August 25, 2026 15:24

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 updates the internal @flaky test decorator used across the PyMongo test suite to reduce cross-attempt interference by running test cleanups between retry attempts (aimed at preventing leaked clients/listeners from impacting subsequent retries). It also updates the stated reason for one flaky SDAM test in both sync and async variants.

Changes:

  • Add a cleanup step to @flaky to run registered unittest.TestCase cleanups between retry attempts.
  • Extend @flaky wrapper logic to locate the active unittest.TestCase instance (unbound “prose” vs bound “unified” execution paths).
  • Update the flaky annotation reason for test_connection_close_does_not_block_other_operations (sync + async).

Reviewed changes

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

File Description
test/utils.py Adds between-attempt cleanup execution to the sync @flaky decorator.
test/asynchronous/utils.py Adds between-attempt cleanup execution to the async @flaky decorator (awaiting async cleanups).
test/test_discovery_and_monitoring.py Updates the flaky reason string for a slow test.
test/asynchronous/test_discovery_and_monitoring.py Updates the flaky reason string for the async variant of the same test.
Suppressed comments (2)

test/utils.py:212

  • depth is recomputed at the start of every retry attempt. This prevents cleanups added by reset_func (e.g., setUp) from being considered part of the next attempt, so they won't be run on subsequent failures and can accumulate across retries.
            passes = 0
            for i in range(max_runs):
                depth = len(method._cleanups) if method is not None else 0
                try:

test/asynchronous/utils.py:212

  • depth is recomputed at the start of every retry attempt. This prevents cleanups added by reset_func (e.g., asyncSetUp) from being considered part of the next attempt, so they won't be run on subsequent failures and can accumulate across retries.
            passes = 0
            for i in range(max_runs):
                depth = len(method._cleanups) if method is not None else 0
                try:

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread test/utils.py
Comment thread test/asynchronous/utils.py
@codecov

codecov Bot commented Aug 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@NoahStapp

Copy link
Copy Markdown
Contributor Author

4.2 failures are expected until #2892 is merged.

@NoahStapp
NoahStapp merged commit 066aeb3 into mongodb:main Aug 28, 2026
88 of 90 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.

3 participants