Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

fix(execd): send WSCloseTakenOver close frame on takeover during initial writes (#2377)

amadeus8988c1850856
diff --git a/components/execd/pkg/web/controller/pty_ws.go b/components/execd/pkg/web/controller/pty_ws.go
index cfc4fe3c7..3b00b66b7 100644
--- a/components/execd/pkg/web/controller/pty_ws.go
+++ b/components/execd/pkg/web/controller/pty_ws.go
@@ -217,16 +217,15 @@ func PTYSessionWebSocket(ctx *gin.Context) {
return conn.SetReadDeadline(time.Now().Add(wsReadDeadline))
})
- // Register a close-only eviction hook BEFORE the initial writes so a concurrent
+ // Register the eviction hook BEFORE the initial writes so a concurrent
// ?takeover=1 can interrupt a holder stalled during a large replay or the connected
- // frame. At this point the initial writes run without connMu (pumps not yet started),
- // so the hook must not acquire connMu or write to conn — only close it. Closing
- // unblocks any blocked WriteMessage and makes the subsequent read loop exit, which
- // triggers the deferred cleanup and releases the lock. cancelOnce is also called so
- // all goroutines stop once the pumps do start.
+ // frame. WriteControl is concurrent-safe with the initial unlocked writes, so evictClose
+ // sends the WSCloseTakenOver close frame best-effort (allowing the client to distinguish
+ // a handoff from a network drop) before closing the connection and unblocking any writer.
+ // Generation-tokened: a tearing-down handler never clears a successor's hook.
evictGen = session.SetEvictHandler(func() {
+ evictClose(model.WSCloseTakenOver, model.WSErrCodeTakenOver)
cancelOnce()
- _ = conn.Close()
})
// 8. Send replay frame if there is missed output.
diff --git a/components/execd/pkg/web/controller/pty_ws_test.go b/components/execd/pkg/web/controller/pty_ws_test.go
index 82a335f55..556fdb45c 100644
--- a/components/execd/pkg/web/controller/pty_ws_test.go
+++ b/components/execd/pkg/web/controller/pty_ws_test.go
@@ -676,6 +676,55 @@ func TestPTYWS_TakeoverClosesBusyHolderWithCloseCode(t *testing.T) {
require.Equal(t, model.WSCloseTakenOver, closeErr.Code)
}
+// TestPTYWS_TakeoverDuringInitialReplayClosesHolderWithCloseCode verifies that
+// evicting a holder while its initial replay write is blocked sends the
+// WSCloseTakenOver (4001) close frame rather than a bare close (1006).
+func TestPTYWS_TakeoverDuringInitialReplayClosesHolderWithCloseCode(t *testing.T) {
+ requireBash(t)
+ srv := newPTYTestServer(t)
+ defer srv.Close()
+
+ pl := newPipeListener()
+ pipeSrv := &http.Server{Handler: buildPTYRouter()}
+ go func() { _ = pipeSrv.Serve(pl) }()
+ defer pipeSrv.Close()
+
+ id := ptyCreateSession(t, srv)
+ runner := codeRunner
+ t.Cleanup(func() { _ = runner.DeletePTYSession(id) })
+
+ // Pre-populate output in the session so connecting triggers an initial replay frame.
+ initConn := wsDialPTY(t, srv.URL, "/pty/"+id+"/ws", "")
+ ptyWaitFrame(t, initConn, "connected", 10*time.Second)
+ ptyWriteStdin(t, initConn, "for i in $(seq 1 2000); do echo pre_replay_line; done\n")
+ time.Sleep(150 * time.Millisecond)
+ _ = initConn.Close()
+ time.Sleep(100 * time.Millisecond)
+
+ // Dial over pipe listener, but do NOT read messages, causing the initial replay write
+ // to block on the pipe buffer.
+ dialer := websocket.Dialer{NetDialContext: pl.DialContext}
+ conn1, _, err := dialer.Dial("ws://pipe/pty/"+id+"/ws", nil)
+ require.NoError(t, err)
+ defer conn1.Close()
+
+ // Trigger takeover while conn1 is in initial replay write.
+ time.Sleep(50 * time.Millisecond)
+ _ = wsDialPTY(t, srv.URL, "/pty/"+id+"/ws", "takeover=1")
+ time.Sleep(50 * time.Millisecond)
+
+ var closeErr *websocket.CloseError
+ deadline := time.Now().Add(8 * time.Second)
+ for time.Now().Before(deadline) {
+ if _, _, err := conn1.ReadMessage(); err != nil {
+ _ = errors.As(err, &closeErr)
+ break
+ }
+ }
+ require.NotNil(t, closeErr, "holder should be closed with a WS CloseError after takeover")
+ require.Equal(t, model.WSCloseTakenOver, closeErr.Code)
+}
+
// TestPTYWS_TakeoverOnFreeSessionConnects verifies ?takeover=1 is a no-op when the
// session is free: it connects normally (there is no holder to evict).
func TestPTYWS_TakeoverOnFreeSessionConnects(t *testing.T) {