@@ -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