Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Memory leak when debugging `asyncio.create_subprocess_shell` calls

Open
#1,884 0 comments 1 reaction 1 assignee View on GitHub

@judej is already working on this.

Since Apr 1, 2025.

Assessment

This issue has not been assessed yet.

Description

bug needs repro

Environment data

  • debugpy version: 2025.4.1
  • OS and version: MacOS 14.4
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.13.2

Actual behavior

When debugging code that repeatedly calls asyncio.create_subprocess_shell, memory usage increases over time.

import asyncio


async def do_update(cmd: str):
    while True:
        process = await asyncio.create_subprocess_shell(
            cmd,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE,
        )
        stdout, stderr = await process.communicate()

        if process.returncode != 0:
            raise Exception(stderr.decode())

        print(stdout)
        await asyncio.sleep(0.1)


if __name__ == "__main__":
    task = asyncio.run(do_update("ps aux | grep bash"))

Expected behavior

When running the above code directly with python (e.g. no debugger), there is no memory leak. The create_subprocess_shell creates a new thread each time it's called, and some cursory debugging of allocations shows mostly thread related things. My suspicion is that the leak is related to thread creation, and that this case is especially bad because asyncio is creating (and destroying) a new thread for each call.

This issue is very severe: a simple application making repeated shell calls will leak memory >1gb running overnight - I also see idle CPU usage at 100% by this point, which I assume is related to the memory issue, but ofc I'm not totally sure.

Steps to reproduce:

  1. Run example code while debugging with debugpy, watch memory usage of python process.
Dominant language
Python
Stars
2.5k
Forks
202
Avg merge
5d 1h
Merged PRs (30d)
1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/debugpy

All issues in microsoft/debugpy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.