runner: bound Mac commands' waits and pace untrusted probe reruns
macHost.run ran each command with exec.CommandContext and Output() and
no WaitDelay. At the timeout Go killed the child alone, and Wait then
waited for the stdout pipe without limit: a grandchild holding it (a
helper xcrun, swift or claude started) kept the runner from its hello.
Every command the hello's capabilities and the probes run goes through
run (mac.Capabilities' Exec and the probe host's Exec are both h.run).
- Each command starts in its own process group (Setpgid). The end of
its context kills the whole group (SIGKILL to -pgid), not only the
child. The group code is unix build-tagged, with a no-op elsewhere.
- WaitDelay is 5 s. A command that exited 0 but left something in its
group holding the pipe answers with what it printed, and that group
is killed.
TestMacRunEndsAHungGrandchild starts a child whose grandchild ignores
SIGTERM and holds stdout. It runs on linux. On the code before this
change:
=== RUN TestMacRunEndsAHungGrandchild
procgroup_unix_test.go:41: run took 15.002s, past its timeout
and WaitDelay (7.3s): err signal: killed
--- FAIL: TestMacRunEndsAHungGrandchild (15.00s)
Now it passes in 0.30 s and the grandchild is gone afterwards.
TestMacRunAnswersPastALingeringHelper covers the exited-0 case.
An untrusted probe report asked for a full probe run on every look,
and every hello and check looks: a Mac whose tools are off their pins
reran the suite on every heartbeat. Now it reruns when the report has
expired, or when what it was made with changes (the version, a cheap
fingerprint of srt and its tools on disk, and whether they match their
pins), or else an hour after the last run, the wait doubling up to a
day. One run is pending at a time. Each reason, and each rerun it asks
for, is warned once. The probe-request file still asks for a run at
once. TestMacProbesUntrustedDoNotStorm: 150 looks at an unchanged
untrusted report give no rerun (78 before this change), a changed
fingerprint gives exactly one, and an hour on gives one more. Two hours
are needed for the next, and a request still runs at once.
The internal/runner/thin size budget goes to its measured 16569; three
reference-scan entries follow the changed mac.go lines.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7 files changed,
+374
−15
.rastrillo/budgets.txt+1 −1internal/runner/thin/mac.go+136 −13internal/runner/thin/probe_storm_test.go+77 −0internal/runner/thin/procgroup_other.go+12 −0internal/runner/thin/procgroup_unix.go+29 −0internal/runner/thin/procgroup_unix_test.go+104 −0scripts/runner-reference-allowlist.json+15 −1
CI failed — run details
diff --git a/.rastrillo/budgets.txt b/.rastrillo/budgets.txt| index 181b52bb..b8a7eab0 100644 |
| --- a/.rastrillo/budgets.txt |
| +++ b/.rastrillo/budgets.txt |
| @@ -34,7 +34,7 @@ time internal/runner/provider 15s Plan 3 CI repair measured 10.346s for the full |
| perf GET /releases/* skip Verified runner release file downloads, not browser GET screens. |
| perf GET /install.sh skip Mac runners: the one-line bootstrap a terminal pipes to sh, not a browser screen. |
| time internal/runner/containment 15s Plan 3 loaded CI measured 10.817s; niced serial reruns measured 5.773s and 5.750s. The added coordinator mount check has no repeated launches to remove; 15s leaves 38 percent headroom for namespace, process-exit and recovery assertions. |
| -size internal/runner/thin 16405 - Runner startup logs add the info lines a silent start is pinned to a phase by (startup phases, the Mac host's xcode-select and capability commands, slow-command warnings, the probes, and the link's connecting, hello and welcome lines: startPhase, openRunner, watchSlow and logged, about 155 lines); measured 16405. Merge authority thin v1 adds the review's measured change facts (facts.go: raw and numstat listings, modes, signing lines, the revert tree check, about 230 lines), the merge_request confirmation and the merge's provenance; measured 16196 with spend-1 merged. Spend 1 resumes a fix round in Engineering's kept session on its healthy connection (fixSession, fixPlan, fixPrompt, keptCommit, about 90 lines); measured 15873. Claude's sign-in by a link's review round 1 contains its setup-token in a recorded namespace (the contained terminal, its proven stop and the start's recovery before a home goes, about 130 lines); measured 15787. Claude's sign-in by a link adds the runner's own setup-token attempt, the code's sealed delivery with its crash boundaries and the shared token commit (signin.go, about 300 lines, less the removed terminal watcher); measured 15655. Claude setup tokens add sealed delivery, the token check, Check again and recovery (token.go, tokencheck.go, about 700 lines) beside the composer and Mac lines; measured 15239. Astra round 2 moves the attachment quota to admission: arriving files counted by declared size, admissions protecting a file until its message names it, room made from the oldest unprotected files, refusal as runner_storage_full, and a sweep that stops when protection cannot be read (about 150 lines); measured 12191. Mac runners slices A and B add protocol negotiation (link.go), the capability report, the start authorization, the pack relay (relay.go), the primary's publication, the worker executor (worker.go) and the test-build fake worker, about 2700 lines on the same step engine. Composer attachments add the chunked receipt with exactly-once writes, the read-only desk delivery and its checks (files.go, about 230 lines), image blocks and localImage inputs, the attachment read through read_file; measured 11736. Connections slice 1 Astra round 1 adds the Codex step path (codexstep.go: Engineering and Review on the pinned app-server in the implement and inspect containment, about 300 lines); measured 11345. Connections slice 1 (after Astra round 1) also keeps a durable ledger of each turn's write tools so a later attempt never repeats one (effects.go), the waiting state between attempts and usage rows per attempt, about 500 lines more; measured 10590 before gate 1d. Connections slice 1 adds the Codex coordinator beside the Claude one (codex.go: the pinned app-server over JSON-RPC in the converse containment, sharing the coordinator base, desk, tool socket and session table), the connection registry over the bindings with health and provider limits (conns.go), in-app sign-in attempts (connector.go) and turn failover and holding (failover.go), about 1500 lines; the pure parts (the choice function, limit parsing, the app-server client, the sign-in flows) live in internal/runner/connect. Measured 10097. Thin v1 UX1 adds step activity lines from the specialist's stream, the repository head and checks gate in the index and project-tagged notes (about 120 lines); measured 8615 with slice 4c's review fixes. Thin v1 slice 4c adds the merge step (mergestep.go, about 190 lines) and the rebase round beside the slice 4b ask fixes. Thin v1 slice 3 adds Engineering steps beside the coordinator: the durable step state and engine (step.go) and the executor that checks out the task tree, runs the CLI in the implement containment, commits and pushes (engineer.go), about 800 lines sharing the coordinator's launch, tool socket and account selection. Measured 5686 source lines. Splitting would export the launch internals; revisit at the slice 7 sweep. Slice 4 adds checks and review on a candidate (judge.go) and the merge watch (merge.go), about 800 lines, sharing the Engineer launch path; measured 6808, 7035 after review round 1 (branch head watch, deadlines, streamed outputs). Slice 4b keeps Engineering and Review sessions and answers the creator's questions from them (ask.go: kept-session and ask state, read-only git tools, the answer stream), about 700 lines on the same launch path. |
| +size internal/runner/thin 16569 - The Mac host's hang fix and probe pacing add the process group and its kill (procgroup_unix.go, procgroup_other.go), the bounded wait for a command's output, and the untrusted report's rerun decision with its tool fingerprint (about 165 lines); measured 16569. Runner startup logs add the info lines a silent start is pinned to a phase by (startup phases, the Mac host's xcode-select and capability commands, slow-command warnings, the probes, and the link's connecting, hello and welcome lines: startPhase, openRunner, watchSlow and logged, about 155 lines); measured 16405. Merge authority thin v1 adds the review's measured change facts (facts.go: raw and numstat listings, modes, signing lines, the revert tree check, about 230 lines), the merge_request confirmation and the merge's provenance; measured 16196 with spend-1 merged. Spend 1 resumes a fix round in Engineering's kept session on its healthy connection (fixSession, fixPlan, fixPrompt, keptCommit, about 90 lines); measured 15873. Claude's sign-in by a link's review round 1 contains its setup-token in a recorded namespace (the contained terminal, its proven stop and the start's recovery before a home goes, about 130 lines); measured 15787. Claude's sign-in by a link adds the runner's own setup-token attempt, the code's sealed delivery with its crash boundaries and the shared token commit (signin.go, about 300 lines, less the removed terminal watcher); measured 15655. Claude setup tokens add sealed delivery, the token check, Check again and recovery (token.go, tokencheck.go, about 700 lines) beside the composer and Mac lines; measured 15239. Astra round 2 moves the attachment quota to admission: arriving files counted by declared size, admissions protecting a file until its message names it, room made from the oldest unprotected files, refusal as runner_storage_full, and a sweep that stops when protection cannot be read (about 150 lines); measured 12191. Mac runners slices A and B add protocol negotiation (link.go), the capability report, the start authorization, the pack relay (relay.go), the primary's publication, the worker executor (worker.go) and the test-build fake worker, about 2700 lines on the same step engine. Composer attachments add the chunked receipt with exactly-once writes, the read-only desk delivery and its checks (files.go, about 230 lines), image blocks and localImage inputs, the attachment read through read_file; measured 11736. Connections slice 1 Astra round 1 adds the Codex step path (codexstep.go: Engineering and Review on the pinned app-server in the implement and inspect containment, about 300 lines); measured 11345. Connections slice 1 (after Astra round 1) also keeps a durable ledger of each turn's write tools so a later attempt never repeats one (effects.go), the waiting state between attempts and usage rows per attempt, about 500 lines more; measured 10590 before gate 1d. Connections slice 1 adds the Codex coordinator beside the Claude one (codex.go: the pinned app-server over JSON-RPC in the converse containment, sharing the coordinator base, desk, tool socket and session table), the connection registry over the bindings with health and provider limits (conns.go), in-app sign-in attempts (connector.go) and turn failover and holding (failover.go), about 1500 lines; the pure parts (the choice function, limit parsing, the app-server client, the sign-in flows) live in internal/runner/connect. Measured 10097. Thin v1 UX1 adds step activity lines from the specialist's stream, the repository head and checks gate in the index and project-tagged notes (about 120 lines); measured 8615 with slice 4c's review fixes. Thin v1 slice 4c adds the merge step (mergestep.go, about 190 lines) and the rebase round beside the slice 4b ask fixes. Thin v1 slice 3 adds Engineering steps beside the coordinator: the durable step state and engine (step.go) and the executor that checks out the task tree, runs the CLI in the implement containment, commits and pushes (engineer.go), about 800 lines sharing the coordinator's launch, tool socket and account selection. Measured 5686 source lines. Splitting would export the launch internals; revisit at the slice 7 sweep. Slice 4 adds checks and review on a candidate (judge.go) and the merge watch (merge.go), about 800 lines, sharing the Engineer launch path; measured 6808, 7035 after review round 1 (branch head watch, deadlines, streamed outputs). Slice 4b keeps Engineering and Review sessions and answers the creator's questions from them (ask.go: kept-session and ask state, read-only git tools, the answer stream), about 700 lines on the same launch path. |
| time internal/thintest 180s Mac runners slices A and B add role, scoping, migration, placement, withdrawal, transfer and publication contracts, and Astra round 1 eight transition contracts (about 15s; the package measured 126s locally under load). Housekeeping slice 1 adds six contract tests (combine, Undo round trips and the race, Pause, auto-disable, quiet hours, restart while claimed; about 6s); the package measured 67.7s beside other packages locally, so 75s. Thin v1 slice 4c adds the merge, adoption, cancel and retention contract tests (CI measured 35.7s). Thin v1 contract tests: a real instance and a scripted runner per test, fixtures replayed against every request, frame and event. Slice 3 adds the task, step-sync and grant contracts; CI measured 14.805s. 30s keeps room for the slice 4 review contracts. UX1 adds step activity, task source and duplicates, coordinator merging and project facts (four contracts, about 6s); the package measured 34s locally under load, so 40s. UX1's inner screens gallery (two owners, seven tasks, every page) adds about 3s; measured 40.9s locally under load, so 60s. |
| time internal/runner/mac 30s Mac runners (Darwin worker): real process groups killed after a grace and proven gone (SIGTERM, SIGKILL, the marker sweep), and the probes run end to end with fake srt and xcodebuild, P6 killing a waiting test two seconds after it starts. About 6s alone; loaded CI measured 12.5s. |
| time internal/runner/thin 45s Thin v1 slice 4 review: real git repositories for the merge and squash checks, a stalled fetch, and a 16 MiB candidate streamed through the review diff and the checks pack (about 5s). Measured 17.8s locally under load. Slice 4b review adds a 16 MiB git output bound, an injected index-pack failure and stall (3s deadline) and held-answer tests; measured 30.3s locally under load. 45s keeps 33 percent headroom. |
diff --git a/internal/runner/thin/mac.go b/internal/runner/thin/mac.go| index 3cd313e8..365af061 100644 |
| --- a/internal/runner/thin/mac.go |
| +++ b/internal/runner/thin/mac.go |
| @@ -13,6 +13,7 @@ import ( |
| "os" |
| "os/exec" |
| "path/filepath" |
| + "strconv" |
| "strings" |
| "sync" |
| "sync/atomic" |
| @@ -124,26 +125,53 @@ const probeTrust = 24 * time.Hour |
| // for this checks profile, made with the srt, Node and ripgrep that are |
| // on disk now matching the pins compiled into this binary (verified), |
| // and less than a day old; otherwise checks |
| -// run as not verified and the probes run again. `equipo-runner probe` |
| -// only asks for that (request): a forged request costs a probe run and |
| -// never upgrades trust. |
| +// run as not verified. The probes run again only when that can change |
| +// the answer (see untrusted), not on every look. `equipo-runner probe` |
| +// asks for a run at once (request): a forged request costs a probe run |
| +// and never upgrades trust. |
| type macProbes struct { |
| version string |
| run func(context.Context) mac.Report |
| verified func() bool |
| - request string |
| - now func() time.Time |
| - // log hears each run start and finish, with the verdicts. |
| + // fingerprint names the tools on disk a report depends on (cheaply: |
| + // paths, link targets, sizes and times); nil is "". |
| + fingerprint func() string |
| + request string |
| + now func() time.Time |
| + // log hears each run start and finish, with the verdicts, and why a |
| + // report is not trusted. |
| log *slog.Logger |
| mu sync.Mutex |
| last mac.Report |
| ready chan struct{} |
| again chan struct{} |
| + // What the last run was made with and when it started; whether a |
| + // run asked for by untrusted has yet to report; the wait before the |
| + // next run of an unchanged untrusted report; the last reason warned. |
| + ranWith string |
| + ranAt time.Time |
| + pending bool |
| + backoff time.Duration |
| + warned string |
| } |
| +// probeRetry is the first wait before an untrusted report whose inputs |
| +// have not changed is probed again; it doubles up to probeTrust. |
| +const probeRetry = time.Hour |
| + |
| func newMacProbes(version, request string, run func(context.Context) mac.Report, verified func() bool) *macProbes { |
| - return &macProbes{version: version, request: request, run: run, verified: verified, now: time.Now, ready: make(chan struct{}), again: make(chan struct{}, 1)} |
| + return &macProbes{version: version, request: request, run: run, verified: verified, now: time.Now, ready: make(chan struct{}), again: make(chan struct{}, 1), backoff: probeRetry} |
| +} |
| + |
| +// inputs is what a report depends on: this binary's version, the tools' |
| +// fingerprint and whether they match their pins. |
| +func (p *macProbes) inputs(verified bool) string { |
| + fp := "" |
| + if p.fingerprint != nil { |
| + fp = p.fingerprint() |
| + } |
| + return p.version + "|" + strconv.FormatBool(verified) + "|" + fp |
| } |
| // report is the newest report, for the instance to show. |
| @@ -156,13 +184,68 @@ func (p *macProbes) report() mac.Report { |
| // trusted is the newest report and whether it may decide a gate now. |
| func (p *macProbes) trusted() (mac.Report, bool) { |
| r := p.report() |
| - ok := r.Version == p.version && r.Profile == mac.ProfileDigest() && r.SRT == srtPinned && p.verified() && !r.At.IsZero() && p.now().Sub(r.At) < probeTrust && p.now().Sub(r.At) >= -time.Minute |
| + verified, now := p.verified(), p.now() |
| + ok := r.Version == p.version && r.Profile == mac.ProfileDigest() && r.SRT == srtPinned && verified && !r.At.IsZero() && now.Sub(r.At) < probeTrust && now.Sub(r.At) >= -time.Minute |
| if !ok && !r.At.IsZero() { |
| - p.probeSoon() |
| + p.untrusted(r, verified, now) |
| } |
| return r, ok |
| } |
| +// untrusted decides whether an untrusted report is probed again. Every |
| +// hello and every check looks, so asking each time would run the whole |
| +// suite on every heartbeat. A run is asked for when the report expired, |
| +// or what it was made with changed (the version, the tools' fingerprint, |
| +// whether they match their pins), or else once probeRetry has passed |
| +// since the last run, the wait doubling each time up to a day. One run |
| +// at a time: none is asked for while one asked for has yet to report. |
| +// Each reason, and each run it asks for, is warned once. |
| +func (p *macProbes) untrusted(r mac.Report, verified bool, now time.Time) { |
| + reason := "" |
| + switch { |
| + case r.Version != p.version || r.Profile != mac.ProfileDigest(): |
| + reason = "the report is another runner version's or checks profile's" |
| + case !verified: |
| + reason = "srt or its tools on disk do not match their pins" |
| + case r.SRT != srtPinned: |
| + reason = "the report was made with tools that did not match their pins" |
| + case now.Sub(r.At) < -time.Minute: |
| + reason = "the report is dated in the future" |
| + default: |
| + reason = "the report is more than a day old" |
| + } |
| + expired := now.Sub(r.At) >= probeTrust || now.Sub(r.At) < -time.Minute |
| + inputs := p.inputs(verified) |
| + p.mu.Lock() |
| + again, why := false, "" |
| + switch { |
| + case p.pending: |
| + case expired: |
| + again, why = true, "expired" |
| + case inputs != p.ranWith: |
| + again, why, p.backoff = true, "its inputs changed", probeRetry |
| + case now.Sub(p.ranAt) >= p.backoff: |
| + again, why = true, "retry after "+p.backoff.String() |
| + p.backoff = min(2*p.backoff, probeTrust) |
| + } |
| + if again { |
| + p.pending = true |
| + } |
| + warn := again || reason != p.warned |
| + p.warned = reason |
| + p.mu.Unlock() |
| + if warn && p.log != nil { |
| + attrs := []any{"reason", reason, "probe_again", again} |
| + if again { |
| + attrs = append(attrs, "because", why) |
| + } |
| + p.log.Warn("probe report not trusted", attrs...) |
| + } |
| + if again { |
| + p.probeSoon() |
| + } |
| +} |
| + |
| func (p *macProbes) probeSoon() { |
| select { |
| case p.again <- struct{}{}: |
| @@ -182,9 +265,13 @@ func (p *macProbes) start(ctx context.Context, refresh func()) { |
| // after them: a report made with anything else is never |
| // trusted. |
| before := p.verified() |
| - // The first run at info; later ones (a day on, or asked for |
| - // again while the report is not trusted, which can be every |
| - // hello) at debug unless their verdicts changed. |
| + inputs := p.inputs(before) |
| + p.mu.Lock() |
| + p.ranWith, p.ranAt = inputs, p.now() |
| + p.mu.Unlock() |
| + // The first run at info; later ones (a day on, asked for, or |
| + // an untrusted report's retry) at debug unless their verdicts |
| + // changed. |
| start, level := time.Now(), slog.LevelDebug |
| if first { |
| level = slog.LevelInfo |
| @@ -207,7 +294,7 @@ func (p *macProbes) start(ctx context.Context, refresh func()) { |
| r.SRT = srtPinned |
| } |
| p.mu.Lock() |
| - p.last = r |
| + p.last, p.pending = r, false |
| p.mu.Unlock() |
| if first { |
| close(p.ready) |
| @@ -263,6 +350,24 @@ func (p *macProbes) wait(ctx context.Context) error { |
| } |
| } |
| +// toolFingerprint names the tools a probe report depends on without |
| +// reading them (verified hashes them): each one's path, link target, |
| +// size, mode and modification time, and the srt package tree's. |
| +func toolFingerprint(installDir string, t mac.Tools) string { |
| + var b strings.Builder |
| + for _, path := range []string{t.SRT, t.Node, t.RG, filepath.Join(installDir, "srt", "node_modules")} { |
| + b.WriteString(path) |
| + if target, err := os.Readlink(path); err == nil { |
| + b.WriteString(" -> " + target) |
| + } |
| + if info, err := os.Stat(path); err == nil { |
| + b.WriteString(" " + strconv.FormatInt(info.Size(), 10) + " " + info.Mode().String() + " " + strconv.FormatInt(info.ModTime().UnixNano(), 10)) |
| + } |
| + b.WriteByte('\n') |
| + } |
| + return b.String() |
| +} |
| + |
| // srtPinned marks a report made with srt, Node and ripgrep matching the |
| // pins compiled into this binary. |
| const srtPinned = "pinned" |
| @@ -292,6 +397,11 @@ type macHost struct { |
| // it is logged as slow: while it still runs, so a hung child shows. |
| var slowCommand = 10 * time.Second |
| +// commandWaitDelay bounds how long run waits for a command's output once |
| +// it has exited or been killed: a grandchild still holding the pipe |
| +// cannot hold the runner. |
| +var commandWaitDelay = 5 * time.Second |
| + |
| // Mac tool discovery (spec §3.3): EQUIPO_SRT_PATH and the installer's |
| // private srt first, then the usual places. |
| func macTools(installDir, home string, getenv func(string) string) mac.Tools { |
| @@ -328,8 +438,20 @@ func executable(p string) bool { |
| func (h *macHost) run(ctx context.Context, name string, args ...string) (string, error) { |
| cmd := exec.CommandContext(ctx, name, args...) |
| cmd.Env = withPath(os.Environ(), gatePath(h.tools)) |
| + // Its own process group, killed whole at the timeout, and a bounded |
| + // wait for its output: a grandchild holding the pipe (xcrun, swift |
| + // or claude starting a helper) cannot block run past its timeout. |
| + ownGroup(cmd) |
| + cmd.WaitDelay = commandWaitDelay |
| done := h.watchSlow(name, args) |
| out, err := cmd.Output() |
| + if errors.Is(err, exec.ErrWaitDelay) { |
| + // The command itself exited successfully; only something it left |
| + // behind in its group still held the pipe. That goes, and what |
| + // the command printed is its answer. |
| + _ = killGroup(cmd) |
| + err = nil |
| + } |
| done(err) |
| return string(out), err |
| } |
| @@ -475,6 +597,7 @@ func newMacHost(c runner.Config, o Options) (h *macHost, err error) { |
| return mac.RunProbes(ctx, h.probeHost(o.Version, gate)) |
| }, func() bool { return mac.VerifyTools(h.paths.InstallDir, h.tools) == nil }) |
| h.probes.log = o.Logger |
| + h.probes.fingerprint = func() string { return toolFingerprint(h.paths.InstallDir, h.tools) } |
| h.taint = loadTaint(filepath.Join(c.StateDir, "tainted")) |
| h.checks = &macChecks{probes: h.probes, taint: h.taint, srt: h.tools.SRT, sandboxed: gate(h.tools.SRT, wire.SandboxSeatbelt), unproven: gate(h.tools.SRT, wire.SandboxNone), bare: gate("", wire.SandboxNone)} |
| h.caps = func(ctx context.Context) wire.Capabilities { |
diff --git a/internal/runner/thin/probe_storm_test.go b/internal/runner/thin/probe_storm_test.go| new file mode 100644 |
| index 00000000..30e49236 |
| --- /dev/null |
| +++ b/internal/runner/thin/probe_storm_test.go |
| @@ -0,0 +1,77 @@ |
| +package thin |
| + |
| +import ( |
| + "log/slog" |
| + "strings" |
| + "sync/atomic" |
| + "testing" |
| + "time" |
| +) |
| + |
| +// An untrusted report is not probed again on every look (every hello |
| +// and check looks): only when what it was made with changes, or an hour |
| +// on with the wait doubling, and each decision is warned once. A request |
| +// (equipo-runner probe) still runs the probes at once. |
| +func TestMacProbesUntrustedDoNotStorm(t *testing.T) { |
| + x := newProbeRig(t) |
| + x.pinned = false |
| + var fp atomic.Value |
| + fp.Store("tools-a") |
| + x.p.fingerprint = func() string { return fp.Load().(string) } |
| + logs := &syncBuffer{} |
| + x.p.log = slog.New(slog.NewTextHandler(logs, nil)) |
| + x.started(t) |
| + look := func() { |
| + t.Helper() |
| + for range 50 { |
| + if _, ok := x.p.trusted(); ok { |
| + t.Fatal("a report made with tools off their pins trusted") |
| + } |
| + } |
| + } |
| + runs := func(want int32) { |
| + t.Helper() |
| + look() |
| + deadline := time.Now().Add(5 * time.Second) |
| + for { |
| + x.p.mu.Lock() |
| + pending := x.p.pending |
| + x.p.mu.Unlock() |
| + if x.runs.Load() >= want && !pending { |
| + break |
| + } |
| + if time.Now().After(deadline) { |
| + t.Fatalf("%d probe runs, want %d (pending %v):\n%s", x.runs.Load(), want, pending, logs) |
| + } |
| + time.Sleep(10 * time.Millisecond) |
| + } |
| + look() |
| + time.Sleep(100 * time.Millisecond) |
| + if n := x.runs.Load(); n != want { |
| + t.Fatalf("%d probe runs, want %d:\n%s", n, want, logs) |
| + } |
| + } |
| + warned := func() int { return strings.Count(logs.String(), `level=WARN msg="probe report not trusted"`) } |
| + |
| + runs(1) |
| + if n := warned(); n != 1 || !strings.Contains(logs.String(), "probe_again=false") { |
| + t.Fatalf("an unchanged untrusted report warned %d times:\n%s", n, logs) |
| + } |
| + fp.Store("tools-b") |
| + runs(2) |
| + if !strings.Contains(logs.String(), `because="its inputs changed"`) { |
| + t.Fatalf("no warning for the rerun:\n%s", logs) |
| + } |
| + x.now = x.now.Add(probeRetry) |
| + runs(3) |
| + // The next retry waits two hours: one more hour is not enough. |
| + x.now = x.now.Add(probeRetry) |
| + runs(3) |
| + x.now = x.now.Add(probeRetry) |
| + runs(4) |
| + if n := warned(); n != 4 { |
| + t.Fatalf("%d warnings for 3 reruns and the first look:\n%s", n, logs) |
| + } |
| + x.p.probeSoon() |
| + runs(5) |
| +} |
diff --git a/internal/runner/thin/procgroup_other.go b/internal/runner/thin/procgroup_other.go| new file mode 100644 |
| index 00000000..78eca926 |
| --- /dev/null |
| +++ b/internal/runner/thin/procgroup_other.go |
| @@ -0,0 +1,12 @@ |
| +//go:build !unix |
| + |
| +package thin |
| + |
| +import "os/exec" |
| + |
| +// ownGroup leaves cmd as it is where there are no Unix process groups: |
| +// its context kills cmd alone, and WaitDelay still bounds the wait. |
| +func ownGroup(*exec.Cmd) {} |
| + |
| +// killGroup has no group to kill here. |
| +func killGroup(*exec.Cmd) error { return nil } |
diff --git a/internal/runner/thin/procgroup_unix.go b/internal/runner/thin/procgroup_unix.go| new file mode 100644 |
| index 00000000..96b560c7 |
| --- /dev/null |
| +++ b/internal/runner/thin/procgroup_unix.go |
| @@ -0,0 +1,29 @@ |
| +//go:build unix |
| + |
| +package thin |
| + |
| +import ( |
| + "errors" |
| + "os/exec" |
| + "syscall" |
| +) |
| + |
| +// ownGroup starts cmd in a process group of its own and makes the end of |
| +// its context kill that whole group, not only cmd: a grandchild that |
| +// ignores SIGTERM and holds cmd's output dies with it. |
| +func ownGroup(cmd *exec.Cmd) { |
| + cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} |
| + cmd.Cancel = func() error { return killGroup(cmd) } |
| +} |
| + |
| +// killGroup sends SIGKILL to cmd's process group (-pgid); a group that |
| +// is already gone is not an error. |
| +func killGroup(cmd *exec.Cmd) error { |
| + if cmd.Process == nil { |
| + return nil |
| + } |
| + if err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL); err != nil && !errors.Is(err, syscall.ESRCH) { |
| + return err |
| + } |
| + return nil |
| +} |
diff --git a/internal/runner/thin/procgroup_unix_test.go b/internal/runner/thin/procgroup_unix_test.go| new file mode 100644 |
| index 00000000..c17b2879 |
| --- /dev/null |
| +++ b/internal/runner/thin/procgroup_unix_test.go |
| @@ -0,0 +1,104 @@ |
| +//go:build unix |
| + |
| +package thin |
| + |
| +import ( |
| + "context" |
| + "os" |
| + "path/filepath" |
| + "strconv" |
| + "strings" |
| + "syscall" |
| + "testing" |
| + "time" |
| +) |
| + |
| +// A command whose grandchild ignores SIGTERM and holds its output open |
| +// cannot hold run past its timeout: the whole process group is killed |
| +// when the context ends, and WaitDelay bounds the wait for the pipes. |
| +// The grandchild is gone afterwards. |
| +func TestMacRunEndsAHungGrandchild(t *testing.T) { |
| + pidFile := filepath.Join(t.TempDir(), "grandchild") |
| + pid := 0 |
| + t.Cleanup(func() { |
| + // Only the grandchild this test started, by pid, if it is still |
| + // that sleep. |
| + if pid > 0 && grandchildAlive(pid) { |
| + _ = syscall.Kill(pid, syscall.SIGKILL) |
| + } |
| + }) |
| + const timeout = 300 * time.Millisecond |
| + script := `sh -c 'trap "" TERM; echo $$ > "$1"; exec sleep 15' grandchild "$1" & wait` |
| + ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| + defer cancel() |
| + start := time.Now() |
| + _, err := (&macHost{}).run(ctx, "/bin/sh", "-c", script, "child", pidFile) |
| + took := time.Since(start) |
| + if raw, rerr := os.ReadFile(pidFile); rerr == nil { |
| + pid, _ = strconv.Atoi(strings.TrimSpace(string(raw))) |
| + } |
| + if limit := timeout + commandWaitDelay + 2*time.Second; took > limit { |
| + t.Fatalf("run took %s, past its timeout and WaitDelay (%s): err %v", took.Round(time.Millisecond), limit, err) |
| + } |
| + if err == nil { |
| + t.Fatal("a command killed at its timeout succeeded") |
| + } |
| + if pid <= 0 { |
| + t.Fatal("the grandchild never started") |
| + } |
| + for deadline := time.Now().Add(2 * time.Second); grandchildAlive(pid); time.Sleep(20 * time.Millisecond) { |
| + if time.Now().After(deadline) { |
| + t.Fatalf("the grandchild %d outlived run", pid) |
| + } |
| + } |
| +} |
| + |
| +// A command that exits at once but leaves a helper holding its output |
| +// answers within WaitDelay with what it printed, and the helper, left in |
| +// the command's own group, is gone. |
| +func TestMacRunAnswersPastALingeringHelper(t *testing.T) { |
| + was := commandWaitDelay |
| + commandWaitDelay = 200 * time.Millisecond |
| + defer func() { commandWaitDelay = was }() |
| + pidFile := filepath.Join(t.TempDir(), "helper") |
| + pid := 0 |
| + t.Cleanup(func() { |
| + if pid > 0 && grandchildAlive(pid) { |
| + _ = syscall.Kill(pid, syscall.SIGKILL) |
| + } |
| + }) |
| + script := `echo 1.2.3; sh -c 'echo $$ > "$1"; exec sleep 15' helper "$1" & while [ ! -s "$1" ]; do sleep 0.01; done` |
| + start := time.Now() |
| + out, err := (&macHost{}).run(context.Background(), "/bin/sh", "-c", script, "child", pidFile) |
| + took := time.Since(start) |
| + if raw, rerr := os.ReadFile(pidFile); rerr == nil { |
| + pid, _ = strconv.Atoi(strings.TrimSpace(string(raw))) |
| + } |
| + if pid <= 0 { |
| + t.Fatal("the helper never started") |
| + } |
| + if err != nil || out != "1.2.3\n" || took > 3*time.Second { |
| + t.Fatalf("run answered %q %v after %s", out, err, took.Round(time.Millisecond)) |
| + } |
| + for deadline := time.Now().Add(2 * time.Second); pid > 0 && grandchildAlive(pid); time.Sleep(20 * time.Millisecond) { |
| + if time.Now().After(deadline) { |
| + t.Fatalf("the helper %d outlived run", pid) |
| + } |
| + } |
| +} |
| + |
| +// grandchildAlive: pid is a live process (not a zombie) running sleep. |
| +func grandchildAlive(pid int) bool { |
| + if syscall.Kill(pid, 0) != nil { |
| + return false |
| + } |
| + if stat, err := os.ReadFile("/proc/" + strconv.Itoa(pid) + "/stat"); err == nil { |
| + if i := strings.LastIndexByte(string(stat), ')'); i > 0 && i+2 < len(stat) && stat[i+2] == 'Z' { |
| + return false |
| + } |
| + } |
| + if cmdline, err := os.ReadFile("/proc/" + strconv.Itoa(pid) + "/cmdline"); err == nil { |
| + return strings.Contains(string(cmdline), "sleep") |
| + } |
| + return true |
| +} |
diff --git a/scripts/runner-reference-allowlist.json b/scripts/runner-reference-allowlist.json| index 07a6ebad..7cbf4110 100644 |
| --- a/scripts/runner-reference-allowlist.json |
| +++ b/scripts/runner-reference-allowlist.json |
| @@ -10382,7 +10382,7 @@ |
| }, |
| { |
| "path": "internal/runner/thin/mac.go", |
| - "line_sha256": "cf8018bd71b2fac45dcea98e9f738fc5b20ae35a34133b27198635cd160a72c4", |
| + "line_sha256": "b26743611f2f5ce41538d4cd3dd9cc0215ce18b9410a5307b973ba30ab7b8bf4", |
| "match": "equipo-runner", |
| "count": 1, |
| "reason": "Mac runner (darwin worker): the runner executable, its launchd agent, install paths, the pinned srt package and Node it needs, and their tests and docs." |
| @@ -11086,5 +11086,19 @@ |
| "match": "NODE", |
| "count": 1, |
| "reason": "Mac runner (darwin worker): the runner executable, its launchd agent, install paths, the pinned srt package and Node it needs, and their tests and docs." |
| + }, |
| + { |
| + "path": "internal/runner/thin/mac.go", |
| + "line_sha256": "aeb7fbbdabfa876ff3091fb2e4961286eb2c2913fdc59d680bcf81d52e8a17d5", |
| + "match": "Node", |
| + "count": 1, |
| + "reason": "Mac runner (darwin worker): the runner executable, its launchd agent, install paths, the pinned srt package and Node it needs, and their tests and docs." |
| + }, |
| + { |
| + "path": "internal/runner/thin/mac.go", |
| + "line_sha256": "aeb7fbbdabfa876ff3091fb2e4961286eb2c2913fdc59d680bcf81d52e8a17d5", |
| + "match": "node", |
| + "count": 1, |
| + "reason": "Mac runner (darwin worker): the runner executable, its launchd agent, install paths, the pinned srt package and Node it needs, and their tests and docs." |
| } |
| ] |