pytest-threadleak
Detects thread leaks
What it is and what it does
pytest-threadleak is a pytest plugin that automatically detects when a test leaves threads running after it completes. It works by checking for leaked threads after each test and failing the test if any are found. You can enable it globally via pytest.ini, per-test using the @pytest.mark.threadleak decorator, or per-module. The plugin also supports excluding specific threads by regex pattern if certain background threads are expected.
The plugin helps catch both test bugs (tests that spawn threads without cleaning them up) and issues in the code being tested (libraries that leak threads). It integrates seamlessly into your pytest workflow and requires no changes to test code beyond optionally adding markers or configuration.
Use it for:
- Catch tests that spawn background threads without properly joining or stopping them before test completion.
- Detect thread leaks in libraries under test that may cause resource exhaustion or test isolation problems.
- Enforce thread cleanup discipline across a test suite by enabling threadleak globally in pytest configuration.
- Exclude known background threads (e.g., connection pools) from leak detection using regex patterns.
- Debug test flakiness caused by threads from previous tests interfering with subsequent tests.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
A pytest plugin that detects when tests leak threads—threads that remain running after a test completes—and fails the test to surface the issue.
Yes, if you need to catch thread leaks in your tests. The plugin is simple, low-friction, and has no known vulnerabilities. However, be aware that maintenance is dormant; verify compatibility with your pytest and Python versions before relying on it in a critical pipeline. For most projects, the benefit of catching thread leaks outweighs the maintenance risk.
Install
pytest-threadleak on PyPI
pip
pip install pytest-threadleakuv
uv add pytest-threadleakpoetry
poetry add pytest-threadleakInstalling pytest-threadleak
Before you install
Low install friction; depends only on pytest. Maintenance is dormant—last release was 2022-07-03, though the repository remains active and has not been archived.
License in practice
MIT license (permissive); you may use, modify, and distribute this software freely with minimal restrictions.
Quickstart
pip install pytest-threadleak
# In your test file or pytest.ini:
# pytest.ini:
# [pytest]
# threadleak = True
# Then run:
# pytest --threadleak your_test.py
Verify before relying
- Whether the plugin works correctly with modern pytest versions given the last release was 2022-07-03.
- Whether thread exclusion patterns (threadleak_exclude) work as documented with current Python threading internals.
- Performance impact when running large test suites with threadleak enabled.
Package facts
| License | MIT (permissive) |
| Python support | not specified |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — pytest |
| Maintenance | dormant — 1,503 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 240,664/month — #8,902 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pytest_threadleak-0.5.0-py3-none-any.whl
Tags
More Testing packages
Pluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
pytestpytest is a testing framework that lets you…
permissive · top 100 on PyPI
virtualenvvirtualenv creates isolated Python environments…
permissive · top 100 on PyPI
coverageCoverage.py measures which lines of Python code…
permissive · top 1,000 on PyPI
pytest-asynciopytest-asyncio is a pytest plugin that enables…
permissive · top 1,000 on PyPI
pytest-json-ctrfA pytest plugin that generates test reports in…
permissive · top 1,000 on PyPI
pytest-loggingA pytest plugin that configures Python logging…
permissive · top 15,000 on PyPI
pyleakDetects leaked asyncio tasks, threads, and…
unclear · top 15,000 on PyPI
pytest-custom-exit-codeA pytest plugin that lets you customize the…
permissive · top 5,000 on PyPI
pytest-run-parallelA pytest plugin that runs test functions…
permissive · top 15,000 on PyPI
pytest-flakefinderA pytest plugin that runs tests multiple times…
permissive · top 5,000 on PyPI
pytest-find-dependenciesA pytest plugin that identifies dependencies…
permissive · top 5,000 on PyPI
pytest-monitorA pytest plugin that automatically measures and…
permissive · top 15,000 on PyPI
pytest-replayA pytest plugin that records test execution…
permissive · top 15,000 on PyPI
pytest-timeoutAborts pytest tests that exceed a specified…
permissive · top 1,000 on PyPI
pytest-raceProvides a pytest fixture that runs test…
permissive · top 15,000 on PyPI