Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(daemon): tolerate darwin EPERM while a process group drains (MUL-9162) (#10506)

Jiayuan Zhang90d5d6da6430
diff --git a/server/internal/daemon/processtree/controller_unix.go b/server/internal/daemon/processtree/controller_unix.go
index b75c204704..6454f5d449 100644
--- a/server/internal/daemon/processtree/controller_unix.go
+++ b/server/internal/daemon/processtree/controller_unix.go
@@ -7,6 +7,7 @@ import (
"fmt"
"os"
"os/exec"
+ "runtime"
"syscall"
"time"
)
@@ -30,7 +31,7 @@ func (*controller) interrupt(cmd *exec.Cmd) error {
return os.ErrProcessDone
}
if err := syscall.Kill(-cmd.Process.Pid, syscall.SIGTERM); err != nil {
- if errors.Is(err, syscall.ESRCH) {
+ if errors.Is(err, syscall.ESRCH) || onlyExitingMembers(err) {
return nil
}
return err
@@ -43,7 +44,7 @@ func (*controller) stop(cmd *exec.Cmd) error {
return os.ErrProcessDone
}
if err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL); err != nil {
- if errors.Is(err, syscall.ESRCH) {
+ if errors.Is(err, syscall.ESRCH) || onlyExitingMembers(err) {
return nil
}
return cmd.Process.Kill()
@@ -62,17 +63,30 @@ func (c *controller) finish(cmd *exec.Cmd) error {
// A normally-exited leader can still leave a descendant holding inherited
// pipes or repository locks. Kill the remaining group before returning
// ownership of the repository to another operation.
- if err := syscall.Kill(-pid, syscall.SIGKILL); err != nil && !errors.Is(err, syscall.ESRCH) {
+ if err := syscall.Kill(-pid, syscall.SIGKILL); err != nil && !errors.Is(err, syscall.ESRCH) && !onlyExitingMembers(err) {
return err
}
deadline := time.Now().Add(processTreeFinishTimeout)
+ var lastErr error
for time.Now().Before(deadline) {
- if err := syscall.Kill(-pid, 0); errors.Is(err, syscall.ESRCH) {
+ lastErr = syscall.Kill(-pid, 0)
+ if errors.Is(lastErr, syscall.ESRCH) {
return nil
}
time.Sleep(10 * time.Millisecond)
}
+ if lastErr != nil {
+ return fmt.Errorf("process group %d still active after %s: %w", pid, processTreeFinishTimeout, lastErr)
+ }
return fmt.Errorf("process group %d still active after %s", pid, processTreeFinishTimeout)
}
+// onlyExitingMembers reports whether a kill(-pgid, ...) error means the group
+// still exists but none of its members can take a signal any more. Darwin
+// skips members that are exiting or zombies and then returns EPERM rather than
+// ESRCH while the group lingers; the group disappears once they are reaped.
+func onlyExitingMembers(err error) bool {
+ return runtime.GOOS == "darwin" && errors.Is(err, syscall.EPERM)
+}
+
func (*controller) close() {}
diff --git a/server/internal/daemon/processtree/run_unix_test.go b/server/internal/daemon/processtree/run_unix_test.go
index e792fa56c1..f0098da36f 100644
--- a/server/internal/daemon/processtree/run_unix_test.go
+++ b/server/internal/daemon/processtree/run_unix_test.go
@@ -39,3 +39,15 @@ func TestCombinedOutputKillsDescendantsHoldingOutput(t *testing.T) {
t.Fatalf("descendant survived process-tree cancellation, stat error = %v", err)
}
}
+
+func TestCombinedOutputSucceedsWhileExitedDescendantsDrain(t *testing.T) {
+ t.Parallel()
+
+ // On darwin the group lingers with only exiting members for a few hundred
+ // microseconds; only a few runs per thousand signal it inside that window.
+ for i := 0; i < 1000; i++ {
+ if _, err := CombinedOutput(context.Background(), exec.Command("sh", "-c", "true & exit 0"), time.Second); err != nil {
+ t.Fatalf("run %d: CombinedOutput error = %v, want nil", i, err)
+ }
+ }
+}