Secure, Fast, and Extensible Sandbox runtime for AI agents.
fix(execd): drop PTY stdin once the shell is reaped
Maohao0f57fac7e070
diff --git a/components/execd/pkg/runtime/pty_session.go b/components/execd/pkg/runtime/pty_session.goindex db9334c13..a8853c006 100644--- a/components/execd/pkg/runtime/pty_session.go+++ b/components/execd/pkg/runtime/pty_session.go@@ -508,13 +508,20 @@ func (s *ptySession) waitAndExitPipe(mp *managedProcess, stdinW, stdoutR, stderr}// WriteStdin writes p to shell stdin (PTY master or pipe write-end).+// Input that arrives once the shell is reaped is dropped, as a terminal would:+// while the output drains the master is still open, and a write to it would+// block once the tty input queue is full and then fail.func (s *ptySession) WriteStdin(p []byte) (int, error) {s.mu.Lock()w := s.stdin+ exited := s.pid == 0s.mu.Unlock()if w == nil {return 0, errors.New("session not started")}+ if exited {+ return len(p), nil+ }return w.Write(p)}diff --git a/components/execd/pkg/runtime/pty_session_test.go b/components/execd/pkg/runtime/pty_session_test.goindex 26c7b1e16..1014a505f 100644--- a/components/execd/pkg/runtime/pty_session_test.go+++ b/components/execd/pkg/runtime/pty_session_test.go@@ -491,3 +491,43 @@ func TestPTYSession_StopsSignallingOnceShellIsReaped(t *testing.T) {ptyDrainTimeout/2, 10*time.Millisecond,"session still signalable while its output drains")}++// Input that arrives after the shell is reaped, while its output drains, is+// dropped as a terminal would, rather than failing the client's stdin write.+func TestPTYSession_DropsStdinOnceShellIsReaped(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()++ require.Eventually(t, func() bool { return !s.IsRunning() },+ ptyDrainTimeout/2, 10*time.Millisecond)+ select {+ case <-s.Done():+ t.Fatal("session ended before its output drained")+ default:+ }++ // More than the tty input queue holds, so a write that reached the master+ // would block until the drain ends and then fail.+ line := []byte(strings.Repeat("a", 1023) + "\n")+ written := make(chan error, 1)+ safego.Go(func() {+ for range 64 {+ if _, err := s.WriteStdin(line); err != nil {+ written <- err+ return+ }+ }+ written <- nil+ })+ select {+ case err := <-written:+ require.NoError(t, err)+ case <-time.After(ptyDrainTimeout / 2):+ t.Fatal("stdin write blocked while the output drained")+ }+}