Registry / testing / pyleak

pyleak

JSON →
library0.2.0pypypi✓ verified 85d ago

Pyleak is a Python library inspired by Go's goleak, designed to detect leaked asyncio tasks, threads, and event loop blocking in asynchronous Python applications. The current version is 0.2.0, with frequent minor and patch releases, indicating active development.

pip install pyleak
INSTALL
IMPORT
SIG · PYLEAK
P
pyleak
testingpythonv0.2.0
Install
1.7s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 18.5MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.7s · import 0.000s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

no_task_leaks
from pyleak import no_task_leaks
from pyleak import LeakDetector
no_thread_leaks
from pyleak import no_thread_leaks
no_event_loop_blocking
from pyleak import no_event_loop_blocking

This example demonstrates how to use `LeakDetector` to identify unawaited or long-running asyncio tasks that are not properly shut down, which pyleak considers a leak. Running this code will raise an `AssertionError` at the end of the `async with LeakDetector():` block.

import asyncio from pyleak import LeakDetector async def create_leak(): # A task that runs forever, simulating a leak async def forever_task(): while True: await asyncio.sleep(100) asyncio.create_task(forever_task()) async def main(): async with LeakDetector(): await create_leak() print("This will run, but a leak was created.") # LeakDetector exits, will raise AssertionError if leaks are found print("LeakDetector exited, no assertion was raised (this line won't be reached if a leak is present).") if __name__ == "__main__": # To see the AssertionError from the leak: # asyncio.run(main()) # To avoid the assertion for demonstration: try: asyncio.run(main()) except AssertionError as e: print(f"Caught expected leak: {e}")
Debug
Known issues
breaking`no_event_loop_blocking` API changed from decorator/function to an async context manager.
fix
Update usage from `@no_event_loop_blocking` or `no_event_loop_blocking()` to `async with no_event_loop_blocking():`.
affects: >=0.2.0
gotchaFor best results and detailed stack traces when detecting asyncio leaks or blocking, it is recommended to enable asyncio debug mode.
fix
Run your application with `asyncio.run(main(), debug=True)` or set the `PYTHONASYNCIODEBUG` environment variable to `1`.
affects: All versions
gotchaUsing `LeakDetector` without properly shutting down or cancelling all expected long-running background tasks will result in false positives (AssertionError).
fix
Ensure all asyncio tasks, threads, and resources are correctly cleaned up or awaited before the `LeakDetector` context exits. For intentional long-lived tasks, consider using `pyleak.set_allow_extra_tasks` for specific test scenarios, though this should be used cautiously.
affects: All versions
gotcha`CombinedLeakDetector` in versions prior to 0.1.16 could lead to false positive warnings.
fix
Upgrade pyleak to version 0.1.16 or newer to benefit from fixes addressing false positives in `CombinedLeakDetector`.
affects: <0.1.16
Upgrade
Version history
0.2.0latest on PyPI · released Jan 29, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
19 hits · last 30 days
node
16
OpenAI (training)
2
Amazon
1
Resources
pyleak — pip install pyleak · libregistry