Skip to content

Commit 3acf812

Browse files
committed
test: consolidate acpshim deadlock regression tests
Signed-off-by: tomsen02 <[email protected]>
1 parent af71913 commit 3acf812

3 files changed

Lines changed: 54 additions & 64 deletions

File tree

go/core/pkg/acpshim/child_deadlock_test.go renamed to go/core/pkg/acpshim/child_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
// TestTerminateReturnsWhenOutNotDrained guards against a teardown deadlock:
9-
// when nobody drains c.out (cap 64), the stdout reader goroutine blocks
9+
// when nobody drains c.out, the stdout reader goroutine blocks
1010
// sending on the full channel and can never reach cmd.Wait()/close(done).
1111
// terminate() must still reap the child and return promptly instead of
1212
// blocking forever on <-c.done.

‎go/core/pkg/acpshim/server_deadlock_test.go‎

Lines changed: 0 additions & 63 deletions
This file was deleted.

‎go/core/pkg/acpshim/server_test.go‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,56 @@ func TestLongLivedChildSurvivesReconnect(t *testing.T) {
229229
t.Errorf("after reconnect read %q, want \"ping\" (a second \"ready\" means the child was restarted)", got)
230230
}
231231
}
232+
233+
// TestShutdownReturnsWithStalledClient exercises the deadlock end to end
234+
// through the real Server. A connected client that never reads causes pump()'s
235+
// conn.WriteMessage to block, so pump stops draining c.out; c.out fills and
236+
// the child's stdout reader goroutine blocks on send. Server.Shutdown ->
237+
// child.terminate() must still kill the child and return promptly instead of
238+
// hanging on <-c.done.
239+
func TestShutdownReturnsWithStalledClient(t *testing.T) {
240+
// Emit ~6.4MB (400 x 16KB lines) to overflow the socket buffers of a
241+
// non-reading client, then stay alive so terminate() must kill the child.
242+
cfg := &Config{
243+
ListenAddr: "127.0.0.1:0",
244+
ChildArgv: []string{"sh", "-c",
245+
"head -c 6400000 /dev/zero | tr '\\0' 'A' | fold -w 16000; sleep 3600"},
246+
GracePeriod: 500 * time.Millisecond,
247+
}
248+
if err := cfg.Validate(); err != nil {
249+
t.Fatalf("config: %v", err)
250+
}
251+
l, err := net.Listen("tcp", cfg.ListenAddr)
252+
if err != nil {
253+
t.Fatalf("listen: %v", err)
254+
}
255+
srv := NewServer(cfg)
256+
go func() { _ = srv.Serve(l) }()
257+
url := "ws://" + l.Addr().String() + "/acp"
258+
259+
// A client that connects but never reads. Its receive window fills, so the
260+
// server's pump blocks in conn.WriteMessage and stops draining c.out.
261+
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
262+
if err != nil {
263+
t.Fatalf("dial: %v", err)
264+
}
265+
defer conn.Close()
266+
267+
// Let the pipeline stall: child spews -> out fills -> pump blocks on write.
268+
time.Sleep(2 * time.Second)
269+
270+
done := make(chan struct{})
271+
go func() {
272+
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
273+
defer cancel()
274+
_ = srv.Shutdown(ctx)
275+
close(done)
276+
}()
277+
278+
select {
279+
case <-done:
280+
case <-time.After(6 * time.Second):
281+
t.Fatalf("Server.Shutdown did not return within 6s: stalled client -> pump stops " +
282+
"draining c.out -> child reader blocks on send -> terminate() hangs on <-c.done")
283+
}
284+
}

0 commit comments

Comments
 (0)