Skip to content

Warn instead of exiting when the adapter endpoints file can't be written - #2109

Open
om singhal (Om-singhaI) wants to merge 1 commit into
microsoft:mainfrom
Om-singhaI:fix/adapter-endpoints-file-error
Open

om singhal (Om-singhaI) wants to merge 1 commit into
microsoft:mainfrom
Om-singhaI:fix/adapter-endpoints-file-error

Conversation

@Om-singhaI

Copy link
Copy Markdown
Contributor

Fixes #1866

When DEBUGPY_ADAPTER_ENDPOINTS names a file that can't be written, for example a path a terminal kept from an earlier VS Code session whose directory is gone, the adapter that debugpy.listen() spawns sends its endpoints to the debug server, then fails to write the file and exits. Its stdio is already closed by then, so the real error never reaches the user (on Python 3.12 and later the terminal only shows a bare lost sys.stderr dump): listen() returns normally, but nothing is listening on the port and the client's connection is refused.

Following the discussion in microsoft/vscode-python-debugger#612, where a warning was suggested instead of a crash, this makes the failure a warning on that path:

  • src/debugpy/adapter/__main__.py: with --for-server, the endpoints file is written just before the endpoints are sent to the debug server instead of just after. If writing it fails, the adapter logs a warning, keeps running, and adds a "warning" entry to the endpoints it sends. Without --for-server a failure is still fatal, since there the file can be the only way to learn the port. If sending the endpoints fails, the listeners are now closed before exiting, so the file is still deleted only after the ports are closed.
  • src/debugpy/server/api.py: a "warning" in the endpoints is logged, so it shows on the debuggee's stderr with the default DEBUGPY_LOG_STDERR. It's the only new stderr output, and only when the variable is set and the write failed.
  • tests/debugpy/test_attach.py: test_attach_api_endpoints_file_not_writable points the variable at a missing directory, calls debugpy.listen(), attaches, stops at a breakpoint, and checks the warning on stderr.

Writing the file a little earlier means a client watching it can connect a moment before the debug server does, which could already happen on main since the debug server connects back only after it gets the endpoints. An attach with "connect" waits up to PROCESS_SPAWN_TIMEOUT for the server in that case (clients.py).

Testing on macOS with CPython 3.13 and PYTHONPATH=src:

  • The new test passes. With src/ restored from main it fails with ConnectionRefusedError, the error from the issue.
  • tests/debugpy/test_attach.py, test_api.py, adapter/, server/ and test_run.py: 332 passed, 45 skipped. I left out the PID attach cases and test_nodebug, whose cases crash the pytest worker with no output on this machine, the same way on main.
  • Running the adapter by hand with a closed server port, it closes its listeners before deleting the file. With --port 0, no --for-server and a missing directory, it still exits with the FileNotFoundError as on main.
  • ruff check . passes, and black reports nothing new for the changed lines.

@Om-singhaI
om singhal (Om-singhaI) requested a review from a team as a code owner December 3, 2026 19:43
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DEBUGPY_ADAPTER_ENDPOINTS being invalid should output an error

1 participant