generation census: one shared 20 s deadline for every pass and its reads
Astra (fix round 1, finding 3): the window was checked only after a pass returned and before a wait, so a pass starting near the end could run on, a slow pass that succeeded late was accepted, and no deadline reached the lookup's systemctl show calls. - generationCensusRetryFor sets one deadline, window from its start, and hands every pass a context that ends there. Each pass's lookup is built on that context, so systemctl show (CommandContext) and the deleted-image hash (now read through a context-checking reader) stop at it. - A pass that ends past the deadline refuses, even if it succeeded, with errGenerationCensusLate; no pass starts after it, including after a wait that overran. - The note states the real worst-case boot delay on the recover path: about 90 s, at most 150 s, under TimeoutStartSec=10min.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+203
−27
docs/superpowers/notes/census-proposal-claims-on-gate-recover.md+26 −0internal/host/plan14_generation_kernel_linux_test.go+2 −2internal/host/rootgeneration_census_linux.go+43 −16internal/host/rootgeneration_census_recover_linux_test.go+115 −6internal/host/rootgeneration_services_linux.go+17 −3
diff --git a/docs/superpowers/notes/census-proposal-claims-on-gate-recover.md b/docs/superpowers/notes/census-proposal-claims-on-gate-recover.md| index a5a897613..5c295fb19 100644 |
| --- a/docs/superpowers/notes/census-proposal-claims-on-gate-recover.md |
| +++ b/docs/superpowers/notes/census-proposal-claims-on-gate-recover.md |
| @@ -85,6 +85,32 @@ reference checks: exe, cwd, root, descriptors, maps and mounts are still |
| compared against the retired generations |
| (`TestRecoverCensusStillChecksReferencesOfAnAcceptedDeletedExe`). |
| +## The census window and the worst-case boot delay |
| + |
| +Each call of root's kernel census runs under one deadline, 20 s from its |
| +start (`generationCensusRetryFor`). Every pass gets a context that ends at |
| +the deadline. The lookup's blocking reads obey it: `systemctl show` through |
| +`exec.CommandContext`, and the deleted-image hash, which stops between |
| +reads. A pass that ends past the deadline refuses even if it succeeded, and |
| +no pass starts after it. What the window does not cover: the versions-tree |
| +walk and the mount checks before the first pass (bounded at 8192 entries), |
| +and one `/proc` read already in flight when the deadline passes. |
| + |
| +On the recover path, with the census refusing every time, the edge's start |
| +waits at most for, in order: |
| + |
| +1. the guard-exec recover prelude's census (`rootFloorDowngradeIdle`), when |
| + a moves floor is armed: 20 s; |
| +2. recover's first controllers census: 20 s; |
| +3. root's pending-controller recovery (`recoverRootExecution`), which runs |
| + under its own 30 s context, plus two cleanups of up to 30 s each; |
| +4. recover's second controllers census: 20 s. |
| + |
| +Then recover defers and exits 0. That is about 90 s, and at most 150 s when |
| +both cleanups run to their bounds. The recover unit's TimeoutStartSec is |
| +10 min. instance-control's start runs its own census under the same 20 s |
| +window. |
| + |
| ## Tests named in the rows |
| - (a): `TestSystemGateWaitIsKeyedOnClaimsMode`, |
diff --git a/internal/host/plan14_generation_kernel_linux_test.go b/internal/host/plan14_generation_kernel_linux_test.go| index 4e17551a4..b4f0e192b 100644 |
| --- a/internal/host/plan14_generation_kernel_linux_test.go |
| +++ b/internal/host/plan14_generation_kernel_linux_test.go |
| @@ -291,7 +291,7 @@ func TestPlan14GenerationServiceReferences(t *testing.T) { |
| func TestPlan14GenerationCensusRetriesWholeObservation(t *testing.T) { |
| for _, refusal := range []error{os.ErrNotExist, os.ErrPermission} { |
| calls := 0 |
| - boot, digest, err := generationCensusRetry(t.Context(), func() (string, string, error) { |
| + boot, digest, err := generationCensusRetry(t.Context(), func(context.Context) (string, string, error) { |
| calls++ |
| if calls == 1 { |
| return "", "", fmt.Errorf("task exited: %w", refusal) |
| @@ -307,7 +307,7 @@ func TestPlan14GenerationCensusRetriesWholeObservation(t *testing.T) { |
| } |
| } |
| calls := 0 |
| - _, _, err := generationCensusRetry(t.Context(), func() (string, string, error) { calls++; return "", "", os.ErrNotExist }) |
| + _, _, err := generationCensusRetry(t.Context(), func(context.Context) (string, string, error) { calls++; return "", "", os.ErrNotExist }) |
| if err == nil || calls != 3 { |
| t.Fatal("unbounded or incomplete scan accepted", calls, err) |
| } |
diff --git a/internal/host/rootgeneration_census_linux.go b/internal/host/rootgeneration_census_linux.go| index 361176102..200ae15bd 100644 |
| --- a/internal/host/rootgeneration_census_linux.go |
| +++ b/internal/host/rootgeneration_census_linux.go |
| @@ -97,19 +97,24 @@ func productionGenerationKernelCensus(ctx context.Context) (boot, digest string, |
| return "", "", err |
| } |
| records, _ := ctx.Value(generationCensusReservationsKey{}).([]release.GenerationReservation) |
| - return generationCensusRetry(ctx, func() (string, string, error) { |
| + return generationCensusRetry(ctx, func(ctx context.Context) (string, string, error) { |
| service, recheck := generationServiceReader(ctx) |
| return (generationKernel{proc: "/proc", cgroups: "/sys/fs/cgroup", contained: records, service: service, recheck: recheck}).census(ctx, tree) |
| }) |
| } |
| -// generationCensusWindow bounds how long the census waits out tasks that |
| -// are still settling: a task that exited or was replaced mid-pass, or one |
| -// the service lookup refused (a unit still activating, the guard's prelude, |
| -// a login's or sshd's short-lived child). A task still refused after it |
| -// refuses the census. |
| +// generationCensusWindow bounds a whole census, its passes and the waits |
| +// between them: a task that exited or was replaced mid-pass, or one the |
| +// service lookup refused (a unit still activating, the guard's prelude, a |
| +// login's or sshd's short-lived child), is waited out inside it. A task |
| +// still refused, or a pass still running, when it ends refuses the census. |
| const generationCensusWindow = 20 * time.Second |
| +// errGenerationCensusLate is a census that did not finish inside its |
| +// window: the last pass ran past it, successful or not, or no pass could |
| +// start before it ended. |
| +var errGenerationCensusLate = fmt.Errorf("generation census ran past its window: %w", os.ErrPermission) |
| + |
| // generationTaskRefusal is a census refusal of one task, named by its /proc |
| // path. Its text is the path and the cause, as before it had a type. |
| type generationTaskRefusal struct { |
| @@ -135,7 +140,7 @@ func (r *generationTaskRefusal) transient() bool { |
| return errors.Is(r.err, os.ErrNotExist) || errors.Is(r.err, unix.ESRCH) || errors.Is(r.err, errGenerationTaskReplaced) || errors.As(r.err, &service) |
| } |
| -func generationCensusRetry(ctx context.Context, inspect func() (string, string, error)) (string, string, error) { |
| +func generationCensusRetry(ctx context.Context, inspect func(context.Context) (string, string, error)) (string, string, error) { |
| return generationCensusRetryFor(ctx, inspect, generationCensusWindow, time.Now, generationSleep) |
| } |
| @@ -150,25 +155,47 @@ func generationSleep(ctx context.Context, d time.Duration) error { |
| } |
| } |
| -// generationCensusRetryFor runs whole census passes. A transient task |
| -// refusal is retried with backoff (250 ms, doubling to 4 s) while the next |
| -// wait still ends inside window; then the last refusal is returned as it |
| -// is. Any other missing-file or vanished-process error is retried at once, |
| -// up to three passes, as before. Everything else returns at once. |
| -func generationCensusRetryFor(ctx context.Context, inspect func() (string, string, error), window time.Duration, now func() time.Time, sleep func(context.Context, time.Duration) error) (string, string, error) { |
| - begin, delay, quick := now(), 250*time.Millisecond, 0 |
| +// generationCensusRetryFor runs whole census passes under one deadline, |
| +// window from its start. Each pass gets a context that ends at the |
| +// deadline, which its blocking reads (systemctl show, the image hash) obey, |
| +// and a pass that ends past the deadline refuses even if it succeeded, so |
| +// nothing it saw is accepted late. A transient task refusal is retried with |
| +// backoff (250 ms, doubling to 4 s) while the wait ends before the |
| +// deadline; then the last refusal is returned as it is. Any other |
| +// missing-file or vanished-process error is retried at once, up to three |
| +// passes, as before. Everything else returns at once. now is the clock the |
| +// deadline is read on. |
| +func generationCensusRetryFor(ctx context.Context, inspect func(context.Context) (string, string, error), window time.Duration, now func() time.Time, sleep func(context.Context, time.Duration) error) (string, string, error) { |
| + deadline := now().Add(window) |
| + bounded, cancel := context.WithTimeout(ctx, window) |
| + defer cancel() |
| + late := func(last error) error { |
| + if last == nil { |
| + return errGenerationCensusLate |
| + } |
| + return fmt.Errorf("%w (last pass: %v)", errGenerationCensusLate, last) |
| + } |
| + delay, quick := 250*time.Millisecond, 0 |
| + var last error |
| for { |
| if err := ctx.Err(); err != nil { |
| return "", "", err |
| } |
| - boot, digest, err := inspect() |
| + if !now().Before(deadline) || bounded.Err() != nil { |
| + return "", "", late(last) |
| + } |
| + boot, digest, err := inspect(bounded) |
| + if ctx.Err() == nil && (!now().Before(deadline) || bounded.Err() != nil) { |
| + return "", "", late(err) |
| + } |
| if err == nil { |
| return boot, digest, nil |
| } |
| + last = err |
| var task *generationTaskRefusal |
| switch { |
| case errors.As(err, &task) && task.transient(): |
| - if now().Sub(begin)+delay > window || sleep(ctx, delay) != nil { |
| + if !now().Add(delay).Before(deadline) || sleep(bounded, delay) != nil { |
| return boot, digest, err |
| } |
| delay = min(2*delay, 4*time.Second) |
diff --git a/internal/host/rootgeneration_census_recover_linux_test.go b/internal/host/rootgeneration_census_recover_linux_test.go| index ffd660207..d8a3e75cd 100644 |
| --- a/internal/host/rootgeneration_census_recover_linux_test.go |
| +++ b/internal/host/rootgeneration_census_recover_linux_test.go |
| @@ -320,13 +320,13 @@ func TestRecoverCensusRetriesATransientRefusalWithinTheWindow(t *testing.T) { |
| // the fourth pass. |
| f := deletedExe(t) |
| passes := 0 |
| - inspect := func() (string, string, error) { |
| + inspect := func(ctx context.Context) (string, string, error) { |
| passes++ |
| f.mainPID = "0" |
| if passes == 4 { |
| f.mainPID = "41" |
| } |
| - err := f.kernelCensus(t.Context()) |
| + err := f.kernelCensus(ctx) |
| return "boot", "digest", err |
| } |
| if _, _, err := generationCensusRetryFor(t.Context(), inspect, 20*time.Second, now, sleep); err != nil { |
| @@ -339,10 +339,10 @@ func TestRecoverCensusRetriesATransientRefusalWithinTheWindow(t *testing.T) { |
| // The same refusal, persistent: refused once the next wait would end |
| // past the window, with the last pass's refusal. |
| clock, slept, passes = time.Unix(0, 0), nil, 0 |
| - persistent := func() (string, string, error) { |
| + persistent := func(ctx context.Context) (string, string, error) { |
| passes++ |
| f.mainPID = "0" |
| - err := f.kernelCensus(t.Context()) |
| + err := f.kernelCensus(ctx) |
| return "", "", err |
| } |
| _, _, err := generationCensusRetryFor(t.Context(), persistent, 20*time.Second, now, sleep) |
| @@ -374,7 +374,7 @@ func TestRecoverCensusRetriesOnlyTaskLevelTransients(t *testing.T) { |
| } { |
| t.Run(tc.name, func(t *testing.T) { |
| passes := 0 |
| - _, _, err := generationCensusRetryFor(t.Context(), func() (string, string, error) { |
| + _, _, err := generationCensusRetryFor(t.Context(), func(context.Context) (string, string, error) { |
| passes++ |
| if passes == 2 { |
| return "boot", "digest", nil |
| @@ -392,7 +392,7 @@ func TestRecoverCensusRetriesOnlyTaskLevelTransients(t *testing.T) { |
| // A cancelled wait refuses with the refusal it was waiting out. |
| ctx, cancel := context.WithCancel(t.Context()) |
| refusal := &generationTaskRefusal{task: "/proc/9/task/9", err: os.ErrNotExist} |
| - _, _, err := generationCensusRetryFor(ctx, func() (string, string, error) { return "", "", refusal }, 20*time.Second, time.Now, func(context.Context, time.Duration) error { cancel(); return context.Canceled }) |
| + _, _, err := generationCensusRetryFor(ctx, func(context.Context) (string, string, error) { return "", "", refusal }, 20*time.Second, time.Now, func(context.Context, time.Duration) error { cancel(); return context.Canceled }) |
| if !errors.Is(err, refusal) { |
| t.Fatalf("cancelled wait = %v", err) |
| } |
| @@ -525,3 +525,112 @@ func TestRecoverCensusNeedsABindingForATrustedTask(t *testing.T) { |
| t.Fatalf("census without a recheck = %v", err) |
| } |
| } |
| + |
| +func TestRecoverCensusDeadlineCountsInspectionTime(t *testing.T) { |
| + clock := time.Unix(0, 0) |
| + now := func() time.Time { return clock } |
| + sleep := func(_ context.Context, d time.Duration) error { clock = clock.Add(d); return nil } |
| + transient := &generationTaskRefusal{task: "/proc/9/task/9", err: os.ErrNotExist} |
| + |
| + // A slow pass that succeeds past the deadline refuses. |
| + _, _, err := generationCensusRetryFor(t.Context(), func(context.Context) (string, string, error) { |
| + clock = clock.Add(25 * time.Second) |
| + return "boot", "digest", nil |
| + }, 20*time.Second, now, sleep) |
| + if !errors.Is(err, errGenerationCensusLate) { |
| + t.Fatalf("a pass that ended past the window = %v, want a late refusal", err) |
| + } |
| + |
| + // Slow refused passes: inspection time and waits share the deadline, |
| + // no pass starts after it, and the census refuses inside it. |
| + clock = time.Unix(0, 0) |
| + var starts []time.Duration |
| + _, _, err = generationCensusRetryFor(t.Context(), func(context.Context) (string, string, error) { |
| + starts = append(starts, clock.Sub(time.Unix(0, 0))) |
| + clock = clock.Add(6 * time.Second) |
| + return "", "", transient |
| + }, 20*time.Second, now, sleep) |
| + if err == nil { |
| + t.Fatal("persistent refusal accepted") |
| + } |
| + for _, s := range starts { |
| + if s >= 20*time.Second { |
| + t.Fatalf("a pass started at %s, past the window (%v)", s, starts) |
| + } |
| + } |
| + if end := clock.Sub(time.Unix(0, 0)); end > 26*time.Second { |
| + t.Fatalf("census ran to %s with 6 s passes; it must stop at the first pass that ends past 20 s", end) |
| + } |
| + if len(starts) < 2 { |
| + t.Fatalf("passes started at %v: the slow passes were not retried inside the window", starts) |
| + } |
| + |
| + // A wait that overruns (a timer firing late) never starts a pass past |
| + // the deadline. |
| + clock, starts = time.Unix(0, 0), nil |
| + oversleep := func(_ context.Context, d time.Duration) error { clock = clock.Add(d + 3*time.Second); return nil } |
| + _, _, err = generationCensusRetryFor(t.Context(), func(context.Context) (string, string, error) { |
| + starts = append(starts, clock.Sub(time.Unix(0, 0))) |
| + return "", "", transient |
| + }, 20*time.Second, now, oversleep) |
| + if !errors.Is(err, errGenerationCensusLate) { |
| + t.Fatalf("overrun wait = %v, want a late refusal", err) |
| + } |
| + for _, s := range starts { |
| + if s >= 20*time.Second { |
| + t.Fatalf("a pass started at %s, past the window (%v)", s, starts) |
| + } |
| + } |
| +} |
| + |
| +func TestRecoverCensusDeadlineReachesTheInspection(t *testing.T) { |
| + // On the real clock: the pass's context ends at the deadline, and a pass |
| + // that only returns when it does is refused, promptly. |
| + start := time.Now() |
| + var deadline time.Time |
| + var has bool |
| + _, _, err := generationCensusRetryFor(t.Context(), func(ctx context.Context) (string, string, error) { |
| + deadline, has = ctx.Deadline() |
| + select { |
| + case <-ctx.Done(): |
| + case <-time.After(2 * time.Second): |
| + } |
| + return "boot", "digest", nil |
| + }, 50*time.Millisecond, time.Now, generationSleep) |
| + if !errors.Is(err, errGenerationCensusLate) { |
| + t.Fatalf("a pass blocked to the deadline = %v, want a late refusal", err) |
| + } |
| + if !has || deadline.Sub(start) > 50*time.Millisecond+10*time.Millisecond { |
| + t.Fatalf("pass context deadline = %v (set %t), want the census's", deadline.Sub(start), has) |
| + } |
| + if took := time.Since(start); took > 2*time.Second { |
| + t.Fatalf("census took %s past a 50 ms window", took) |
| + } |
| + // The lookup's own blocking reads get that context: systemctl show and |
| + // the image hash. |
| + f := deletedExe(t) |
| + var showDeadline bool |
| + show := f.env.show |
| + f.env.show = func(ctx context.Context, unit string) (map[string]string, error) { |
| + _, showDeadline = ctx.Deadline() |
| + return show(ctx, unit) |
| + } |
| + if _, _, err := generationCensusRetryFor(t.Context(), func(ctx context.Context) (string, string, error) { |
| + return "boot", "digest", f.kernelCensus(ctx) |
| + }, 20*time.Second, time.Now, generationSleep); err != nil { |
| + t.Fatal(err) |
| + } |
| + if !showDeadline { |
| + t.Fatal("systemctl show ran without the census deadline") |
| + } |
| + // The deadline passes after the lookup has started on the task: the |
| + // image hash stops. |
| + ctx, cancel := context.WithCancel(t.Context()) |
| + f.env.show = func(ctx context.Context, unit string) (map[string]string, error) { |
| + cancel() |
| + return show(ctx, unit) |
| + } |
| + if err := f.kernelCensus(ctx); !errors.Is(err, context.Canceled) || !strings.Contains(err.Error(), "deleted exe check refused") { |
| + t.Fatalf("image hash under an ended context = %v", err) |
| + } |
| +} |
diff --git a/internal/host/rootgeneration_services_linux.go b/internal/host/rootgeneration_services_linux.go| index 524a8eead..fce85e3ed 100644 |
| --- a/internal/host/rootgeneration_services_linux.go |
| +++ b/internal/host/rootgeneration_services_linux.go |
| @@ -256,7 +256,7 @@ func (env generationServiceEnv) reader(ctx context.Context) (func(string, []byte |
| if s.selected != (rootstate.FileIdentity{}) { |
| want = s.selectedPath |
| } |
| - image, err := env.deletedProgram(path, program, want, prove) |
| + image, err := env.deletedProgram(ctx, path, program, want, prove) |
| if err != nil { |
| return false, refuse("deleted exe", err) |
| } |
| @@ -354,7 +354,7 @@ func (env generationServiceEnv) recheck(bound map[string]*generationExeBinding, |
| // |
| // On success it returns the image's descriptor, open: the lookup keeps it |
| // to bind the task to that image for the rest of its inspection (recheck). |
| -func (env generationServiceEnv) deletedProgram(task, program, want string, prove func() ([]string, error)) (*os.File, error) { |
| +func (env generationServiceEnv) deletedProgram(ctx context.Context, task, program, want string, prove func() ([]string, error)) (*os.File, error) { |
| if program != want || want != filepath.Join(env.opt, "carlos") { |
| return nil, fmt.Errorf("%s is not this unit's router binary: %w", program, os.ErrPermission) |
| } |
| @@ -376,7 +376,7 @@ func (env generationServiceEnv) deletedProgram(task, program, want string, prove |
| return nil, fmt.Errorf("image is not an unlinked root-owned file: %w", os.ErrPermission) |
| } |
| sum := sha256.New() |
| - if n, err := io.Copy(sum, io.LimitReader(f, 512<<20+1)); err != nil { |
| + if n, err := io.Copy(sum, generationContextReader{ctx: ctx, r: io.LimitReader(f, 512<<20+1)}); err != nil { |
| return nil, err |
| } else if n > 512<<20 { |
| return nil, os.ErrPermission |
| @@ -392,6 +392,20 @@ func (env generationServiceEnv) deletedProgram(task, program, want string, prove |
| return f, nil |
| } |
| +// generationContextReader stops a long read, the image hash, when the |
| +// census's deadline passes. |
| +type generationContextReader struct { |
| + ctx context.Context |
| + r io.Reader |
| +} |
| + |
| +func (c generationContextReader) Read(p []byte) (int, error) { |
| + if err := c.ctx.Err(); err != nil { |
| + return 0, err |
| + } |
| + return c.r.Read(p) |
| +} |
| + |
| func generationGuardEdgeStart(unit, start string) bool { |
| if unit != "carlos-edge.service" || strings.Count(start, "{") != 1 || strings.Count(start, "}") != 1 { |
| return false |