pyleak
Detect leaked asyncio tasks, threads, and event loop blocking in Python. Inspired by Go's goleak
What it is and what it does
pyleak is a testing and debugging tool that catches resource leaks in Python async code. It provides three main detectors—no_task_leaks, no_thread_leaks, and no_event_loop_blocking—each usable as a context manager or decorator. When a leak or blocking event is detected, the tool reports it with detailed stack traces showing where the resource was created and where it is currently executing. You configure the response via an action parameter: warn (default, issues ResourceWarning), log (writes to logger), cancel (for tasks only), or raise (throws an exception).
The package is inspired by Go's goleak and targets developers writing asyncio-heavy applications who need to catch unintended resource leaks during testing. It depends on exceptiongroup and sniffio, both lightweight libraries. The tool supports name filtering by exact match or regex pattern for tasks and threads, and optional creation-stack tracking (via monkey-patching) for deeper debugging. It requires Python 3.9 or later.
Use it for:
- Detect unintended asyncio tasks left running after a test or function completes
- Catch threads spawned but never properly joined in sync code paths
- Identify blocking calls (e.g., time.sleep) that freeze the event loop during async execution
- Validate cleanup in async context managers and fixture teardown
- Debug resource leaks in long-running async services before deployment
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Detects leaked asyncio tasks, threads, and event loop blocking in Python code using context managers or decorators, reporting violations with stack traces.
Yes, if you write asyncio or threaded code and want automated leak detection during testing. The low install friction and clear API make it straightforward to add to test suites. However, verify the license terms first (currently unclear in the metadata), and note that the project is aging—last release was 197 days ago. Not suitable for production monitoring; use it as a development and testing tool.
Install
pyleak on PyPI
pip
pip install pyleakuv
uv add pyleakpoetry
poetry add pyleakInstalling pyleak
Before you install
Low install friction with only two lightweight runtime dependencies (exceptiongroup, sniffio). Maintenance status is aging—last release was 197 days ago, though the repository remains active with 403 stars.
License in practice
License status is unclear; no SPDX identifier or raw license text is available in the package metadata. Verify the actual license before use in proprietary or restricted-license projects.
Quickstart
pip install pyleak
import asyncio
from pyleak import no_task_leaks
async def main():
async with no_task_leaks():
asyncio.create_task(asyncio.sleep(10))
await asyncio.sleep(0.1)
asyncio.run(main())
Requires Python 3.9 or later. Event loop blocking detection only works in async context; thread detection only in sync context.
Verify before relying
- Whether the package is suitable for production use or primarily for testing and development
- Actual license terms and any attribution or usage restrictions
- Performance overhead of leak detection in real-world async codebases
Package facts
| License | not declared (unclear) |
| Python support | supports the current Python release (>=3.9) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — exceptiongroup, sniffio |
| Maintenance | aging — 197 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 562,550/month — #5,987 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pyleak-0.2.0-py3-none-any.whl
Keywords: async, asyncio, goleak, leak, pytest, tasks, testing, threads
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
aiocopDetects and logs blocking I/O and CPU calls in…
permissive · top 15,000 on PyPI
aiodebugaiodebug provides monitoring and testing…
permissive · top 15,000 on PyPI
pytest-threadleakA pytest plugin that detects when tests leak…
permissive · top 15,000 on PyPI
blockbusterBlockbuster detects and prevents blocking calls…
permissive · top 5,000 on PyPI
nest-asyncio2Patches asyncio to allow nested event loops,…
permissive · top 1,000 on PyPI
mozleakExtracts memory leaks from leak log files,…
copyleft · top 15,000 on PyPI
zthreadingProvides event-driven communication and task…
unclear · top 15,000 on PyPI
aiorunProvides a simplified entry point for asyncio…
permissive · top 15,000 on PyPI
unsyncDecorator-based library that runs async…
permissive · top 15,000 on PyPI
aiomonitorAdds a concurrent monitor and interactive…
permissive · top 15,000 on PyPI