Skip to content

Memory leak when debugging asyncio.create_subprocess_shell calls #1884

@scztt

Description

@scztt

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.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingneeds reproIssue has not been reproduced yet

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions