containment: retry poll on EINTR on Linux and Darwin
A signal during the exit check in recovery made poll return EINTR, which recovery reported as namespace_identity_unproven. pollFD retries an interrupted poll: a timed poll only within its original timeout, a zero-timeout look until it gets an answer. Recovery and the Linux test helpers (exited, alive, the stop check) now use it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+85
−7
docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md+10 −0internal/runner/containment/linux_test.go+3 −6internal/runner/containment/poll_unix.go+33 −0internal/runner/containment/poll_unix_test.go+38 −0internal/runner/containment/recovery.go+1 −1
diff --git a/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md b/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md| new file mode 100644 |
| index 00000000..a284f75e |
| --- /dev/null |
| +++ b/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md |
| @@ -0,0 +1,10 @@ |
| +# Hosted runners slice 1a: progress and mutation checks |
| + |
| +## Task 1 |
| + |
| +Retry `poll` on `EINTR` on Linux and Darwin (`internal/runner/containment/poll_unix.go`). |
| + |
| +| Mutation | Command | Observed failure | Restored | |
| +| --- | --- | --- | --- | |
| +| `pollRetry` returns on the first `EINTR` (`if e != unix.EINTR \|\| true`) | `go test ./internal/runner/containment -run 'TestPollRetriesEINTR' -count=1` | `poll_unix_test.go:23: interrupted poll not retried: n=0 err=interrupted system call calls=1` | yes, `-run 'TestPollRetr'` passes | |
| +| Extra: the expired deadline reports the `EINTR` instead of a timeout (`return 0, e`) | `go test ./internal/runner/containment -run 'TestPollRetryKeepsDeadline' -count=1` | `poll_unix_test.go:36: retries outlived the timeout: n=0 err=interrupted system call after 120.617911ms` | yes, `-run 'TestPollRetr'` passes | |
diff --git a/internal/runner/containment/linux_test.go b/internal/runner/containment/linux_test.go| index c9cafe4d..77db2e2f 100644 |
| --- a/internal/runner/containment/linux_test.go |
| +++ b/internal/runner/containment/linux_test.go |
| @@ -107,7 +107,7 @@ func descriptor(t *testing.T, pid int) int { |
| func exited(t *testing.T, fd int) { |
| t.Helper() |
| p := []unix.PollFd{{Fd: int32(fd), Events: unix.POLLIN}} |
| - n, e := unix.Poll(p, 5000) |
| + n, e := pollFD(p, 5*time.Second) |
| if e != nil || n != 1 || p[0].Revents&unix.POLLIN == 0 { |
| t.Fatalf("exact process has not exited: poll=%d,%v", n, e) |
| } |
| @@ -128,10 +128,7 @@ func alive(t *testing.T, fd int) { |
| t.Fatalf("owned unrelated control died: %v", e) |
| } |
| p := []unix.PollFd{{Fd: int32(fd), Events: unix.POLLIN}} |
| - n, e := unix.Poll(p, 0) |
| - for e == unix.EINTR { |
| - n, e = unix.Poll(p, 0) |
| - } |
| + n, e := pollFD(p, 0) |
| if e != nil || n != 0 { |
| t.Fatalf("owned unrelated control exited: %d %v", n, e) |
| } |
| @@ -487,7 +484,7 @@ func TestNamespaceStopWaitsForExit(t *testing.T) { |
| t.Fatalf("stop: %+v %v", proof, e) |
| } |
| polls := []unix.PollFd{{Fd: int32(fd), Events: unix.POLLIN}} |
| - n, e := unix.Poll(polls, 0) |
| + n, e := pollFD(polls, 0) |
| if e != nil || n != 1 { |
| t.Fatal("stop acknowledged before exact init exit") |
| } |
diff --git a/internal/runner/containment/poll_unix.go b/internal/runner/containment/poll_unix.go| new file mode 100644 |
| index 00000000..f2b12280 |
| --- /dev/null |
| +++ b/internal/runner/containment/poll_unix.go |
| @@ -0,0 +1,33 @@ |
| +//go:build linux || darwin |
| + |
| +package containment |
| + |
| +import ( |
| + "time" |
| + |
| + "golang.org/x/sys/unix" |
| +) |
| + |
| +// pollFD is unix.Poll that does not report a signal as a failure. |
| +func pollFD(fds []unix.PollFd, timeout time.Duration) (int, error) { |
| + return pollRetry(fds, timeout, unix.Poll) |
| +} |
| + |
| +// pollRetry polls again when a signal interrupts the call. A timed poll |
| +// retries only for the time left of the original timeout and then reports a |
| +// timeout (0, nil). A zero timeout is a look that does not wait, so it is |
| +// retried until it gets an answer. |
| +func pollRetry(fds []unix.PollFd, timeout time.Duration, poll func([]unix.PollFd, int) (int, error)) (int, error) { |
| + deadline := time.Now().Add(timeout) |
| + for { |
| + n, e := poll(fds, int(timeout.Milliseconds())) |
| + if e != unix.EINTR { |
| + return n, e |
| + } |
| + if timeout > 0 { |
| + if timeout = time.Until(deadline); timeout <= 0 { |
| + return 0, nil |
| + } |
| + } |
| + } |
| +} |
diff --git a/internal/runner/containment/poll_unix_test.go b/internal/runner/containment/poll_unix_test.go| new file mode 100644 |
| index 00000000..f1daef4e |
| --- /dev/null |
| +++ b/internal/runner/containment/poll_unix_test.go |
| @@ -0,0 +1,38 @@ |
| +//go:build linux || darwin |
| + |
| +package containment |
| + |
| +import ( |
| + "testing" |
| + "time" |
| + |
| + "golang.org/x/sys/unix" |
| +) |
| + |
| +func TestPollRetriesEINTR(t *testing.T) { |
| + calls := 0 |
| + poll := func([]unix.PollFd, int) (int, error) { |
| + calls++ |
| + if calls <= 2 { |
| + return 0, unix.EINTR |
| + } |
| + return 1, nil |
| + } |
| + n, e := pollRetry(nil, 0, poll) |
| + if n != 1 || e != nil || calls != 3 { |
| + t.Fatalf("interrupted poll not retried: n=%d err=%v calls=%d", n, e, calls) |
| + } |
| +} |
| + |
| +func TestPollRetryKeepsDeadline(t *testing.T) { |
| + poll := func([]unix.PollFd, int) (int, error) { |
| + time.Sleep(30 * time.Millisecond) |
| + return 0, unix.EINTR |
| + } |
| + start := time.Now() |
| + n, e := pollRetry(nil, 100*time.Millisecond, poll) |
| + took := time.Since(start) |
| + if n != 0 || e != nil || took > 200*time.Millisecond { |
| + t.Fatalf("retries outlived the timeout: n=%d err=%v after %v", n, e, took) |
| + } |
| +} |
diff --git a/internal/runner/containment/recovery.go b/internal/runner/containment/recovery.go| index 383cf97f..db103586 100644 |
| --- a/internal/runner/containment/recovery.go |
| +++ b/internal/runner/containment/recovery.go |
| @@ -76,7 +76,7 @@ func exactPID(r Record) (int, error) { |
| } |
| // Check exit before /proc status: an exiting init may already have lost it. |
| polls := []unix.PollFd{{Fd: int32(fd), Events: unix.POLLIN}} |
| - n, e := unix.Poll(polls, 0) |
| + n, e := pollFD(polls, 0) |
| if e == nil && n > 0 && polls[0].Revents&unix.POLLIN != 0 { |
| unix.Close(fd) |
| return -1, errNamespaceExited |