Skip to content

python attach: close_debug_session while the target is busy for more than a second closes the target's debug port for good (it waits 1 s for the disconnect answer; debugpy's adapter exits when the client goes first) #1325

Backlog

Description

@debugmcpdev

Found on 2026-11-28 in agent trials against a Python service started with python -m debugpy --listen 0.0.0.0:5678 (debugpy 1.8.22), and then read in debugpy's source. The harm described first is measured for hand-written DAP clients. Whether mcp-debugger can cause it is not measured yet; the reason to think it can is in the second section.

What a client can do to a debugpy target

A DAP client that sends disconnect and closes its socket before reading the response makes debugpy's debug adapter process exit. That process owns the listening port and is started once, so the target's debug port is closed for the rest of the target's life. The service itself runs on.

  • debugpy's adapter passes the disconnect to pydevd in the debuggee first and replies to the client only afterwards (debugpy/adapter/clients.py, disconnect_request), about 40 ms later on an idle target.
  • If the client's stream is closed by then, the reply fails, the error reply fails too, and the handler thread ends the process (debugpy/common/messaging.py, _run_handlers: self.close(); os._exit(1)).
  • Measured on fresh targets, three runs each: attach, evaluate, disconnect, close at once: the port is gone every time. The same with the response read before closing: never. With no disconnect at all, or with the client killed while attached or paused: never.

Three of four coding agents that wrote their own DAP client did exactly this to the service they were asked not to harm.

Why mcp-debugger may do the same

src/proxy/dap-proxy-connection-manager.ts (about line 350) caps the disconnect request at 1000 ms and then goes on to close the connection. On an idle target debugpy answers in about 40 ms, so the cap is never reached, and no mcp-debugger session in the trials harmed its target. But the adapter's answer waits for pydevd inside the debuggee. A debuggee that holds the interpreter for more than a second at that moment (a long call into C code, a catastrophic regular expression, a stop-the-world pause) would answer late; mcp-debugger would have closed by then, and the adapter would exit as above.

That would mean: detaching from a busy production Python service can permanently close its debug port. It is the opposite of what a detach promises.

