microsoft / microsoft/vscode-python-debugger

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

未关闭
#809 1 条评论 3 个 reaction 已指派 1 人 在 GitHub 查看

@eleanorjboyd 已经在做这个了。

开始于 2025年9月16日。

triage-needed
主要语言
TypeScript
星标
180
派生
126
平均合并
2 天 3 小时
30 天内合并 PR
3

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。