More screenshots

    Works with the AI Agents you already use

    Claude CodeClaude CodeCursorCursorCodex CLICodex CLIMuseMuseDotsDots+21 more

    Python Leak Hunter

    1

    Find, explain and fix memory leaks in long-running Python services: what accumulates, what keeps it alive, and a test that keeps it fixed.

    Free

    1 useSecurity scanned
    Python Leak Hunter

    Python Leak Hunter

    Example session with this skill installed

    My orderbot service's memory keeps growing with traffic, roughly 50 KB per customer message, until the process gets restarted. Can you find the leak and fix it?

    • Read your context and instructions
    • Compiled the python leak hunter

    Found it: every message subscribes a listener to the module-global event bus and never removes it, so every Turn, with its 50 KB scratch buffer, stays alive for the life of the process. Fixed and verified, with a regression test.

    1. What accumulates

    $ leak-hunter check leak_harness:one_turn --include orderbot. --trace
    Objects accumulating across calls (2 windows x 20 calls):
      orderbot.conversation.Turn  +1 per call  (window 1: +20, window 2: +20)
    Allocation sites still holding memory from the last 20 calls:
      orderbot/conversation.py:12  +48.88 KiB per call  (+40 blocks)
      orderbot/events.py:9  +6.12 KiB per call  (+1358 blocks)
      orderbot/conversation.py:14  +0.3 KiB per call  (+20 blocks)
      orderbot/catalog.py:9  +0.11 KiB per call  (+40 blocks)
      orderbot/conversation.py:16  +0.1 KiB per call  (+20 blocks)
    

    One Turn survives every message, and conversation.py:12 (its scratch buffer) accounts for about 49 KiB per call, which matches the ~50 KB you're seeing.

    2. What keeps it alive

    $ leak-hunter why leak_harness:many_turns orderbot.conversation.Turn
    50 live orderbot.conversation.Turn; traced 5:
    5 of 5 held by:
      module orderbot.events, global '_listeners'
        -> dict['product_viewed']
        -> list[49]
        -> bound method Turn.record.__self__
        -> orderbot.conversation.Turn  <- the object being explained
    

    This is a known pattern: a listener registered per request on a module-global registry and never removed. It also gets worse with traffic, because every new event is delivered to every old Turn.

    3. The fix

     def handle_message(text: str, sku: str = "A-1") -> str:
         turn = Turn(next(_ids), text)
         events.subscribe("product_viewed", turn.record)
    -    item = catalog.product(sku)
    -    events.emit("product_viewed", sku=sku)
    +    try:
    +        item = catalog.product(sku)
    +        events.emit("product_viewed", sku=sku)
    +    finally:
    +        events.unsubscribe("product_viewed", turn.record)
    

    plus an unsubscribe() in orderbot/events.py.

    4. Verified

    $ leak-hunter check leak_harness:one_turn --include orderbot. --trace --iterations 300
    No object type grew steadily over 2 x 300 calls.
    

    The @lru_cache(maxsize=256) on catalog.product also showed small growth at first. It's bounded: past 256 SKUs it stops growing, so it isn't the leak.

    5. Regression test

    def test_handle_message_does_not_leak(leak_check):
        leak_check(one_turn, include=["orderbot."], iterations=30)
    

    It fails on the old code and passes on the fixed code (2 passed), so the leak can't quietly come back.

    Connects securely to your tools. The creator never sees your data.

    About this skill

    Leaks are found by measurement, not by reading code. This skill walks your agent through a real investigation, step by step:

    1. Confirm it's a leak, not allocator fragmentation or native memory, and measure the growth per request.
    2. Find what accumulates: the object types that grow with each unit of work, with allocation sites and size per call.
    3. Find what keeps it alive: the reference chain back to a module global, a running function, or a C extension holding it outside Python.
    4. Match the chain to a known pattern (11 common culprits, from per-request clients and unbounded caches to listeners that are never removed and contextvars captured by native code) and apply the fix.
    5. Lock it in with a pytest regression test that fails if the leak comes back.

    It uses the open-source python-leak-hunter package from PyPI, which the agent installs into your project. Tested against a real gRPC leak, where it traced the cause to a context held by gRPC's C core with no hints.

    Works with Claude Code, Codex, Cursor and other agents that support skills.

    Source, docs and issues: https://github.com/tariqamarneh/python-leak-hunter

    The story behind it: https://www.tariqamarneh.com/writing/grpc-contextvars-memory-leak

    Needs the python-leak-hunter package installed in the same environment as the code under investigation, and a small harness function that runs one unit of work (one request, message or job). The allocator check that separates real leaks from fragmentation needs Linux with glibc 2.33+; on macOS or Alpine (musl) it is skipped. Memory allocated by C extensions shows up as "held outside Python"; tracing inside native code needs memray or jemalloc, which the skill explains. Tested on Linux and macOS with Python 3.10–3.14; Windows is untested.

    How to use

    1. 1

      Connect your agent to the Agensi MCP

      Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup

    2. 2

      Sign in to Agensi

      Your agent shows a sign-in link. Sign in and click Allow.

    3. 3

      Ask for what you need

      Your agent loads this skill and uses it on your task. It always asks before any payment.

    Reviews

    No reviews yet

    Be one of the first to try it. Every listed skill goes through the trust checks below.

    Security scanned

    Checked by our 8-point scan before listing

    1 use

    Times used to date

    Free forever

    No account required to browse

    Trust & safety

    Security scan

    Security scan passed 27 days ago

    • Free with an Agensi account

    Requires

    Pypi
    Python 3.10+
    pip or uv (Python tool runner)
    python-leak-hunter
    Python

    Permissions

    Terminal / Shell
    Read Files
    Write Files
    Network Access

    Allowed Hosts

    pypi.org

    File Scopes

    **/*.py
    tests/**

    Terminal: installs the open-source python-leak-hunter package and runs its CLI and pytest against your own code. Network: only to download that package from PyPI; no data is sent anywhere. Read/write files: reads the code under investigation, applies the fix you approve, and adds a regression test. No credentials or environment variables are used.

    Listed27 days ago

    What's inside

    Frequently Asked Questions

    It runs where you can import your code: locally, in CI, or in a staging container. For a live process, the skill shows how to sample memory over time; the object tracing runs on a reproduction of the workload.

    No. You give it a function that does one unit of work, and it reports which object types grow and what keeps them alive. That points to the culprit.

    New in LLM Ops