Secure, Fast, and Extensible Sandbox runtime for AI agents.
fix(execd): stop an isolated run when its script cannot be written
Maohaoac4d39856de6
diff --git a/components/execd/pkg/runtime/isolated_session_ctrl.go b/components/execd/pkg/runtime/isolated_session_ctrl.goindex 021363ef9..18611dfc2 100644--- a/components/execd/pkg/runtime/isolated_session_ctrl.go+++ b/components/execd/pkg/runtime/isolated_session_ctrl.go@@ -674,6 +674,11 @@ func (r *IsolatedRunner) RunInIsolatedSession(ctx context.Context, id string, co}script.WriteString(fmt.Sprintf("echo %s $?\n", runMarker))+ // runCtx is also cancelled when the script cannot be written, so the+ // cancellation below interrupts whatever still holds stdout open.+ runCtx, cancelRun := context.WithCancel(ctx)+ defer cancelRun()+done := make(chan struct{})watcherDone := make(chan struct{})defer func() {@@ -685,7 +690,7 @@ func (r *IsolatedRunner) RunInIsolatedSession(ctx context.Context, id string, cogo func() {defer close(watcherDone)select {- case <-ctx.Done():+ case <-runCtx.Done():_ = s.signalProcessGroupIfRunning(syscall.SIGINT)// No-envs scripts queue their remaining statements in the shell// after the interrupt; repeated SIGINT kills each as it starts@@ -719,13 +724,16 @@ func (r *IsolatedRunner) RunInIsolatedSession(ctx context.Context, id string, cowritten := make(chan error, 1)go func() {_, err := io.WriteString(stdin, script.String())+ if err != nil {+ cancelRun()+ }written <- err}()- exitCode, err := scanUntilMarker(ctx, stdout, runMarker, onStdout)- // Scanning stops at the marker, which the shell reads last, or when the- // shell exits, so the write has returned by then and cannot spill into the- // next run.+ exitCode, err := scanUntilMarker(runCtx, stdout, runMarker, onStdout)+ // Scanning stops at the marker, which the shell reads last, when the shell+ // exits, or after a failed write, so the write has returned by then and+ // cannot spill into the next run.if writeErr := <-written; writeErr != nil && ctx.Err() == nil {return fmt.Errorf("write stdin: %w", writeErr)}diff --git a/components/execd/pkg/runtime/isolated_session_test.go b/components/execd/pkg/runtime/isolated_session_test.goindex 6b20209d9..7f0d6564d 100644--- a/components/execd/pkg/runtime/isolated_session_test.go+++ b/components/execd/pkg/runtime/isolated_session_test.go@@ -706,6 +706,34 @@ func TestRunInIsolatedSession_LargeScriptWithEarlyOutput(t *testing.T) {}}+// A run whose script stops the shell from reading its stdin, while another+// process keeps stdout open, must report the failed write instead of waiting+// for an end marker that never comes.+func TestRunInIsolatedSession_ReportsFailedScriptWrite(t *testing.T) {+ runner := newTestRunner(t)++ id, err := runner.CreateIsolatedSession(&IsolatedSessionOptions{+ WorkspacePath: t.TempDir(),+ WorkspaceMode: "rw",+ })+ if err != nil {+ t.Fatal(err)+ }+ defer runner.DeleteIsolatedSession(id)++ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)+ defer cancel()++ code := "exec sleep 300 </dev/null\n#" + strings.Repeat("x", 150*1024)+ err = runner.RunInIsolatedSession(ctx, id, code, nil, nil)+ if err == nil || !strings.Contains(err.Error(), "write stdin") {+ t.Fatalf("RunInIsolatedSession error = %v, want a write stdin error", err)+ }+ if ctx.Err() != nil {+ t.Fatal("run waited for its deadline instead of reporting the failed write")+ }+}+func TestRunInIsolatedSession_EnvPersistence(t *testing.T) {runner := newTestRunner(t)