What to do

  1. Measure it. A target that holds the interpreter for a few seconds on demand (for example a regular expression with catastrophic backtracking), attached by mcp-debugger, then detach_from_process during the hold. Does the listener survive? Repeat on Linux and Windows.
  2. If it does not survive: the connection must not be closed before the adapter has answered the disconnect or closed the connection itself. The answer to the caller can go out earlier and say that the adapter has not confirmed yet; the socket stays open until it does, with a long backstop. Whether that is the rule for every adapter or a declared property of this one belongs in the policy, not in the core.
  3. Upstream: debugpy ending its adapter because one client went away mid-reply looks like a defect in its own right (a failed write to a departed client should end that client's session, not the process that holds the listener). Check the current release and the tracker, and report it if it is not known.

Class: a detach that may leave the target changed, silently. Path: main (tier 1, attach to a running Python service).

Activity

  1. added
    bug
    on Nov 28, 2026
  2. debugmcpdev commented on Nov 28, 2026

    @debugmcpdev
    CollaboratorAuthor

    Measured (2026-11-28): close_debug_session does it; detach_from_process does not.

    Published 0.27.0, a Python target under python -m debugpy --listen (debugpy 1.8.22) with a route that holds the interpreter for about 4 seconds (a regular expression with catastrophic backtracking), the session ended during the hold, a fresh target per run:

    Ending, during the hold Answer Debug port afterwards
    detach_from_process (2 runs) waited 3.4 s and 3.6 s, until the hold ended, then released: true usable: adapter alive, a fresh client attached
    close_debug_session (1 run) after 1.0 s, with the warning "The debug adapter did not confirm that it released the process (DAP disconnect request timed out after 1000ms)" closed for good: adapter process gone, connection refused; the service still answers HTTP

    On an idle target close_debug_session alone released cleanly.

    So the one-second cap is the close path's, and it does what the report above feared: closing a session while the target is busy for more than a second permanently closes the target's debug port. The warning in the answer does not say that. The debugging skill tells agents to always call close_debug_session, and nothing tells them to detach first.

    Not yet measured: a hold longer than detach_from_process's own bound (10 s), and Linux as the host.

  3. added
    in-progress
    on Nov 28, 2026
  4. changed the title [-]python: a client that closes right after disconnect makes debugpy's adapter exit and closes the target's debug port for good; mcp-debugger waits only 1 s for that answer[/-] [+]python attach: close_debug_session while the target is busy for more than a second closes the target's debug port for good (it waits 1 s for the disconnect answer; debugpy's adapter exits when the client goes first)[/+] on Nov 28, 2026
  5. debugmcpdev commented on Nov 28, 2026

    @debugmcpdev
    CollaboratorAuthor

    Reported upstream as microsoft/debugpy#2104, re-run on debugpy 1.8.22 (their current release) on Windows and in a Linux container:

    Client Adapter gone afterwards (Windows / Linux)
    sends disconnect, closes at once 5 of 5 / 5 of 5
    reads the response first 0 of 5 / 0 of 5
    closes without sending disconnect 0 of 5 / 0 of 5
    client process killed 0 of 5 / 0 of 5
    busy target, client gives up after 1 s 5 of 5 / 5 of 5

    On an idle target the response arrives after 1 to 3 ms (not the 40 ms first estimated here); on a target that holds the interpreter it arrives when the hold ends.

    On our side the fix is in two parts. Part A is #1330: close_debug_session waits for the adapter the way detach_from_process does (10 s), which removes the harm for holds up to that length. What remains (a hold longer than the bound, the server exiting, the idle policy, a close during an unfinished attach) needs a connection kept open past the end of a session and gets a design review first; a fix in debugpy would make it unnecessary.

  6. debugmcpdev commented on Nov 28, 2026

    @debugmcpdev
    CollaboratorAuthor

    Two more ways the worker's own shutdown path can lose a debugpy port, seen by the reviewer of #1330 on main's code and not changed by that pull request. They belong to what remains here:

    • The server exiting while a release is in flight. The exit does not go through the tool's close; once the disconnect has been forwarded the worker does not send a second one, but it can close the connection before the answer.
    • A second disconnect during the worker's pre-detach resume. Reproduced at the worker level: with the first operation's resume held, the shutdown's resume completed first, and the adapter received continue, continue, disconnect, disconnect. This path matters most for CodeLLDB on Windows, which resumes before it detaches.

    #1330 covers the tool's close_debug_session and detach_from_process and their overlaps; it does not cover the exit, the idle policy, or a child session's close (each child keeps its 1 s cap).

  7. debugmcpdev commented on Nov 28, 2026

    @debugmcpdev
    CollaboratorAuthor

    Part A is merged (#1330, c17feed8).

    What changed. close_debug_session on an attach session releases the target the way detach_from_process does: the same step, the same 10 s bound, before the session's worker is stopped. A close during a hold of a few seconds now answers when the hold ends with released: true, and the port serves the next attach; measured on Windows (debugpy 1.8.20) and Linux (debugpy 1.8.0). A close and a detach of one session that overlap send one confirmed release between them, and when the adapter does not confirm within the bound the answer says that the target's debug port may have closed with it.

    What this issue still covers. These endings still go through the worker's own detach, capped at 1 s, so a target that is holding its interpreter at that moment can still lose its port:

    • a hold longer than the 10 s bound;
    • the server exiting, and the idle policy closing a session;
    • a close while the session is still initializing;
    • a child session of an attached tree (each child's close keeps the 1 s cap).

    Removing the rest means keeping a connection, and a worker, alive past the end of a session, which is a design question of its own. A fix in debugpy would make it unnecessary: the adapter's exit is reported upstream as microsoft/debugpy#2104, and an outside contributor has opened microsoft/debugpy#2105 for it.

    Filed from the review of #1330: #1343 (closing a child session of an attached Python tree ends the child process) and #1350 (a tree close lists a descendant as released when stopping it failed). One change can settle both: the fate of each session a close touches, decided in one place.

  8. removed
    in-progress
    on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions