Skip to content

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

fix(execd): send the takeover close frame while an output write is in flight

Maohaobd97461116ab
diff --git a/components/execd/pkg/web/controller/pty_ws.go b/components/execd/pkg/web/controller/pty_ws.go
index c7b5a3adc..cfc4fe3c7 100644
--- a/components/execd/pkg/web/controller/pty_ws.go
+++ b/components/execd/pkg/web/controller/pty_ws.go
@@ -200,16 +200,14 @@ func PTYSessionWebSocket(ctx *gin.Context) {
// closeConn it must not wait behind a stalled output writer: a takeover targets
// exactly the slow/abandoned-client case, so blocking on connMu (held by a pump
// stuck in WriteMessage) until wsWriteDeadline would defeat wsTakeoverTimeout. It
- // therefore sends the close frame only on a best-effort basis (TryLock, short
- // deadline) and always closes the conn, which unblocks any stuck writer. A client
+ // therefore sends the close frame with WriteControl, which is safe alongside the
+ // pumps' writes and waits at most wsTakeoverCloseTimeout for an in-flight frame to
+ // finish, and always closes the conn, which unblocks any stuck writer. A client
// too backed-up to receive the frame could not have received it anyway.
evictClose := func(code int, text string) {
- if connMu.TryLock() {
- _ = conn.SetWriteDeadline(time.Now().Add(wsTakeoverCloseTimeout))
- _ = conn.WriteMessage(websocket.CloseMessage,
- websocket.FormatCloseMessage(code, text))
- connMu.Unlock()
- }
+ _ = conn.WriteControl(websocket.CloseMessage,
+ websocket.FormatCloseMessage(code, text),
+ time.Now().Add(wsTakeoverCloseTimeout))
_ = conn.Close()
}
@@ -257,7 +255,7 @@ func PTYSessionWebSocket(ctx *gin.Context) {
}
// Upgrade the eviction hook now that the initial writes are done and all subsequent
- // writes will be serialized by connMu (pumps + evictClose's TryLock). The full hook
+ // writes will be serialized by connMu (evictClose bypasses it via WriteControl). The full hook
// sends the WSCloseTakenOver close frame best-effort so the client can distinguish an
// intentional handoff from a network drop, then closes the conn and cancels goroutines.
// Generation-tokened: a tearing-down handler never clears a successor's hook.
diff --git a/components/execd/pkg/web/controller/pty_ws_test.go b/components/execd/pkg/web/controller/pty_ws_test.go
index 5d4422f8e..4962fd57c 100644
--- a/components/execd/pkg/web/controller/pty_ws_test.go
+++ b/components/execd/pkg/web/controller/pty_ws_test.go
@@ -587,6 +587,52 @@ func TestPTYWS_TakeoverEvictsHolder(t *testing.T) {
ptyOutputContains(t, conn2, "SAMESHELL_alive", 8*time.Second)
}
+// TestPTYWS_TakeoverClosesBusyHolderWithCloseCode verifies that a holder whose
+// output pump is in the middle of a write still gets the WSCloseTakenOver close
+// frame, rather than a bare TCP close (1006). The holder reads slowly until the
+// takeover handshake completes, so the pump is blocked writing when it is evicted.
+func TestPTYWS_TakeoverClosesBusyHolderWithCloseCode(t *testing.T) {
+ requireBash(t)
+ srv := newPTYTestServer(t)
+ defer srv.Close()
+
+ id := ptyCreateSession(t, srv)
+
+ conn1 := wsDialPTY(t, srv.URL, "/pty/"+id+"/ws", "")
+ ptyWaitFrame(t, conn1, "connected", 10*time.Second)
+ ptyWriteStdin(t, conn1, "yes flood_output\n")
+
+ var readSlowly atomic.Bool
+ readSlowly.Store(true)
+ closeErrCh := make(chan *websocket.CloseError, 1)
+ go func() {
+ var closeErr *websocket.CloseError
+ for {
+ if _, _, err := conn1.ReadMessage(); err != nil {
+ _ = errors.As(err, &closeErr)
+ closeErrCh <- closeErr
+ return
+ }
+ if readSlowly.Load() {
+ time.Sleep(time.Millisecond)
+ }
+ }
+ }()
+ time.Sleep(500 * time.Millisecond)
+
+ conn2 := wsDialPTY(t, srv.URL, "/pty/"+id+"/ws", "takeover=1")
+ readSlowly.Store(false)
+ defer ptyWriteStdin(t, conn2, "\x03")
+
+ select {
+ case closeErr := <-closeErrCh:
+ require.NotNil(t, closeErr, "holder should be closed with a WS CloseError after takeover")
+ require.Equal(t, model.WSCloseTakenOver, closeErr.Code)
+ case <-time.After(8 * time.Second):
+ t.Fatal("holder was not closed after takeover")
+ }
+}
+
// 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) {