oficina / equipo Public

containment: bound poll retries when the timeout is zero

A zero-timeout poll retried EINTR without limit, so a steady stream of
signals could hold recovery inside exactPID without seeing cancellation.
It now makes at most zeroTimeoutTries (8) calls and then returns the
EINTR, which recovery reports as namespace_identity_unproven. Timed
polls keep their deadline rule.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 975997fff639911374fb6e17b69e0e153d3a7e77 parent ff5066f
3 files changed, +27 −3
  • docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md +1 −0
  • internal/runner/containment/poll_unix.go +11 −3
  • internal/runner/containment/poll_unix_test.go +15 −0
diff --git a/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md b/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md
index 654ef649..b665bc79 100644
--- a/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md
+++ b/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md
@@ -8,6 +8,7 @@ Retry `poll` on `EINTR` on Linux and Darwin (`internal/runner/containment/poll_u
| --- | --- | --- | --- |
| `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 |
+| Fix round 1: remove the zero-timeout cap (`else if false && tries >= zeroTimeoutTries`) | `go test ./internal/runner/containment -run 'TestPollRetryBoundsZeroTimeout' -count=1` | `poll_unix_test.go:38: endless interruption not bounded: n=0 err=<nil> calls=81` | yes, `-run 'TestPollRetr'` passes |
## Task 2
diff --git a/internal/runner/containment/poll_unix.go b/internal/runner/containment/poll_unix.go
index f2b12280..e6b5a6a0 100644
--- a/internal/runner/containment/poll_unix.go
+++ b/internal/runner/containment/poll_unix.go
@@ -8,6 +8,12 @@ import (
"golang.org/x/sys/unix"
)
+// zeroTimeoutTries caps the calls a zero-timeout poll makes when every one
+// is interrupted. Such a poll takes microseconds, so a few tries ride out an
+// odd signal, while a steady stream of signals cannot keep the caller (for
+// example recovery) spinning; it gets the EINTR and fails closed.
+const zeroTimeoutTries = 8
+
// 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)
@@ -15,11 +21,11 @@ func pollFD(fds []unix.PollFd, timeout time.Duration) (int, error) {
// 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.
+// timeout (0, nil). A zero timeout has no time to measure, so it makes at
+// most zeroTimeoutTries calls and then returns the EINTR.
func pollRetry(fds []unix.PollFd, timeout time.Duration, poll func([]unix.PollFd, int) (int, error)) (int, error) {
deadline := time.Now().Add(timeout)
- for {
+ for tries := 1; ; tries++ {
n, e := poll(fds, int(timeout.Milliseconds()))
if e != unix.EINTR {
return n, e
@@ -28,6 +34,8 @@ func pollRetry(fds []unix.PollFd, timeout time.Duration, poll func([]unix.PollFd
if timeout = time.Until(deadline); timeout <= 0 {
return 0, nil
}
+ } else if tries >= zeroTimeoutTries {
+ return 0, e
}
}
}
diff --git a/internal/runner/containment/poll_unix_test.go b/internal/runner/containment/poll_unix_test.go
index f1daef4e..abe13e61 100644
--- a/internal/runner/containment/poll_unix_test.go
+++ b/internal/runner/containment/poll_unix_test.go
@@ -24,6 +24,21 @@ func TestPollRetriesEINTR(t *testing.T) {
}
}
+func TestPollRetryBoundsZeroTimeout(t *testing.T) {
+ calls := 0
+ poll := func([]unix.PollFd, int) (int, error) {
+ calls++
+ if calls > 10*zeroTimeoutTries {
+ return 0, nil // without a cap, stop here so the assertion reports it
+ }
+ return 0, unix.EINTR
+ }
+ n, e := pollRetry(nil, 0, poll)
+ if n != 0 || e != unix.EINTR || calls != zeroTimeoutTries {
+ t.Fatalf("endless interruption not bounded: 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)