Skip to content

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

@codeyel

Description

@codeyel

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

Metadata

Metadata

Assignees

Labels

triage-neededNeeds assignment to the proper sub-team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions