Secure, Fast, and Extensible Sandbox runtime for AI agents.
fix(execd): keep PTY output buffered when the shell exits
Maohaoe837927b6c00
diff --git a/components/execd/pkg/runtime/pty_session.go b/components/execd/pkg/runtime/pty_session.goindex 40d1d85ed..adfc413e7 100644--- a/components/execd/pkg/runtime/pty_session.go+++ b/components/execd/pkg/runtime/pty_session.go@@ -314,7 +314,7 @@ func (s *ptySession) StartPTY() error {defer close(outputDoneCh)s.broadcastPTY(ptmx)})- safego.Go(func() { s.waitAndExit(mp, ptmx) })+ safego.Go(func() { s.waitAndExit(mp, ptmx, outputDoneCh) })return nil}@@ -459,11 +459,23 @@ func (s *ptySession) writeAndFanout(chunk []byte, isStdout bool) {}}+// ptyDrainTimeout bounds how long the session waits for buffered PTY output+// after the shell exits, in case an attached client stopped reading.+const ptyDrainTimeout = 2 * time.Second+// waitAndExit waits for the PTY-mode process and updates session state on exit.-func (s *ptySession) waitAndExit(mp *managedProcess, ptmx *os.File) {+func (s *ptySession) waitAndExit(mp *managedProcess, ptmx *os.File, outputDone <-chan struct{}) {_ = mp.Wait()- // Close the PTY master to unblock the broadcast goroutine.+ // Output the shell wrote before exiting can still be buffered in the+ // master, and closing it discards that output. The broadcast goroutine+ // stops once it has read everything (the master then returns EIO), so+ // wait for it, but not past ptyDrainTimeout: it blocks while an attached+ // client is not reading.+ select {+ case <-outputDone:+ case <-time.After(ptyDrainTimeout):+ }_ = ptmx.Close()s.mu.Lock()diff --git a/components/execd/pkg/runtime/pty_session_test.go b/components/execd/pkg/runtime/pty_session_test.goindex 5f380be0e..706e548d7 100644--- a/components/execd/pkg/runtime/pty_session_test.go+++ b/components/execd/pkg/runtime/pty_session_test.go@@ -424,3 +424,54 @@ func terminalEntries(env []string) []string {}return found}++// When the shell exits, the PTY master still holds output the reader has not+// consumed yet, e.g. because the attached client reads slowly. That output+// must still reach the replay buffer.+func TestPTYSession_KeepsOutputBufferedWhenShellExits(t *testing.T) {+ requireBash(t)++ const outputBytes = 600000+ s := newPTYSession(uuidString(), "", "head -c 600000 /dev/zero | tr '\\0' a; printf END")+ require.NoError(t, s.StartPTY())+ t.Cleanup(func() { s.close() })++ stdoutR, _, detach := s.AttachOutput()+ defer detach()+ safego.Go(func() {+ buf := make([]byte, 4096)+ for {+ if _, err := stdoutR.Read(buf); err != nil {+ return+ }+ time.Sleep(200 * time.Microsecond)+ }+ })++ select {+ case <-s.OutputDone():+ case <-time.After(30 * time.Second):+ t.Fatal("PTY output was not drained")+ }+ data, _ := s.replay.ReadFrom(0)+ require.Equal(t, outputBytes+len("END"), len(data))+ require.True(t, strings.HasSuffix(string(data), "END"))+}++// A client that is attached but stopped reading blocks the broadcast+// goroutine; the session must still end once the shell exits.+func TestPTYSession_EndsWhileClientStopsReading(t *testing.T) {+ requireBash(t)++ s := newPTYSession(uuidString(), "", "echo started")+ require.NoError(t, s.StartPTY())+ t.Cleanup(func() { s.close() })+ _, _, detach := s.AttachOutput()+ defer detach()++ select {+ case <-s.Done():+ case <-time.After(ptyDrainTimeout + 5*time.Second):+ t.Fatal("session did not end after the shell exited")+ }+}