Skip to content

Flaky: python-attach-close-busy-target, a new attach is refused after a released close (debugpy's adapter is gone), and no log says why #1376

Backlog

Description

@debugmcpdev

main's push run for 06d547f3 (run 46226544729, first attempt) failed in one file, tests/adapters/python/integration/python-attach-close-busy-target.test.ts, on Ubuntu. Both of its tests failed the same way, at the same step.

What happened

Each test attaches to a target started with python -m debugpy --listen, makes the target hold the interpreter, ends the session (a close in one test, a detach joined by a close in the other), lets the target go, and then checks that the same port serves a new attach (portServesANewAttach).

Everything up to that last step passed in both tests: the ending answered released: true with no warning, the hold ended by the test's own byte, and the target process was alive. Then the new attach was refused:

Failed to attach: Critical initialization error: Failed to connect DAP client: nothing accepted a connection on 127.0.0.1:33229 within 12.5s (12.5s elapsed, 61 attempts; last error: connect ECONNREFUSED 127.0.0.1:33229) [direct connect to 127.0.0.1:33229; no adapter process was started]

Refused, 61 times over 12.5 s, means nothing was listening: the target's debug adapter had gone, although the server had been told the release was answered. That is the outcome #1325 exists to prevent, reached by a route its repair does not cover.

What is known

  • It is not deterministic. The same tree passed this file in the pull request's own run 25 minutes earlier (fix(python): a logpoint's message is checked by the program's own interpreter before it is sent #1371, Ubuntu and Windows), and in four runs on a two-core Ubuntu machine during that change. After the failure: the file alone, on that two-core machine, passed 15 of 15 at the merged tree and 15 of 15 at the commit before the merge. Before the merge, main's last eleven push runs were green. So the failure needs something a full run has and the file alone does not (load, or another test file running beside it), and one failure cannot say whether the merge made it more likely.
  • The merged change (fix(python): a logpoint's message is checked by the program's own interpreter before it is sent #1371) does not touch an attach's close, except for one awaited call after the proxy has stopped that returns at once when the session has no logpoint checker, which an attach never has. I could not derive a route from it to this failure, and I could not rule one out without the adapter's own log.
  • debugpy's adapter ends itself on any exception in a message handler or while finalizing a session (debugpy/common/messaging.py, _run_handlers: self.close(); os._exit(1); debugpy/adapter/sessions.py, finalize: os._exit(1)). The known trigger is a client that closes its connection before a reply is written. Here the reply to disconnect was received, so it was something after it.
  • Both tests failing in one run points at something common to that run (the machine's speed at that moment, or the one server process the file's tests share) rather than two independent accidents.

What cannot be answered from this run

Why the adapter went. Neither the test nor CI keeps the two logs that say:

  • the adapter's own log (debugpy --log-to <dir> on the target's command line writes it, and the adapter logs the exception before it exits);
  • the server's per-session proxy log, whose path is in the error (tests/adapters/sessions/<id>/run-…/proxy-<id>.log) and which is gone with the runner.

Expected

  1. The test starts its target with --log-to into its scratch directory and, when the new attach is refused, puts the tail of the adapter's log into the assertion's message. The next occurrence then names its own cause.
  2. CI uploads the session logs of a failed integration job as an artifact (an upload step with if: failure()), so a failure of any attach test can be read after the fact.
  3. With the cause known: either a route in our teardown that still lets the adapter meet a closed connection (then it is a defect of ours, in the class of javascript: a connection to a Node target is closed while it may still be writing, in thirteen places (the class behind #1119) #1373), or a race inside debugpy (then it joins the upstream queue, with a fix).

Class: a failure that cannot be diagnosed from its own record. Path: edge (a session ended on a busy attached target), but the cost when it happens outside a test is the user's process losing its debug port for good.

Activity

  1. debugmcpdev commented on Nov 29, 2026

    @debugmcpdev
    CollaboratorAuthor

    A lead, from debugpy's own log on a run that was made to fail on purpose (#1377 adds the log keeping; debugpy 1.8.20, Windows). It is not yet the log of a real failure.

    After the disconnect had been answered and the client had gone, the adapter logged:

    /handling #44 event "thread" from Server[1]/
    Handler 'Server.event' … couldn't handle #44 event "thread" from Server[pid=…]:
    Client[1] disconnected unexpectedly
    

    So the debuggee goes on sending events after the client has left, and the adapter's handler for them meets a client that is gone. In that run the error was the kind debugpy logs and survives. For any other exception in a handler it closes and exits (debugpy/common/messaging.py, _run_handlers), which would close the port exactly as seen.

    What that suggests, to be checked against a real failure's log:

    • The window: between our side closing the socket (the server does it as soon as it has the answer) and the adapter marking its client as gone (it closes the client's channel itself right after replying). An event from the debuggee that the adapter tries to forward inside that window writes to a broken stream while it still believes the client is connected. A target that has just been let go after holding the interpreter sends such events at once, and a loaded machine widens the window, which fits a failure seen once, in a full run, in both tests.
    • If so, our side has a remedy that needs no change in debugpy: after a disconnect has been answered, let the adapter close the connection and wait for that under a bound, instead of closing first. That is the same class as javascript: a connection to a Node target is closed while it may still be writing, in thirteen places (the class behind #1119) #1373, from the other side.
    • And debugpy has a defect worth a fix there: an event that cannot be forwarded to a client that is leaving should not be able to end the adapter.

    Nothing is changed in the server on this lead alone.

  2. debugmcpdev commented on Nov 29, 2026

    @debugmcpdev
    CollaboratorAuthor

    The re-run of the failed jobs passed on both systems (run 46226544729, attempt 2, same commit), so main is green again. One failure in two full runs at this commit; 30 of 30 passes for the file alone.

  3. debugmcpdev commented on Nov 29, 2026

    @debugmcpdev
    CollaboratorAuthor

    The log-keeping merged as #1377 is now proven on both systems, by a probe that failed this test on purpose (#1379, closed unmerged): on each system the two artifacts arrive and hold debugpy's adapter, server and pydevd logs for both targets, and the failure's message carries both log tails.

    Two things the probe showed besides:

    • The other system's job is cancelled. The test matrix runs with the default fail-fast, so when one system's test job fails, the other's is cancelled where it stands. In the run that failed on main (46226544729, attempt 1) the Linux job failed at 05:13:26 and the Windows job was cancelled ten seconds later, so that run never said whether the test fails on Windows too. fail-fast: false on that matrix would let the other job finish, at the cost of runner minutes on red runs only.
    • The adapter's log on a healthy run. In the four logs from the runners (two targets on each system) the adapter's last lines are All debug servers disconnected; waiting for remaining sessions... and All debug sessions have ended; exiting., well under a second after the second session connected: that is the test ending its target. None of the four holds a couldn't handle line, a disconnected unexpectedly line or a traceback. So the adapter also leaves by design when its debug server's connection ends, and that is a second thing to look for in a real failure's log, beside an exception in a handler.
  4. debugmcpdev commented on Nov 29, 2026

    @debugmcpdev
    CollaboratorAuthor

    Tried to reproduce it in the shape it failed in: CI's own test command (vitest run --coverage --project unit --project integration) three times in a row on a two-core Ubuntu machine at fa41bf26. All three runs passed (534 files each), this file among them. So it stands at one failure in the full job on a runner, none in three full runs and thirty runs of the file alone elsewhere. The next occurrence in CI will carry its logs.

  5. debugmcpdev commented on Nov 29, 2026

    @debugmcpdev
    CollaboratorAuthor

    Reproduced locally with the debugpy adapter log that #1377 makes the test keep. This is the first capture that says why.

    What failed: python-attach-close-busy-target.test.ts, "keeps the target's debug port when the session is closed while the target holds the interpreter". The second attach answered nothing accepted a connection on 127.0.0.1:<port> within 12.5s ... ECONNREFUSED.

    What the target's adapter log shows (times are seconds since the adapter started):

    I+00004.971: /handling #11 request "disconnect" from Client[1]/
    Session[1] finalized.
    D+00004.971: Closing Client[1] message stream
    E+00005.031: /handling #11 request "disconnect" from Client[1]/
    couldn't handle #11 request "disconnect" from Client[1]:
    raise NoMoreMessages(stream=self)
    debugpy.common.messaging.NoMoreMessages: No more messages
    "{0} disconnected unexpectedly", exc.stream.name, silent=True
    

    The adapter finished handling the disconnect at +4.971 s (session finalized, client stream closed) and failed to send its answer 60 ms later with NoMoreMessages: the client's side of the connection was already gone. So the server's wait for the disconnect answer ended before the busy target let the adapter answer. The adapter treated that as the client leaving first and exited, taking the port with it. That is the mechanism of #1325 again, reached when the target's busy period and our wait end at about the same moment.

    Conditions: Windows 11, Python 3.13.12, debugpy 1.8.20, mains power, the machine loaded by other test runs at the time. The branch under test changes the Python launch path only; this test attaches to a target it starts itself.

    So the flake is a real margin: the close's bound against how long the target holds the interpreter. Under load the two meet.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions