dap: fix the race condition in the dap unit tests

The context used for serving the dap server was being canceled too early
because it used defer which would initiate at the end of the function
while every other cleanup function used `t.Cleanup` which executes in
its own goroutine.

One possible solution was to move the cancel to the cleanup, but the
context being passed to serve and start doesn't make sense because if it
ever does get canceled, it'll likely cause a similar race condition with
`Stop`.

This removes the context from the methods that were causing this issue
in favor of just relying on the caller calling `Stop` when they are done
with the adapter and server.

This seems to have only affected tests and I don't believe it affected
the actual dap command.

Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
This commit is contained in:
Jonathan A. Sternberg
2026-03-10 12:03:35 -05:00
parent c423bc738b
commit ca9df87014
4 changed files with 13 additions and 14 deletions
+1 -2
View File
@@ -1,7 +1,6 @@
package commands package commands
import ( import (
"context"
"io" "io"
"net" "net"
"os" "os"
@@ -75,7 +74,7 @@ type adapterProtocolDebugger struct {
} }
func (d *adapterProtocolDebugger) Start(printer *progress.Printer, opts *BuildOptions) error { func (d *adapterProtocolDebugger) Start(printer *progress.Printer, opts *BuildOptions) error {
cfg, err := d.Adapter.Start(context.Background(), d.conn) cfg, err := d.Adapter.Start(d.conn)
if err != nil { if err != nil {
return errors.Wrap(err, "debug adapter did not start") return errors.Wrap(err, "debug adapter did not start")
} }
+3 -3
View File
@@ -67,10 +67,10 @@ func New[C LaunchConfig]() *Adapter[C] {
return d return d
} }
func (d *Adapter[C]) Start(ctx context.Context, conn Conn) (C, error) { func (d *Adapter[C]) Start(conn Conn) (C, error) {
d.eg, _ = errgroup.WithContext(ctx) d.eg, _ = errgroup.WithContext(context.Background())
d.eg.Go(func() error { d.eg.Go(func() error {
return d.srv.Serve(ctx, conn) return d.srv.Serve(conn)
}) })
<-d.initialized <-d.initialized
+7 -7
View File
@@ -24,7 +24,7 @@ func TestLaunch(t *testing.T) {
eg, _ := errgroup.WithContext(ctx) eg, _ := errgroup.WithContext(ctx)
eg.Go(func() error { eg.Go(func() error {
_, err := adapter.Start(ctx, conn) _, err := adapter.Start(conn)
assert.NoError(t, err) assert.NoError(t, err)
return nil return nil
}) })
@@ -57,13 +57,13 @@ func TestLaunch(t *testing.T) {
// We should have received the initialized event. // We should have received the initialized event.
select { select {
case <-initialized: case <-initialized:
default: case <-ctx.Done():
assert.Fail(t, "did not receive initialized event") assert.Fail(t, "did not receive initialized event")
} }
select { select {
case <-configurationDone: case <-configurationDone:
case <-time.After(10 * time.Second): case <-ctx.Done():
assert.Fail(t, "did not receive configurationDone response") assert.Fail(t, "did not receive configurationDone response")
} }
return nil return nil
@@ -82,7 +82,7 @@ func TestSetBreakpoints(t *testing.T) {
eg, _ := errgroup.WithContext(ctx) eg, _ := errgroup.WithContext(ctx)
eg.Go(func() error { eg.Go(func() error {
_, err := adapter.Start(ctx, conn) _, err := adapter.Start(conn)
assert.NoError(t, err) assert.NoError(t, err)
return nil return nil
}) })
@@ -118,7 +118,7 @@ func TestSetBreakpoints(t *testing.T) {
// We should have received the initialized event. // We should have received the initialized event.
select { select {
case <-initialized: case <-initialized:
default: case <-ctx.Done():
assert.Fail(t, "did not receive initialized event") assert.Fail(t, "did not receive initialized event")
} }
@@ -127,7 +127,7 @@ func TestSetBreakpoints(t *testing.T) {
assert.True(t, setBreakpointsResp.Success) assert.True(t, setBreakpointsResp.Success)
assert.Len(t, setBreakpointsResp.Body.Breakpoints, 0) assert.Len(t, setBreakpointsResp.Body.Breakpoints, 0)
assert.NotNil(t, setBreakpointsResp.Body.Breakpoints, "breakpoints should be an empty array instead of null in the JSON") assert.NotNil(t, setBreakpointsResp.Body.Breakpoints, "breakpoints should be an empty array instead of null in the JSON")
case <-time.After(10 * time.Second): case <-ctx.Done():
assert.Fail(t, "did not receive setBreakpoints response") assert.Fail(t, "did not receive setBreakpoints response")
} }
return nil return nil
@@ -249,11 +249,11 @@ func NewTestAdapter[C LaunchConfig](t *testing.T) (*Adapter[C], Conn, *daptest.C
t.Cleanup(func() { clientConn.Close() }) t.Cleanup(func() { clientConn.Close() })
adapter := New[C]() adapter := New[C]()
t.Cleanup(func() { adapter.Stop() })
client := daptest.NewClient(clientConn) client := daptest.NewClient(clientConn)
t.Cleanup(func() { client.Close() }) t.Cleanup(func() { client.Close() })
t.Cleanup(func() { adapter.Stop() })
return adapter, srvConn, client return adapter, srvConn, client
} }
+2 -2
View File
@@ -33,11 +33,11 @@ func NewServer(h Handler) *Server {
return &Server{h: h} return &Server{h: h}
} }
func (s *Server) Serve(ctx context.Context, conn Conn) error { func (s *Server) Serve(conn Conn) error {
writeCh := make(chan dap.Message) writeCh := make(chan dap.Message)
s.ch = writeCh s.ch = writeCh
s.ctx, s.cancel = context.WithCancelCause(ctx) s.ctx, s.cancel = context.WithCancelCause(context.Background())
// Start an error group to handle server-initiated tasks. // Start an error group to handle server-initiated tasks.
s.eg, _ = errgroup.WithContext(s.ctx) s.eg, _ = errgroup.WithContext(s.ctx)