Repository navigation
Conversation
When the scope of an event loop ends, collect the tasks that are still pending and emit a RuntimeWarning that lists them once the runner is closed. Users can turn the warning into an error with pytest's warning filters. Tasks asyncio creates to close abandoned async generators are left out. Closes pytest-dev#1044
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1630 +/- ##
==========================================
+ Coverage 94.86% 94.92% +0.06%
==========================================
Files 2 2
Lines 506 512 +6
Branches 60 61 +1
==========================================
+ Hits 480 486 +6
Misses 21 21
Partials 5 5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Thank you for trying to contribute. The implementation isn't something we can merge as it stands. Skipping tasks whose coroutine type is named This PR was also opened within a minute of several PRs to unrelated projects, which suggests an automated contribution. To protect limited maintainer time and attention, I am closing this PR. Please reach out if there is anything unclear about our AI policy. |
closes #1044
this follows the approach from #1044 (comment). when the scope of an event loop ends, the scoped runner now collects the tasks from
asyncio.all_tasks()that are still pending and emits aRuntimeWarningthat lists them. the tasks are collected before the runner closes and the warning is raised after, so they still get cancelled when the warning is turned into an error (filterwarnings = error:Tasks were still pending:RuntimeWarning, which I also added to the concepts docs)tasks that asyncio creates to close abandoned async generators are left out, so
test_event_loop_fixture_handles_unclosed_async_genstill sees no warning.test_task_is_cancelled_when_abandoned_by_testleaves a task behind on purpose, so it now expects the warningthe warning is on by default, as in the comment. happy to put it behind an ini option instead if you think it's too noisy for suites that use
filterwarnings = errortests: a leaking test next to a clean one (function scope), the same leak in class, module, package and session loops, and the warning turned into an error through
filterwarnings.tox -e py313,pyright,docspasses locally (293 passed, 3 skipped), pre-commit is clean, and the new tests also pass on 3.11 and 3.14