Repository navigation
fix(js-debug): read the child session so Node.js commands stop hanging #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,16 @@ | ||
| package dap | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net" | ||
| "os/exec" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| godap "github.com/google/go-dap" | ||
| ) | ||
|
|
||
| func TestOutputBufferBoundedAtWrite(t *testing.T) { | ||
|
|
@@ -492,3 +496,140 @@ func TestMergeBreakpoints(t *testing.T) { | |
| }) | ||
| } | ||
| } | ||
|
|
||
| // serveChildSession plays the child side of a js-debug style adapter: it answers | ||
| // the daemon's handshake on conn and emits a StoppedEvent once configurationDone | ||
| // arrives. It returns when the connection closes. | ||
| func serveChildSession(conn net.Conn) { | ||
| r := bufio.NewReader(conn) | ||
| seq := 100 | ||
| next := func() int { seq++; return seq } | ||
| for { | ||
| msg, err := godap.ReadProtocolMessage(r) | ||
| if err != nil { | ||
| return | ||
| } | ||
| req, ok := msg.(godap.RequestMessage) | ||
| if !ok { | ||
| continue | ||
| } | ||
| cmd := req.GetRequest().Command | ||
| if cmd == "launch" { | ||
| _ = godap.WriteProtocolMessage(conn, &godap.InitializedEvent{ | ||
| Event: godap.Event{ProtocolMessage: godap.ProtocolMessage{Seq: next(), Type: "event"}, Event: "initialized"}, | ||
| }) | ||
| } | ||
| _ = godap.WriteProtocolMessage(conn, &godap.Response{ | ||
| ProtocolMessage: godap.ProtocolMessage{Seq: next(), Type: "response"}, | ||
| RequestSeq: req.GetRequest().Seq, | ||
| Success: true, | ||
| Command: cmd, | ||
| }) | ||
| if cmd == "configurationDone" { | ||
| _ = godap.WriteProtocolMessage(conn, &godap.StoppedEvent{ | ||
| Event: godap.Event{ProtocolMessage: godap.ProtocolMessage{Seq: next(), Type: "event"}, Event: "stopped"}, | ||
| Body: godap.StoppedEventBody{Reason: "breakpoint", ThreadId: 1}, | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TestReadLoop_ChildSessionHandoff mirrors js-debug's multi-session protocol with | ||
| // a fake adapter: the parent connection only asks for a child session through the | ||
| // startDebugging reverse request, and the debuggee's events arrive on a second | ||
| // connection. Without a reader on that child connection the StoppedEvent below | ||
| // never reaches the channel and every execution command hangs. Needs no js-debug. | ||
| func TestReadLoop_ChildSessionHandoff(t *testing.T) { | ||
| ln, err := net.Listen("tcp", "127.0.0.1:0") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer func() { _ = ln.Close() }() | ||
|
|
||
| d := &Daemon{backend: &jsDebugBackend{}, adapterAddr: ln.Addr().String()} | ||
|
|
||
| parentAck := make(chan godap.Message, 1) | ||
| go func() { | ||
| parent, err := ln.Accept() | ||
| if err != nil { | ||
| return | ||
| } | ||
| defer func() { _ = parent.Close() }() | ||
| _ = godap.WriteProtocolMessage(parent, &godap.StartDebuggingRequest{ | ||
| Request: godap.Request{ProtocolMessage: godap.ProtocolMessage{Seq: 1, Type: "request"}, Command: "startDebugging"}, | ||
| Arguments: godap.StartDebuggingRequestArguments{ | ||
| Configuration: map[string]any{"type": "pwa-node", "name": "child"}, | ||
| Request: "launch", | ||
| }, | ||
| }) | ||
| // The acknowledgement must come back on the parent connection. | ||
| ack, err := godap.ReadProtocolMessage(bufio.NewReader(parent)) | ||
| if err != nil { | ||
| return | ||
| } | ||
| parentAck <- ack | ||
|
|
||
| child, err := ln.Accept() | ||
| if err != nil { | ||
| return | ||
| } | ||
| defer func() { _ = child.Close() }() | ||
| serveChildSession(child) | ||
| }() | ||
|
|
||
| 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) | ||
|
Comment on lines
+580
to
+587
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2. Handoff has no isolated client test 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
|
||
|
|
||
| deadline := time.After(5 * time.Second) | ||
| var stopped *godap.StoppedEvent | ||
| for stopped == nil { | ||
| select { | ||
| case msg, ok := <-ch: | ||
| if !ok { | ||
| t.Fatal("channel closed before the child's StoppedEvent arrived") | ||
| } | ||
| stopped, _ = msg.(*godap.StoppedEvent) | ||
| case <-deadline: | ||
| t.Fatal("the child session's StoppedEvent never reached the channel") | ||
| } | ||
| } | ||
| if stopped.Body.Reason != "breakpoint" { | ||
| t.Errorf("stopped reason = %q, want breakpoint", stopped.Body.Reason) | ||
| } | ||
|
|
||
| select { | ||
| case ack := <-parentAck: | ||
| if _, ok := ack.(*godap.StartDebuggingResponse); !ok { | ||
| t.Errorf("parent connection received %T, want *godap.StartDebuggingResponse", ack) | ||
| } | ||
| case <-time.After(5 * time.Second): | ||
| t.Fatal("startDebugging was never acknowledged on the parent connection") | ||
| } | ||
|
|
||
| child := d.getClient() | ||
| if child == parentClient { | ||
| t.Fatal("d.client still points at the parent session") | ||
| } | ||
|
|
||
| // Ending both connections ends both loops; the channel must close exactly | ||
| // once (a second close would panic). | ||
| child.Close() | ||
| parentClient.Close() | ||
| deadline = time.After(5 * time.Second) | ||
| for { | ||
| select { | ||
| case _, ok := <-ch: | ||
| if !ok { | ||
| return | ||
| } | ||
| case <-deadline: | ||
| t.Fatal("channel was not closed after both connections ended") | ||
| } | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| interface Person { | ||
| name: string; | ||
| age: number; | ||
| } | ||
|
|
||
| function greet(p: Person): string { | ||
| const label: string = p.age >= 18 ? "adult" : "minor"; | ||
| return `Hello ${p.name} (${label})`; | ||
| } | ||
|
|
||
| const message: string = greet({ name: "Ada", age: 36 }); | ||
| console.log(message); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Child failure can crash the daemon
📘 Rule violation☼ ReliabilityAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools