microsoft / microsoft/vscode-python-debugger

VS Code + debugpy + FastAPI: debugpy socket closed (Bad file descriptor)

Open
#809 1 comment 3 reactions 1 assignee View on GitHub

@eleanorjboyd is already working on this.

Since Sep 16, 2025.

triage-needed
Dominant language
TypeScript
Stars
180
Forks
126
Avg merge
2d 3h
Merged PRs (30d)
3

Description

Environment:

  • OS: Microsoft Windows 11 Pro (10.0.22631)
  • WSL: Ubuntu 24.04.2 LTS, WSL 2.4.13.0, kernel 5.15.167.4-1
  • Execution: VS Code Remote-WSL (not SSH)
  • VS Code extension versions:
    • Python (ms-python.debugpy): 2025.14.0
    • Debugger (ms-python.debugpy): 2025.10.0

I’m hitting a repeated OSError: [Errno 9] Bad file descriptor when debugging a Python FastAPI API in VS Code. The traces show failures in pydevd_comm.py on sendall, followed by errors in close(), plus process being replaced timed-out messages. As I understand it, debugpy (the VS Code Python debug adapter) uses pydevd as its engine and they talk to the client over a TCP socket. This socket is getting closed while one thread is still sending data, and during teardown, another thread closes it again, hence the repeated stack traces. I've read that this usually happens due to server autoreload, multiprocessing without subProcess enabled, or Remote-SSH drops.

I have tried:

  • I have confirmed that the debugger is the cause, since when the application is running from the terminal (without the debugger) and a task is completed, no error occurs.
  • I removed the --reload flag from the uvicorn args in launch.json, so it runs without automatic reload while debugging.
  • Launched under debugpy from the terminal and attached from VS Code:
    python -m debugpy --listen 127.0.0.1:5678 --wait-for-client -m uvicorn main:app --host 0.0.0.0 --port 7000
    The issue still reproduces.
  • I tried updating the Python debugger extension, uninstalling and reinstalling, but VS Code reports it’s already the latest.
  • Setting "subProcess": false in launch.json makes the program finish cleanly, but no breakpoints hit, effectively behaving like a non-debug run.

Error trace:

Traceback (most recent call last):
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 422, in _on_run
cmd.send(self.sock)
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_net_command.py", line 111, in send
sock.sendall(("Content-Length: %s\r\n\r\n" % len(as_bytes)).encode("ascii"))
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_daemon_thread.py", line 53, in run
self._on_run()
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 432, in _on_run
self.py_db.dispose_and_kill_all_pydevd_threads()
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/pydevd.py", line 2558, in dispose_and_kill_all_pydevd_threads
self._client_socket.close()
File "/home/daniel/.pyenv/versions/3.10.17/lib/python3.10/socket.py", line 502, in close
self._real_close()
File "/home/daniel/.pyenv/versions/3.10.17/lib/python3.10/socket.py", line 496, in _real_close
_ss.close(self)
OSError: [Errno 9] Bad file descriptor
Traceback (most recent call last):
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 422, in _on_run
cmd.send(self.sock)
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_net_command.py", line 111, in send
sock.sendall(("Content-Length: %s\r\n\r\n" % len(as_bytes)).encode("ascii"))
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_daemon_thread.py", line 53, in run
self._on_run()
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 432, in _on_run
self.py_db.dispose_and_kill_all_pydevd_threads()
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/pydevd.py", line 2558, in dispose_and_kill_all_pydevd_threads
self._client_socket.close()
File "/home/daniel/.pyenv/versions/3.10.17/lib/python3.10/socket.py", line 502, in close
self._real_close()
File "/home/daniel/.pyenv/versions/3.10.17/lib/python3.10/socket.py", line 496, in _real_close
_ss.close(self)
OSError: [Errno 9] Bad file descriptor
Traceback (most recent call last):
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 422, in _on_run
cmd.send(self.sock)
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_net_command.py", line 111, in send
sock.sendall(("Content-Length: %s\r\n\r\n" % len(as_bytes)).encode("ascii"))
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_daemon_thread.py", line 53, in run
self._on_run()
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 432, in _on_run
self.py_db.dispose_and_kill_all_pydevd_threads()
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/pydevd.py", line 2558, in dispose_and_kill_all_pydevd_threads
self._client_socket.close()
File "/home/daniel/.pyenv/versions/3.10.17/lib/python3.10/socket.py", line 502, in close
self._real_close()
File "/home/daniel/.pyenv/versions/3.10.17/lib/python3.10/socket.py", line 496, in _real_close
_ss.close(self)
OSError: [Errno 9] Bad file descriptor
Traceback (most recent call last):
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 422, in _on_run
cmd.send(self.sock)
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_net_command.py", line 111, in send
sock.sendall(("Content-Length: %s\r\n\r\n" % len(as_bytes)).encode("ascii"))
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_daemon_thread.py", line 53, in run
self._on_run()
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 432, in _on_run
self.py_db.dispose_and_kill_all_pydevd_threads()
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/pydevd.py", line 2558, in dispose_and_kill_all_pydevd_threads
self._client_socket.close()
File "/home/daniel/.pyenv/versions/3.10.17/lib/python3.10/socket.py", line 502, in close
self._real_close()
File "/home/daniel/.pyenv/versions/3.10.17/lib/python3.10/socket.py", line 496, in _real_close
_ss.close(self)
OSError: [Errno 9] Bad file descriptor
Traceback (most recent call last):
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 422, in _on_run
cmd.send(self.sock)
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_net_command.py", line 111, in send
sock.sendall(("Content-Length: %s\r\n\r\n" % len(as_bytes)).encode("ascii"))
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_daemon_thread.py", line 53, in run
self._on_run()
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 432, in _on_run
self.py_db.dispose_and_kill_all_pydevd_threads()
File "/home/daniel/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/pydevd.py", line 2558, in dispose_and_kill_all_pydevd_threads
self._client_socket.close()
File "/home/daniel/.pyenv/versions/3.10.17/lib/python3.10/socket.py", line 502, in close
self._real_close()
File "/home/daniel/.pyenv/versions/3.10.17/lib/python3.10/socket.py", line 496, in _real_close
_ss.close(self)
OSError: [Errno 9] Bad file descriptor
42.45s - pydevd: Sending message related to process being replaced timed-out after 5 seconds
43.14s - pydevd: Sending message related to process being replaced timed-out after 5 seconds
43.74s - pydevd: Sending message related to process being replaced timed-out after 5 seconds
44.34s - pydevd: Sending message related to process being replaced timed-out after 5 seconds
45.01s - pydevd: Sending message related to process being replaced timed-out after 5 seconds

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.