Skip to content

Warn about tasks still pending when an event loop scope ends - #1630

Closed
SnakeO wants to merge 1 commit into
pytest-dev:mainfrom
SnakeO:warn-pending-tasks-1044
Closed

SnakeO wants to merge 1 commit into
pytest-dev:mainfrom
SnakeO:warn-pending-tasks-1044

Conversation

@SnakeO

@SnakeO SnakeO commented Nov 19, 2026

Copy link
Copy Markdown

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 a RuntimeWarning that 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_gen still sees no warning. test_task_is_cancelled_when_abandoned_by_test leaves a task behind on purpose, so it now expects the warning

the 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 = error

tests: 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,docs passes locally (293 passed, 3 skipped), pre-commit is clean, and the new tests also pass on 3.11 and 3.14

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

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.92%. Comparing base (599fb32) to head (2a51b3f).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tjkuson

tjkuson commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Thank you for trying to contribute.

The implementation isn't something we can merge as it stands. Skipping tasks whose coroutine type is named "async_generator_athrow" works around a scheduling quirk and it hides real leaks. The warning is reported against whichever test ran last in a particular loop scope, not the test or fixture that leaked the task (a task leaked by a module-scoped fixture can be blamed on a test that doesn't use it).

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.

@tjkuson tjkuson closed this Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow erroring on remaining tasks after test exit as an option

3 participants