carlos / platform Public

instancectl: a revoke's owed restart survives uncertain writes, unnamed slices retry it, and only real readings prove it (#328)

Review fix round 1 (Astra), findings 1-3:

1. A revoke that empties a slice records the restart it may owe BEFORE the
   reset is written, so a write that answers an error after it applied,
   or a read after it that fails, no longer loses it: the next Assert,
   finding the slice empty, restarts. An allow landing again discharges
   it.
2. The sweep's reset of a slice it cannot name records the same before
   writing, and a later sweep that finds that slice empty retries a
   restart still due (before, it returned at the empty read forever).
3. A try-restart that answers an error counts as having ended the old
   invocation only on readings: both InvocationIDs read, non-empty and
   different, or the unit read inactive or failed. A failed read proves
   nothing; the restart stays due and the report is failing.

Tests (each failed before its fix): TestEgressRevokeRestartSurvivesAnUncertainWrite
(read fails after the reset; reset applies but errors; a grant discharges),
TestEgressSweepRetriesAnUnnamedSlicesRestart (two sweeps, then none),
TestEgressRestartNotProvenByAFailedRead.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 06bb1fd41cc94e2408d5113dc3a3c80d77a724ee parent ded0af2
2 files changed, +172 −7
  • internal/instancectl/egress.go +31 −7
  • internal/instancectl/egress_restart_linux_test.go +141 −0
