Skip to main content

Detect leaked asyncio tasks and threads in Python. Inspired by Go's goleak

Project description

pyleak

Detect leaked asyncio tasks and threads in Python. Inspired by Go's goleak.

Installation

pip install pyleak

Usage

Asyncio Tasks

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

Threads

Context Manager

import threading
from pyleak import no_thread_leaks

def main():
    with no_thread_leaks():
        # This will detect any threads that aren't properly joined
        threading.Thread(target=lambda: time.sleep(10)).start()

main()
python -W always script.py
# ResourceWarning: Detected 1 leaked threads: ['Thread-1']

Decorator

from pyleak import no_thread_leaks

@no_thread_leaks()
def main():
    threading.Thread(target=lambda: time.sleep(10)).start()

main()

Actions

Note: Cancelling threads is not supported. It will only warn about them.

from pyleak import no_thread_leaks

# Warn (default) - issues a ResourceWarning
with no_thread_leaks(action="warn"):
    pass

# Log - writes to logger
with no_thread_leaks(action="log"):
    pass


# Raise - raises ThreadLeakError
with no_thread_leaks(action="raise"):
    pass

Name Filtering

Only detect tasks matching specific names:

import re
from pyleak import no_task_leaks

# 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 and threads in tests:

import pytest
from pyleak import no_task_leaks, no_thread_leaks

@pytest.mark.asyncio
async def test_no_leaked_tasks():
    async with no_task_leaks(action="raise"):
        await my_async_function()


def test_no_leaked_threads():
    with no_thread_leaks(action="raise"):
        threading.Thread(target=my_function).start()

More examples can be found in the asyncio tasks tests and thread tests.

Project details


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.2.tar.gz (29.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyleak-0.1.2-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file pyleak-0.1.2.tar.gz.

File metadata

  • Download URL: pyleak-0.1.2.tar.gz
  • Upload date:
  • Size: 29.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.8

File hashes

Hashes for pyleak-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5160282e926aef4b8f5925ca18d0f509f1dfda9baf26b9e8cfc2e7f2e574fdce
MD5 fb8ed49590249d60aa2bcf823952ae80
BLAKE2b-256 f7c52ef6a1b30fe1436c4b8f152f825648f396f3c262861479b6d271a3bb03c6

See more details on using hashes here.

File details

Details for the file pyleak-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: pyleak-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.8

File hashes

Hashes for pyleak-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3aa9e24080893cbf10280f703bff690d7e502acd01f1943aefae56e1cdf0afbe
MD5 fdb1ed73ecc9148f390480601d53c52e
BLAKE2b-256 edd1b5ad3544421a228cd55f0d9e953e64759ad718c1bb47f7b83e81eb7b5837

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page