Skip to content

[gevent worker] prevent late monkey-patching in pytest - #468

Open
ayubun wants to merge 1 commit into
mainfrom
ayu/pytest-gevent-collection
Open

[gevent worker] prevent late monkey-patching in pytest#468
ayubun wants to merge 1 commit into
mainfrom
ayu/pytest-gevent-collection

Conversation

@ayubun

@ayubun ayubun commented Aug 25, 2026

Copy link
Copy Markdown
Member

🤖 summary

prevents gevent test modules from monkey-patching an already-running plain pytest process, while preserving the gevent worker's pre-patched integration runner and forced-exit workaround

related issues/tasks

changes made (˶ᵔ ᵕ ᵔ˶)

  • avoid calling gevent.monkey.patch_all() after plain pytest has started
  • tie the forced-exit workaround to pytest starting under the pre-patched gevent runner
  • add subprocess regression coverage for every import-time monkey-patch site

models used

  • anthropic/claude-opus-4-8: ~50%
  • openai/gpt-5.6-sol: ~30%
  • anthropic/claude-haiku-4-5: ~15%
  • anthropic/claude-fable-5: ~5%

testing

  • plain collection completed with 1,286 worker tests
  • the pre-patched runner still collected all 5 discovery tests

Summary by CodeRabbit

  • Bug Fixes

    • Improved test startup handling to prevent failures caused by late concurrency setup.
    • Prevented unnecessary process termination when tests modify concurrency behavior during execution.
  • Tests

    • Added regression coverage confirming representative test modules collect successfully with standard pytest.
    • Verified collection completes without lock-release errors or collection failures.

@coderabbitai

coderabbitai Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: f5f9602d-ded6-47f7-aa05-e2bf8c01c826

📥 Commits

Reviewing files that changed from the base of the PR and between 1cbf04b and 05ce29a.

📒 Files selected for processing (5)
  • osprey_worker/conftest.py
  • osprey_worker/src/osprey/worker/lib/discovery/tests/test_discovery.py
  • osprey_worker/src/osprey/worker/lib/etcd/tests/conftest.py
  • osprey_worker/src/osprey/worker/lib/instruments/tests/test_concurrency.py
  • osprey_worker/tests/test_gevent_collection.py

Included review availability: Your plan provides up to 2 included reviews per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The changes prevent late gevent monkey-patching during plain pytest collection. Session finalization now uses the launch-time socket-patching state. New subprocess tests verify successful collection for representative modules.

Changes

Gevent collection behavior

Layer / File(s) Summary
Launch-time session state
osprey_worker/conftest.py
Records the initial socket-patching state and uses it during pytest_sessionfinish.
Conditional gevent patching
osprey_worker/src/osprey/worker/lib/discovery/tests/test_discovery.py, osprey_worker/src/osprey/worker/lib/etcd/tests/conftest.py, osprey_worker/src/osprey/worker/lib/instruments/tests/test_concurrency.py
Applies gevent monkey-patching only when the socket module is already patched.
Plain collection regression coverage
osprey_worker/tests/test_gevent_collection.py
Runs plain pytest --collect-only in child processes and checks exit status, error output, and collection summaries.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 05ce2

This PR prevents late gevent monkey-patching in plain pytest while preserving the pre-patched worker path; no actionable merge-blocking risk remains beyond normal checks and review.

Suggested reviewers: exbreder

Sequence Diagram(s)

sequenceDiagram
  participant RegressionTest
  participant ChildPython
  participant Pytest
  participant TestModules
  RegressionTest->>ChildPython: Run pytest --collect-only
  ChildPython->>Pytest: Collect representative test module
  Pytest->>TestModules: Import test configuration
  TestModules-->>Pytest: Patch gevent only if socket is already patched
  Pytest-->>RegressionTest: Return collection status and output
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 5 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: preventing late gevent monkey-patching during pytest execution in the worker.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ayu/pytest-gevent-collection

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ayubun ayubun changed the title [worker] make plain pytest collection reliable [gevent worker] prevent late monkey-patching in pytest Aug 25, 2026
@ayubun
ayubun marked this pull request as ready for review August 25, 2026 01:26
@ayubun
ayubun requested review from a team, EXBreder, haileyok and vinaysrao1 as code owners August 25, 2026 01:26

@julietshen julietshen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, tiny nit on a false green if there are zero tests

# markers must be absent, and a healthy run reports the collected count
assert 'cannot release un-acquired lock' not in combined, combined
assert 'error during collection' not in combined, combined
assert 'collected in' in result.stdout, result.stdout

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this will also say this if it's empty, no?

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