diff --git a/internal/instancectl/egress.go b/internal/instancectl/egress.go
index 43461e1db..6db9db4c2 100644
--- a/internal/instancectl/egress.go
+++ b/internal/instancectl/egress.go
@@ -170,6 +170,10 @@ func (e *EgressAsserter) Assert(ctx context.Context, host string) error {
// slice was seen to hold.
rep.Allowed = held.addrs()
if held.equal(want) {
+ if len(want) > 0 {
+ // An allow in force again: no revoke is left to close.
+ e.setRestartDue(slice, false)
+ }
if len(want) == 0 && e.isRestartDue(slice) {
if err := e.closeByRestart(ctx, slice, &rep); err != nil {
rep.State, rep.Detail = EgressFailing, err.Error()
@@ -203,6 +207,13 @@ func (e *EgressAsserter) Assert(ctx context.Context, host string) error {
e.logf("egress: %s: read back: %v; writing the allow", slice, readErr)
}
+ if emptied {
+ // The restart this revoke may owe is recorded before the reset is
+ // written: an uncertain outcome (the write answers an error after
+ // it applied, or the read after it fails) must not lose it, or the
+ // next Assert would find the slice empty and stop there.
+ e.setRestartDue(slice, true)
+ }
args := []string{"set-property", "--runtime", slice, "IPAddressAllow="}
if len(want) > 0 {
list := make([]string, len(want))
@@ -230,8 +241,9 @@ func (e *EgressAsserter) Assert(ctx context.Context, host string) error {
return fail(fmt.Sprintf("after the write the slice holds %s, not %s", held.text(), prefixesText(want)))
}
e.logf("egress: %s: allow is now %s", slice, prefixesText(want))
- if emptied {
- e.setRestartDue(slice, true)
+ if len(want) > 0 {
+ e.setRestartDue(slice, false)
+ } else if emptied || e.isRestartDue(slice) {
if err := e.closeByRestart(ctx, slice, &rep); err != nil {
return fail(err.Error())
}
@@ -290,14 +302,18 @@ func (e *EgressAsserter) closeByRestart(ctx context.Context, slice string, rep *
failed = append(failed, fmt.Sprintf("%q: not a unit name", unit))
continue
}
- inv, _ := e.Systemctl(ctx, "show", "-p", "InvocationID", "--value", unit)
+ inv, invErr := e.Systemctl(ctx, "show", "-p", "InvocationID", "--value", unit)
if out, err := e.Systemctl(ctx, "try-restart", unit); err != nil {
// A restart whose start fails (a claim unit's start without a
// reservation, say) still ended the old invocation, and that
- // is what closes the address.
- after, _ := e.Systemctl(ctx, "show", "-p", "InvocationID", "--value", unit)
- now, _ := e.Systemctl(ctx, "show", "-p", "ActiveState", "--value", unit)
- gone := strings.TrimSpace(after) != strings.TrimSpace(inv) || strings.TrimSpace(now) == "inactive" || strings.TrimSpace(now) == "failed"
+ // is what closes the address. Only readings prove it: both
+ // InvocationIDs read, non-empty and different, or the unit
+ // read inactive or failed. A failed read proves nothing.
+ after, afterErr := e.Systemctl(ctx, "show", "-p", "InvocationID", "--value", unit)
+ now, nowErr := e.Systemctl(ctx, "show", "-p", "ActiveState", "--value", unit)
+ before, later := strings.TrimSpace(inv), strings.TrimSpace(after)
+ gone := (invErr == nil && afterErr == nil && before != "" && later != before) ||
+ (nowErr == nil && (strings.TrimSpace(now) == "inactive" || strings.TrimSpace(now) == "failed"))
if !gone {
failed = append(failed, fmt.Sprintf("%s: try-restart: %s: %v", unit, strings.TrimSpace(out), err))
continue
@@ -730,8 +746,16 @@ func (e *EgressAsserter) closeUnnamed(ctx context.Context, slice string) (string
return "", false
}
if held.equal(nil) {
+ // A restart an earlier sweep's reset still owes is retried here,
+ // where the slice already reads empty.
+ if e.isRestartDue(slice) {
+ if err := e.closeByRestart(ctx, slice, &EgressReport{}); err != nil {
+ e.logf("egress: %s: no host for this slice; %v", slice, err)
+ }
+ }
return "", false
}
+ e.setRestartDue(slice, true) // before the reset, as Assert records it
if out, err := e.Systemctl(ctx, "set-property", "--runtime", slice, "IPAddressAllow="); err != nil {
e.logf("egress: %s: no host for this slice; closing its allow: set-property: %s: %v", slice, strings.TrimSpace(out), err)
return "", false
diff --git a/internal/instancectl/egress_restart_linux_test.go b/internal/instancectl/egress_restart_linux_test.go
index 018bfe8e5..3207bca06 100644
--- a/internal/instancectl/egress_restart_linux_test.go
+++ b/internal/instancectl/egress_restart_linux_test.go
@@ -10,6 +10,7 @@ package instancectl
import (
"context"
+ "errors"
"os"
"path/filepath"
"slices"
@@ -185,3 +186,143 @@ func TestEgressRevokeToEmptyRestartsWithoutAParentAllow(t *testing.T) {
}
})
}
+
+// restartBox is a slice holding 10.0.5.20/32 with its run unit running,
+// no parent allow, and an asserter whose systemctl the test can wrap.
+func restartBox(t *testing.T, host string) (*egressCtl, *EgressAsserter, string) {
+ t.Helper()
+ slice := SliceName(host)
+ root := t.TempDir()
+ cgroupSlice(t, root, slice, true)
+ run := UnitName(RoleRun, host)
+ cgroupService(t, root, slice, run, true)
+ ctl := newEgressCtl()
+ ctl.setHeld(slice, "10.0.5.20/32")
+ ctl.active[run] = "active"
+ e := newEgressAsserter(ctl, staticSource(EgressPolicy{}, nil))
+ e.CgroupRoot = root
+ return ctl, e, run
+}
+
+// Review finding 1: the restart a revoke to empty owes is recorded before
+// the reset is written, so an uncertain write (a read after it that
+// fails, or a write that applied but answered an error) still leaves it
+// due, and the next Assert, finding the slice empty, restarts.
+func TestEgressRevokeRestartSurvivesAnUncertainWrite(t *testing.T) {
+ slice := SliceName(egressHost)
+ for _, tc := range []struct {
+ name string
+ wrap func(next func(context.Context, ...string) (string, error)) func(context.Context, ...string) (string, error)
+ }{
+ {"the read after the reset fails", func(next func(context.Context, ...string) (string, error)) func(context.Context, ...string) (string, error) {
+ wrote, failed := false, false
+ return func(ctx context.Context, args ...string) (string, error) {
+ if len(args) > 0 && args[0] == "set-property" {
+ wrote = true
+ } else if wrote && !failed && len(args) == 5 && args[2] == "IPAddressAllow" && args[4] == slice {
+ failed = true
+ return "Failed to connect to bus", errors.New("exit status 1")
+ }
+ return next(ctx, args...)
+ }
+ }},
+ {"the reset applies but answers an error", func(next func(context.Context, ...string) (string, error)) func(context.Context, ...string) (string, error) {
+ done := false
+ return func(ctx context.Context, args ...string) (string, error) {
+ out, err := next(ctx, args...)
+ if len(args) > 0 && args[0] == "set-property" && !done {
+ done = true
+ return "Connection timed out", errors.New("exit status 1")
+ }
+ return out, err
+ }
+ }},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ ctl, e, run := restartBox(t, egressHost)
+ e.Systemctl = tc.wrap(e.Systemctl)
+ if err := e.Assert(context.Background(), egressHost); err == nil {
+ t.Fatal("first Assert succeeded through an uncertain write")
+ }
+ if got := ctl.held[slice]; got != "" {
+ t.Fatalf("the reset did not apply in the fixture: %q", got)
+ }
+ ctl.takeCalls()
+ rep := mustAssert(t, e, egressHost) // the slice now reads back empty
+ if !slices.Contains(ctl.takeCalls(), "try-restart "+run) || rep.Detail != egressRestarted {
+ t.Fatalf("the next Assert did not restart the slice's running unit: %+v", rep)
+ }
+ })
+ }
+ t.Run("a grant landing again discharges the restart", func(t *testing.T) {
+ ctl, e, run := restartBox(t, egressHost)
+ ctl.restartFail[run] = true
+ if err := e.Assert(context.Background(), egressHost); err == nil {
+ t.Fatal("Assert succeeded though the restart did not happen")
+ }
+ ctl.restartFail[run] = false
+ e.Source = staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}}, nil)
+ mustAssert(t, e, egressHost)
+ e.Source = staticSource(EgressPolicy{}, nil)
+ ctl.setHeld(slice, "")
+ ctl.takeCalls()
+ mustAssert(t, e, egressHost)
+ wantCalls(t, ctl.takeCalls(), showCall(egressHost))
+ })
+}
+
+// Review finding 2: the sweep's reset of a slice it cannot name keeps a
+// failed restart due, and a later sweep that finds the slice empty
+// discharges it.
+func TestEgressSweepRetriesAnUnnamedSlicesRestart(t *testing.T) {
+ slice := SliceName(longEgressHost) // a hashed id: no name for it here
+ root := t.TempDir()
+ cgroupSlice(t, root, slice, true)
+ unit := UnitName(RoleRun, longEgressHost)
+ cgroupService(t, root, slice, unit, true)
+ ctl := newEgressCtl()
+ ctl.setHeld(slice, "10.0.5.20/32")
+ ctl.active[unit] = "active"
+ ctl.restartFail[unit] = true
+ e := newEgressAsserter(ctl, staticSource(EgressPolicy{}, nil))
+ e.CgroupRoot = root
+ e.Sweep(context.Background())
+ if got := ctl.held[slice]; got != "" {
+ t.Fatalf("the first sweep did not reset the unnamed slice: %q", got)
+ }
+ ctl.restartFail[unit] = false
+ ctl.takeCalls()
+ e.Sweep(context.Background())
+ if !slices.Contains(ctl.takeCalls(), "try-restart "+unit) {
+ t.Fatal("the second sweep did not retry the unnamed slice's restart")
+ }
+ e.Sweep(context.Background())
+ if slices.Contains(ctl.takeCalls(), "try-restart "+unit) {
+ t.Fatal("a discharged restart ran again")
+ }
+}
+
+// Review finding 3: a restart counts as done only on readings that prove
+// it: both InvocationIDs read and different, or the unit read inactive or
+// failed. A failed read proves nothing.
+func TestEgressRestartNotProvenByAFailedRead(t *testing.T) {
+ ctl, e, run := restartBox(t, egressHost)
+ ctl.restartFail[run] = true // try-restart fails; the unit stays active on its invocation
+ next := e.Systemctl
+ reads := 0
+ e.Systemctl = func(ctx context.Context, args ...string) (string, error) {
+ if len(args) == 5 && args[2] == "InvocationID" && args[4] == run {
+ reads++
+ if reads > 1 { // the read after the try-restart
+ return "", errors.New("exit status 1")
+ }
+ }
+ return next(ctx, args...)
+ }
+ if err := e.Assert(context.Background(), egressHost); err == nil {
+ t.Fatal("a failed InvocationID read counted as proof the old invocation is gone")
+ }
+ if rep := e.Reports()[egressHost]; rep.State != EgressFailing || !e.isRestartDue(SliceName(egressHost)) {
+ t.Fatalf("report = %+v, due %v: want failing and the restart kept due", rep, e.isRestartDue(SliceName(egressHost)))
+ }
+}