Repository navigation
fix(js-debug): read the child session so Node.js commands stop hanging - #21
Ayushsinha2004 wants to merge 1 commit into
Conversation
js-debug is multi-session: the connection dap launches on is only a session manager, and the debuggee runs in a child session on a second connection. setupChildSession swapped d.client to the child but never started a reader on it, while readLoop stayed pinned to the parent. The child's StoppedEvent and TerminatedEvent therefore never reached expectCh, and every execution command (debug, step, continue) blocked forever. - readLoop takes its connection and channel explicitly; setupChildSession starts a second loop on the child connection with the same channel, so exactly one live loop closes it. - The parent keeps draining its socket after the hand-off (it only mirrors the child's terminated event) and declines a further startDebugging instead of leaving it unanswered. - Answer startDebugging on the loop's own connection instead of d.client, which may already point at the child. Adds TestReadLoop_ChildSessionHandoff, which drives the daemon against a fake multi-session adapter and needs no js-debug, so it runs in CI where the Node e2e tests skip. TestE2E_DebugNode timed out on master and now passes in ~3.5s. Adds TestE2E_DebugTypeScript (compiled JS + source map, breakpoint in the .ts source). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
PR Summary by QodoFix js-debug hangs by reading the child DAP session
AI Description
Diagram
High-Level Assessment
Files changed (6)
|
Code Review by Qodo
1. Child failure can crash the daemon
|
| // Without a loop of its own, the child's StoppedEvent/TerminatedEvent would | ||
| // never reach ch and every execution command would block forever. | ||
| d.setClient(childClient) | ||
| go d.readLoop(childClient, ch) |
There was a problem hiding this comment.
1. Child failure can crash the daemon 📘 Rule violation ☼ Reliability
setupChildSession starts a child readLoop before the parent loop sets handedOff, while both loops defer closing the same ch. If the child reaches EOF or fails during that window, it closes ch; a subsequent parent event reaches ch <- msg and panics.
Agent Prompt
Issue description
The child reader starts before the parent reader marks the handoff complete, so both readers can close the shared channel. A fast child failure can close the channel first, after which the parent may send an event to the closed channel and panic.
Fix Focus Areas
- daemon.go[266-270]
- daemon.go[320-324]
- daemon.go[1445-1448]
Recommended Fix
Transfer channel-closure ownership atomically before starting the child reader, or centralize closure behind a synchronization primitive such as sync.Once. Ensure a child initialization failure leaves the parent as the sole channel owner and that no reader can send after closure.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| parentClient, err := newDAPClient(ln.Addr().String()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer parentClient.Close() | ||
| d.setClient(parentClient) | ||
| ch := make(chan godap.Message, 64) | ||
| go d.readLoop(parentClient, ch) |
There was a problem hiding this comment.
2. Handoff has no isolated client test 📘 Rule violation ⚙ Maintainability
TestReadLoop_ChildSessionHandoff drives a concrete DAPClient over TCP instead of mocking the DAP client through an interface. The test therefore exercises the fake network adapter and reader together, so client-level failures are not isolated at the unit-test boundary required for this handoff.
Agent Prompt
Issue description
The new child-session handoff test constructs a real network-backed `DAPClient` and fake TCP adapter rather than testing the reader against an interface-backed mock. This does not provide the isolated DAP-client unit coverage required for the new behavior.
Fix Focus Areas
- daemon_test.go[542-587]
- daemon.go[265-324]
Recommended Fix
Introduce or use a narrow DAP client interface for the reader's required operations, provide a mock implementation in the unit test, and keep the TCP fake as an integration test. Assert the parent acknowledgement, child event delivery, and single channel closure through the isolated mock test.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
Title: fix(js-debug): read the child session so Node.js commands stop hanging
Problem
With the js-debug backend, every execution command hangs:
dap debug app.js --break app.js:3never returns. On currentmaster, the existingTestE2E_DebugNodetimes out whenever js-debug is installed:CI doesn't catch it because the workflow only installs debugpy, so the Node tests skip.
Cause
js-debug is multi-session. The connection
daplaunches on is only a session manager, and the debuggee runs in a child session on a second connection that js-debug requests throughstartDebugging.setupChildSessionswapsd.clientto the child but never starts a reader on it, whilereadLoopstays pinned to the parent connection. The child'sStoppedEventandTerminatedEventnever reachexpectCh, sodebug,stepandcontinueblock forever.Fix (daemon.go)
readLoopnow takes the connection and the channel it serves explicitly.setupChildSessionstarts a second loop on the child connection and hands it the same channel, so exactly one live loop closes it.terminatedevent (checked by logging), which is discarded. A furtherstartDebuggingon the parent (js-debug's child-process attach) is declined with an error response and a log line instead of being left unanswered; supporting more than one child session is out of scope here.startDebuggingresponse is sent on the loop's own connection rather thand.client, which may already point at the child.No behaviour change for debugpy, dlv or lldb-dap, which never hand off to a child session.
Tests
TestReadLoop_ChildSessionHandoff(daemon_test.go) drives the daemon against a fake multi-session adapter: the parent connection sendsstartDebugging, theStoppedEventarrives on a second connection, and the test checks it reaches the channel, that the acknowledgement went back on the parent, and that the channel closes exactly once when both connections end. It needs no js-debug, so it runs in CI; without the child reader it times out.TestE2E_DebugNode: times out onmaster, passes with this change (~3.5s).TestE2E_DebugTypeScriptdebugs compiled JS with a breakpoint in the.tssource (committedgreet.js+ source map, no compiler needed). Times out onmaster, passes here.go test ./...,gofmt,go vetandgolangci-lintv2.11.1 are clean (macOS arm64, Go 1.26, Node 20, js-debug, debugpy); the Node and unit tests are also clean under-raceover repeated runs. The dlv tests couldn't run here because macOS developer mode is disabled; they fail the same way onmasterin this environment.Known limitation (not changed here)
A
.tsbreakpoint passed with--breakat launch can be missed by a script that exits in under ~1s, because the program finishes before the breakpoint binds. Longer-running programs, and breakpoints added after--stop-on-entry, bind normally. Happy to look at this separately.Possible follow-up: install js-debug in CI so the Node e2e tests run.