Memory leak when debugging `asyncio.create_subprocess_shell` calls
@judej is already working on this.
Since Apr 1, 2025.
Assessment
This issue has not been assessed yet.
Description
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:
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from microsoft/debugpy
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
All issues in microsoft/debugpy
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
learningequality/ricecooker#747 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
BSData/horus-heresy-3rd-edition#3171 ·
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
run-llama/llama_index#23199 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
KhronosGroup/glTF-Blender-IO#2769 ·