Detect leaked asyncio tasks in Python. Inspired by Go's goleak
Project description
pyleak
Detect leaked asyncio tasks in Python. Inspired by Go's goleak.
Installation
pip install pyleak
Usage
Context Manager
# script.py
import asyncio
from pyleak import no_task_leaks
async def main():
async with no_task_leaks():
# This will detect any tasks that aren't properly awaited
asyncio.create_task(asyncio.sleep(10), name="my-task") # This would be flagged
await asyncio.sleep(0.1)
asyncio.run(main())
python -W always script.py
# ResourceWarning: Detected 1 leaked asyncio tasks: ['my-task']
Decorator
@no_task_leaks()
async def test_my_function():
await my_async_function()
# Any leaked tasks will be detected when the function exits
Actions
Choose what happens when leaks are detected:
# Warn (default) - issues a ResourceWarning
async with no_task_leaks(action="warn"):
pass
# Log - writes to logger
async with no_task_leaks(action="log"):
pass
# Cancel - cancels the leaked tasks
async with no_task_leaks(action="cancel"):
pass
# Raise - raises TaskLeakError
async with no_task_leaks(action="raise"):
pass
Name Filtering
Only detect tasks matching specific names:
import re
# Exact match
async with no_task_leaks(name_filter="background-worker"):
pass
# Regex pattern
async with no_task_leaks(name_filter=re.compile(r"worker-\d+")):
pass
Testing
Perfect for catching leaked tasks in tests:
import pytest
from pyleak import no_task_leaks
@no_task_leaks(action="raise")
async def test_no_leaked_tasks():
# Test will fail if any tasks are leaked
await my_function_under_test()
class TestMyApp:
async def test_with_context_manager(self):
async with no_task_leaks(action="raise"):
await my_async_operation()
More examples can be found in the tests.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pyleak-0.1.1.tar.gz
(25.3 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyleak-0.1.1.tar.gz.
File metadata
- Download URL: pyleak-0.1.1.tar.gz
- Upload date:
- Size: 25.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1eaec6813571b2a9841feeac1cc38f8b4985bba752c16eb3bf78be79232117be
|
|
| MD5 |
2cf29405e20a372d82509151a6d8c55a
|
|
| BLAKE2b-256 |
c7794bb296b28a5fe5df852d79bef75c9735398f0f3c067ffe592d751597afe2
|
File details
Details for the file pyleak-0.1.1-py3-none-any.whl.
File metadata
- Download URL: pyleak-0.1.1-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d07d13c869a1e45ae9c92005288abd411a8bacf8be3d4f7e94d38190498c7884
|
|
| MD5 |
d59c97f239929cc1336821940c08443c
|
|
| BLAKE2b-256 |
db882c1c3be520931840c07461733a8af1d683f7e99b2d4409edfe764924fb04
|