system recover: defer census refusals at every no-journal settlement boundary
Astra (fix round 1, finding 1): the deferral wrapped only the pending-controller census. The census that runs again inside EndParticipation (EndUpdateWith -> idle), the already-completed branch and the unfinished-transaction branch still failed recover, and the unfinished branch removed root's marker before its census ran. - productionGenerationKernelCensus types every refusal as a generationCensusRefusal. - settleWithoutJournal now takes the result of the whole no-journal settlement (controllers, completion or withdrawal, end of participation) in all three branches, and in the unarmed recovery. It defers only when the result is a census refusal and nothing else (onlyCensusRefusal: a join with a failed durable write does not defer), on a census box. - An image that does not verify, malformed or foreign state, a live or unreadable controller and a failed durable write keep their own errors and fail recover, as on main. The round-0 change that deferred any controller failure is withdrawn and main's three tests restored. - The unfinished branch removes root's marker inside the settlement, after the census, so a refusal leaves the whole transaction.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8 files changed,
+364
−64
internal/host/guardparticipation_test.go+1 −11internal/host/guardtransaction.go+32 −25internal/host/guardtransaction_test.go+4 −17internal/host/recover_settlement_test.go+257 −5internal/host/rootgeneration_census_linux.go+10 −1internal/host/rootgeneration_census_recover_linux_test.go+14 −0internal/host/rootgeneration_refusal.go+42 −0internal/host/systemparticipation.go+4 −5
diff --git a/internal/host/guardparticipation_test.go b/internal/host/guardparticipation_test.go| index 59148979c..13746e1b9 100644 |
| --- a/internal/host/guardparticipation_test.go |
| +++ b/internal/host/guardparticipation_test.go |
| @@ -92,19 +92,9 @@ func TestSystemGuardRegionHAKeepsFailedCompletionEvidence(t *testing.T) { |
| } |
| } |
| a.settleUpdateBegun(t.Context()) |
| - err := a.Run(t.Context(), true) |
| - if failure == "controller" { |
| - // No journal is open: recover defers the settlement and |
| - // exits 0 so the edge can start, keeping the evidence. |
| - if err != nil { |
| - t.Fatalf("recover with no journal open = %v, want a deferral that exits 0", err) |
| - } |
| - } else if err == nil { |
| + if err := a.Run(t.Context(), true); err == nil { |
| t.Fatal("unsafe recovery succeeded") |
| } |
| - if _, err := os.Stat(filepath.Join(a.floor.Dir, "completed.json")); failure != "completion" && !os.IsNotExist(err) { |
| - t.Fatalf("completion written: %v", err) |
| - } |
| for _, path := range []string{updateBegunPath(a.optDir), evidence} { |
| if _, err := os.Stat(path); err != nil { |
| t.Fatalf("recovery discarded %s: %v", path, err) |
diff --git a/internal/host/guardtransaction.go b/internal/host/guardtransaction.go| index 0af21f519..4baa14909 100644 |
| --- a/internal/host/guardtransaction.go |
| +++ b/internal/host/guardtransaction.go |
| @@ -138,10 +138,14 @@ func (a *systemApplier) settleGuard(ctx context.Context, tx guardTransaction, be |
| return err |
| } |
| if !finished { |
| - if err := hostfloor.RemoveDurable(filepath.Join(a.floor.Dir, "update-begun.json")); err != nil { |
| - return err |
| - } |
| - return a.floor.EndParticipation(ctx, tx.ID, func() error { return hostfloor.RemoveDurable(filepath.Join(a.floor.Dir, "activation.json")) }) |
| + // The marker goes inside the settlement, after root's census, so a |
| + // census that refuses leaves it with the rest of the transaction. |
| + return a.floor.EndParticipation(ctx, tx.ID, func() error { |
| + if err := hostfloor.RemoveDurable(filepath.Join(a.floor.Dir, "update-begun.json")); err != nil { |
| + return err |
| + } |
| + return hostfloor.RemoveDurable(filepath.Join(a.floor.Dir, "activation.json")) |
| + }) |
| } |
| if beforeCompletion != nil { |
| if err := beforeCompletion(); err != nil { |
| @@ -249,12 +253,12 @@ func (a *systemApplier) checkGuardRecovery(ctx context.Context) error { |
| } |
| completed, readErr := os.ReadFile(filepath.Join(a.floor.Dir, "completed.json")) |
| if readErr == nil && string(completed) == tx.ID { |
| - return a.floor.EndParticipation(ctx, tx.ID, nil) |
| + return a.settleWithoutJournal(ctx, a.floor.EndParticipation(ctx, tx.ID, nil)) |
| } |
| if readErr != nil && !errors.Is(readErr, os.ErrNotExist) { |
| return readErr |
| } |
| - return a.settleGuard(ctx, tx, func() error { return a.settleWithoutJournal(ctx) }) |
| + return a.settleWithoutJournal(ctx, a.settleGuard(ctx, tx, func() error { return a.controllersRecovered(ctx) })) |
| } |
| if err != nil { |
| return err |
| @@ -270,7 +274,7 @@ func (a *systemApplier) checkGuardRecovery(ctx context.Context) error { |
| return hostfloor.ErrFloor |
| } |
| if j == nil { |
| - return a.settleGuard(ctx, tx, func() error { return a.settleWithoutJournal(ctx) }) |
| + return a.settleWithoutJournal(ctx, a.settleGuard(ctx, tx, func() error { return a.controllersRecovered(ctx) })) |
| } |
| if err := a.controllersRecovered(ctx); err != nil { |
| return err |
| @@ -281,28 +285,31 @@ func (a *systemApplier) checkGuardRecovery(ctx context.Context) error { |
| return a.guardImage(systemPrevPath(a.optDir)) |
| } |
| -// errSettlementDeferred marks a settlement that root could not make with |
| -// no journal open. No apply is half done and the files are whole, so |
| -// there is nothing for recovery to restore and no reason to keep the edge |
| -// down (its drop-in Requires= the recover unit). The participation |
| -// transaction stays open, which already refuses every new claim |
| -// acquisition, and the next apply, or recover on the next edge start, |
| -// settles it. Run under --recover logs it and exits 0; the apply returns |
| -// it as before. beginExecutableChange treats the same refusal as a |
| -// deferral (systemparticipation.go). |
| +// errSettlementDeferred marks a settlement that root's generation census |
| +// refused with no journal open. No apply is half done and the files are |
| +// whole, so there is nothing for recovery to restore and no reason to keep |
| +// the edge down (its drop-in Requires= the recover unit). The transaction |
| +// evidence stays as it was: the reservation, root's marker and update |
| +// record, so every new claim acquisition stays refused, and the next apply, |
| +// or recover on the next edge start, settles it. Run under --recover logs |
| +// it and exits 0; the apply returns it as before. beginExecutableChange |
| +// treats a refused census as a deferral too (systemparticipation.go). |
| var errSettlementDeferred = errors.New("participation settlement deferred") |
| -// settleWithoutJournal is controllersRecovered for the branches with no |
| -// journal open. On a box with root participation (a census), a refusal is |
| -// wrapped in errSettlementDeferred. On a box without one it is returned |
| -// unchanged: those boxes see exactly main's recovery (ruling 6). |
| -func (a *systemApplier) settleWithoutJournal(ctx context.Context) error { |
| - err := a.controllersRecovered(ctx) |
| - if err == nil { |
| - return nil |
| +// settleWithoutJournal is the result of one no-journal settlement: the |
| +// pending-controller recovery, the completion or withdrawal, and the end of |
| +// root's update record, whose census runs again inside EndParticipation. A |
| +// result that is only a census refusal, on a box with root participation, |
| +// is wrapped in errSettlementDeferred. Anything else is returned unchanged: |
| +// an image that does not verify, malformed state, a live or unreadable |
| +// controller, a durable write that failed, and every refusal on a box |
| +// without a census, which sees exactly main's recovery (ruling 6). |
| +func (a *systemApplier) settleWithoutJournal(ctx context.Context, err error) error { |
| + if err == nil || !onlyCensusRefusal(err) || ctx.Err() != nil { |
| + return err |
| } |
| required, checkErr := a.participationDisk().ParticipationRequired(ctx) |
| - if checkErr != nil || !required || ctx.Err() != nil { |
| + if checkErr != nil || !required { |
| return err |
| } |
| return fmt.Errorf("%w: no journal is open, so the files are whole; the participation transaction stays open and claim acquisitions stay refused: %w", errSettlementDeferred, err) |
diff --git a/internal/host/guardtransaction_test.go b/internal/host/guardtransaction_test.go| index f0ffc84ec..310d0e3aa 100644 |
| --- a/internal/host/guardtransaction_test.go |
| +++ b/internal/host/guardtransaction_test.go |
| @@ -270,17 +270,9 @@ func TestGuardRecoveryRefusesControllersAfterBegin(t *testing.T) { |
| t.Fatal(err) |
| } |
| } |
| - // With a journal open recover still fails, and Requires= |
| - // keeps the edge down. With none, the files are whole: |
| - // recover defers the settlement and exits 0, leaving the |
| - // transaction open. |
| - err = a.Run(t.Context(), true) |
| - if path.journal && err == nil { |
| + if err := a.Run(t.Context(), true); err == nil { |
| t.Fatal("unsafe recovery succeeded") |
| } |
| - if !path.journal && err != nil { |
| - t.Fatalf("recover with no journal open = %v, want a deferral that exits 0", err) |
| - } |
| if path.marker { |
| if _, err := os.Stat(marker); err != nil { |
| t.Fatal("marker discarded", err) |
| @@ -421,16 +413,11 @@ func TestPlan9M8GuardRecoveryRecoversPendingWithoutJournal(t *testing.T) { |
| } |
| completed := filepath.Join(a.floor.Dir, "completed.json") |
| if !recovers { |
| - // No journal is open, so recover defers and exits 0; |
| - // the controller that survived is never settled. |
| - if err != nil { |
| - t.Fatalf("recover with no journal open = %v, want a deferral that exits 0", err) |
| + if err == nil { |
| + t.Fatal("a controller that survived recovery was settled") |
| } |
| if _, statErr := os.Stat(completed); !os.IsNotExist(statErr) { |
| - t.Fatal("a controller that survived recovery was settled", statErr) |
| - } |
| - if _, statErr := os.Stat(marker); path.marker && statErr != nil { |
| - t.Fatal("marker discarded", statErr) |
| + t.Fatal("completion written", statErr) |
| } |
| return |
| } |
diff --git a/internal/host/recover_settlement_test.go b/internal/host/recover_settlement_test.go| index 414b9e779..f59278012 100644 |
| --- a/internal/host/recover_settlement_test.go |
| +++ b/internal/host/recover_settlement_test.go |
| @@ -2,12 +2,18 @@ package host |
| import ( |
| "context" |
| + "encoding/json" |
| "errors" |
| "fmt" |
| "os" |
| "path/filepath" |
| "strings" |
| "testing" |
| + |
| + "github.com/carlosframework/platform/internal/hostactors" |
| + "github.com/carlosframework/platform/internal/hostfloor" |
| + "github.com/carlosframework/platform/internal/release" |
| + "github.com/carlosframework/platform/internal/rootparticipation" |
| ) |
| // carlos-system-recover runs the root generation census to settle the |
| @@ -17,15 +23,25 @@ import ( |
| // defers the settlement instead: recover exits 0 with the transaction still |
| // open. With a journal open it fails as before. |
| -// refuseCensus makes root's launch census refuse, as the kernel census did |
| -// on the harness box ("service: permission denied"), and stubs the pending |
| -// controller recovery so the refusal is the census's alone. |
| -func refuseCensus(t *testing.T, f *applyFixture) *int { |
| +// harnessRefusal is the kernel census's refusal on the harness box, as |
| +// productionGenerationKernelCensus returns it and the launch verifier |
| +// passes it up unchanged. |
| +func harnessRefusal() error { |
| + return &generationCensusRefusal{err: fmt.Errorf("/proc/1535/task/1535: service: %w", os.ErrPermission)} |
| +} |
| + |
| +// refuseCensus makes root's launch census refuse from its pass-th call on |
| +// (1 is every call), and stubs the pending controller recovery so the |
| +// refusal is the census's alone. |
| +func refuseCensusFrom(t *testing.T, f *applyFixture, pass int) *int { |
| t.Helper() |
| calls := 0 |
| f.a.participation.VerifyLaunches = func(context.Context, uint64) error { |
| calls++ |
| - return fmt.Errorf("generation census: /proc/1535/task/1535: service: %w", os.ErrPermission) |
| + if calls < pass { |
| + return nil |
| + } |
| + return harnessRefusal() |
| } |
| old := recoverPendingControllers |
| t.Cleanup(func() { recoverPendingControllers = old }) |
| @@ -33,6 +49,8 @@ func refuseCensus(t *testing.T, f *applyFixture) *int { |
| return &calls |
| } |
| +func refuseCensus(t *testing.T, f *applyFixture) *int { return refuseCensusFrom(t, f, 1) } |
| + |
| func logLines(f *applyFixture) *[]string { |
| var lines []string |
| logf := f.a.ops.logf |
| @@ -184,4 +202,238 @@ func TestRuling6NoCensusRecoverIsUnchanged(t *testing.T) { |
| if _, err := os.Stat(filepath.Join(a.floor.Dir, "completed.json")); !os.IsNotExist(err) { |
| t.Fatalf("completion written: %v", err) |
| } |
| + // No census refusal can reach a no-census box's settlement, and one |
| + // that did would not defer either. |
| + if err := a.settleWithoutJournal(t.Context(), harnessRefusal()); errors.Is(err, errSettlementDeferred) { |
| + t.Fatalf("a no-census box deferred: %v", err) |
| + } |
| +} |
| + |
| +// openTransaction checks the guarded transaction is still open after a |
| +// deferral: the reservation and root's update record kept, so claim |
| +// acquisitions stay refused. marker says whether root's marker must be |
| +// there too. |
| +func openTransaction(t *testing.T, f *applyFixture, id string, marker bool) { |
| + t.Helper() |
| + a := f.a |
| + if _, err := os.Stat(filepath.Join(a.floor.Dir, "activation.json")); err != nil { |
| + t.Fatalf("reservation discarded: %v", err) |
| + } |
| + if _, err := os.Stat(filepath.Join(a.floor.Dir, "update-begun.json")); marker && err != nil { |
| + t.Fatalf("marker discarded: %v", err) |
| + } |
| + if got, err := a.participation.UpdateID(); err != nil || got != id { |
| + t.Fatalf("update record = %q, %v; want %s still open", got, err, id) |
| + } |
| + if err := a.participation.Acquire(t.Context(), func() error { return nil }); err == nil { |
| + t.Fatal("a claim acquisition started while the transaction was open") |
| + } |
| +} |
| + |
| +func TestRecoverDefersACensusRefusalOnlyAtTheEndOfParticipation(t *testing.T) { |
| + // The pending-controller census passes; the one inside EndParticipation |
| + // (EndUpdateWith -> idle) refuses. A finished transaction. |
| + f, id := guardRecoveryFixture(t) |
| + a := f.a |
| + if err := a.floor.Begin(t.Context(), id, a.dataDir); err != nil { |
| + t.Fatal(err) |
| + } |
| + calls := refuseCensusFrom(t, f, 2) |
| + if err := a.Run(t.Context(), true); err != nil { |
| + t.Fatalf("recover = %v, want a deferral that exits 0", err) |
| + } |
| + if *calls != 2 { |
| + t.Fatalf("census ran %d times, want 2 (controllers, then the end of participation)", *calls) |
| + } |
| + openTransaction(t, f, id, true) |
| + // The next recover, with the census passing, settles it. |
| + a.participation.VerifyLaunches = func(context.Context, uint64) error { return nil } |
| + if err := a.Run(t.Context(), true); err != nil { |
| + t.Fatal(err) |
| + } |
| + if raw, err := os.ReadFile(filepath.Join(a.floor.Dir, "completed.json")); err != nil || string(raw) != id { |
| + t.Fatalf("completion = %q, %v", raw, err) |
| + } |
| + if _, err := a.participation.UpdateID(); !os.IsNotExist(err) { |
| + t.Fatalf("update record after settlement: %v", err) |
| + } |
| +} |
| + |
| +// unfinishedGuard makes the fixture's transaction one that never changed |
| +// the box: the signed reservation names another image as its target. |
| +func unfinishedGuard(t *testing.T, f *applyFixture, id string) { |
| + t.Helper() |
| + path := filepath.Join(f.a.floor.Dir, "activation.json") |
| + raw, err := os.ReadFile(path) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var tx guardTransaction |
| + if err := json.Unmarshal(raw, &tx); err != nil { |
| + t.Fatal(err) |
| + } |
| + var roster hostactors.Roster |
| + if err := release.VerifyRootEnvelope(f.pub, tx.Roster, &roster); err != nil { |
| + t.Fatal(err) |
| + } |
| + h := roster.Activations[id] |
| + h.Hashes = map[string][]string{tx.Actor: {h.Hashes[tx.Actor][0], strings.Repeat("d", 64), h.Hashes[tx.Actor][2]}} |
| + roster.Activations[id] = h |
| + if err := roster.Validate("bucket"); err != nil { |
| + t.Fatal(err) |
| + } |
| + if tx.Roster, err = release.SignRootEnvelope(release.RootSigner(f.priv), roster); err != nil { |
| + t.Fatal(err) |
| + } |
| + raw, _ = json.Marshal(tx) |
| + if err := hostfloor.Durable(path, raw, 0644); err != nil { |
| + t.Fatal(err) |
| + } |
| + if finished, err := f.a.guardFinished(tx); err != nil || finished { |
| + t.Fatalf("fixture transaction finished=%t err=%v, want unfinished", finished, err) |
| + } |
| +} |
| + |
| +func TestRecoverDefersACensusRefusalOfAnUnfinishedTransaction(t *testing.T) { |
| + f, id := guardRecoveryFixture(t) |
| + a := f.a |
| + if err := a.floor.Begin(t.Context(), id, a.dataDir); err != nil { |
| + t.Fatal(err) |
| + } |
| + unfinishedGuard(t, f, id) |
| + refuseCensus(t, f) |
| + if err := a.Run(t.Context(), true); err != nil { |
| + t.Fatalf("recover = %v, want a deferral that exits 0", err) |
| + } |
| + // The withdrawal removes nothing until its census passes. |
| + openTransaction(t, f, id, true) |
| + if _, err := os.Stat(filepath.Join(a.floor.Dir, "completed.json")); !os.IsNotExist(err) { |
| + t.Fatalf("an unfinished transaction recorded a completion: %v", err) |
| + } |
| + a.participation.VerifyLaunches = func(context.Context, uint64) error { return nil } |
| + if err := a.Run(t.Context(), true); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, name := range []string{"update-begun.json", "activation.json", "completed.json"} { |
| + if _, err := os.Stat(filepath.Join(a.floor.Dir, name)); !os.IsNotExist(err) { |
| + t.Fatalf("%s after the withdrawal: %v", name, err) |
| + } |
| + } |
| +} |
| + |
| +func TestRecoverDefersACensusRefusalOfAnAlreadyCompletedTransaction(t *testing.T) { |
| + // Completion recorded and the marker gone, root's update record still |
| + // open: only EndParticipation is left, and its census refuses. |
| + f, id := guardRecoveryFixture(t) |
| + a := f.a |
| + if err := a.floor.Begin(t.Context(), id, a.dataDir); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := hostfloor.Durable(filepath.Join(a.floor.Dir, "completed.json"), []byte(id), 0644); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := hostfloor.RemoveDurable(filepath.Join(a.floor.Dir, "update-begun.json")); err != nil { |
| + t.Fatal(err) |
| + } |
| + calls := refuseCensus(t, f) |
| + if err := a.Run(t.Context(), true); err != nil { |
| + t.Fatalf("recover = %v, want a deferral that exits 0", err) |
| + } |
| + if *calls != 1 { |
| + t.Fatalf("census ran %d times, want 1", *calls) |
| + } |
| + openTransaction(t, f, id, false) |
| + if raw, err := os.ReadFile(filepath.Join(a.floor.Dir, "completed.json")); err != nil || string(raw) != id { |
| + t.Fatalf("completion = %q, %v", raw, err) |
| + } |
| +} |
| + |
| +func TestRecoverStillFailsSettlementErrorsThatAreNotCensusRefusals(t *testing.T) { |
| + for _, tc := range []struct { |
| + name string |
| + setup func(*testing.T, *applyFixture, string) |
| + }{ |
| + {"malformed-ledger-state", func(t *testing.T, f *applyFixture, _ string) { |
| + // The verifier refuses the ledger itself, not through the census. |
| + f.a.participation.VerifyLaunches = func(context.Context, uint64) error { return rootparticipation.ErrUnavailable } |
| + old := recoverPendingControllers |
| + t.Cleanup(func() { recoverPendingControllers = old }) |
| + recoverPendingControllers = func(context.Context, string, string) error { return nil } |
| + }}, |
| + {"live-controller", func(t *testing.T, f *applyFixture, _ string) { |
| + if err := os.WriteFile(filepath.Join(f.a.dataDir, hostexecDir, "host.json"), []byte(`{"state":"pending"}`), 0600); err != nil { |
| + t.Fatal(err) |
| + } |
| + old := recoverPendingControllers |
| + t.Cleanup(func() { recoverPendingControllers = old }) |
| + recoverPendingControllers = func(context.Context, string, string) error { return nil } |
| + }}, |
| + {"image-verification", func(t *testing.T, f *applyFixture, _ string) { |
| + if err := os.WriteFile(systemBinaryPath(f.a.optDir), []byte("an unsigned image"), 0755); err != nil { |
| + t.Fatal(err) |
| + } |
| + }}, |
| + {"marker-for-another-transaction", func(t *testing.T, f *applyFixture, _ string) { |
| + if err := hostfloor.Durable(filepath.Join(f.a.floor.Dir, "update-begun.json"), []byte(`{"id":"`+strings.Repeat("e", 32)+`"}`), 0644); err != nil { |
| + t.Fatal(err) |
| + } |
| + }}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + f, id := guardRecoveryFixture(t) |
| + if err := f.a.floor.Begin(t.Context(), id, f.a.dataDir); err != nil { |
| + t.Fatal(err) |
| + } |
| + tc.setup(t, f, id) |
| + err := f.a.Run(t.Context(), true) |
| + if err == nil || errors.Is(err, errSettlementDeferred) { |
| + t.Fatalf("recover = %v, want the settlement's own failure", err) |
| + } |
| + if _, statErr := os.Stat(filepath.Join(f.a.floor.Dir, "activation.json")); statErr != nil { |
| + t.Fatalf("reservation discarded: %v", statErr) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestOnlyCensusRefusal(t *testing.T) { |
| + refusal := harnessRefusal() |
| + for _, tc := range []struct { |
| + err error |
| + want bool |
| + }{ |
| + {refusal, true}, |
| + {fmt.Errorf("end participation: %w", refusal), true}, |
| + {errors.Join(refusal, nil), true}, |
| + {errors.Join(refusal, harnessRefusal()), true}, |
| + {errors.Join(refusal, errors.New("remove update.json: read-only file system")), false}, |
| + {fmt.Errorf("x: %w", errors.Join(refusal, os.ErrPermission)), false}, |
| + {os.ErrPermission, false}, |
| + {rootparticipation.ErrUnavailable, false}, |
| + {nil, false}, |
| + } { |
| + if got := onlyCensusRefusal(tc.err); got != tc.want { |
| + t.Fatalf("onlyCensusRefusal(%v) = %t, want %t", tc.err, got, tc.want) |
| + } |
| + } |
| +} |
| + |
| +func TestRecoverWithoutAnArmedFloorKeepsALateRefusalNonFatal(t *testing.T) { |
| + // The controllers' census passes; settling the update record at the end |
| + // of the run (settleUpdateBegun -> EndUpdateWith) refuses. As on main |
| + // that is logged, not fatal, and the record stays open. |
| + f := newApplyFixture(t, "stable", "old") |
| + if err := f.a.beginExecutableChange(t.Context(), "new", f.now); err != nil { |
| + t.Fatal(err) |
| + } |
| + calls := refuseCensusFrom(t, f, 2) |
| + if err := f.a.Run(t.Context(), true); err != nil { |
| + t.Fatalf("recover = %v", err) |
| + } |
| + if *calls < 2 { |
| + t.Fatalf("census ran %d times; the late settlement never ran", *calls) |
| + } |
| + if _, err := f.a.participation.UpdateID(); err != nil { |
| + t.Fatalf("a refused settlement closed the update record: %v", err) |
| + } |
| } |
diff --git a/internal/host/rootgeneration_census_linux.go b/internal/host/rootgeneration_census_linux.go| index 57fcdde84..d1dcb5681 100644 |
| --- a/internal/host/rootgeneration_census_linux.go |
| +++ b/internal/host/rootgeneration_census_linux.go |
| @@ -72,7 +72,16 @@ func checkedGenerationTree(ctx context.Context, tree string) error { |
| return nil |
| }) |
| } |
| -func productionGenerationKernelCensus(ctx context.Context) (string, string, error) { |
| + |
| +// productionGenerationKernelCensus is root's kernel generation census. Its |
| +// every refusal is a generationCensusRefusal, so a no-journal recovery can |
| +// tell it from the other ways a settlement fails. |
| +func productionGenerationKernelCensus(ctx context.Context) (boot, digest string, err error) { |
| + defer func() { |
| + if err != nil { |
| + boot, digest, err = "", "", &generationCensusRefusal{err: err} |
| + } |
| + }() |
| if os.Geteuid() != 0 { |
| return "", "", os.ErrPermission |
| } |
diff --git a/internal/host/rootgeneration_census_recover_linux_test.go b/internal/host/rootgeneration_census_recover_linux_test.go| index 50d163bb1..f1477d9ce 100644 |
| --- a/internal/host/rootgeneration_census_recover_linux_test.go |
| +++ b/internal/host/rootgeneration_census_recover_linux_test.go |
| @@ -387,3 +387,17 @@ func TestRecoverCensusRetriesOnlyTaskLevelTransients(t *testing.T) { |
| t.Fatalf("cancelled wait = %v", err) |
| } |
| } |
| + |
| +func TestProductionGenerationCensusRefusalsAreTyped(t *testing.T) { |
| + // Every refusal of the production census is a generationCensusRefusal, |
| + // which is what lets a no-journal recovery defer it. As a non-root test |
| + // the census refuses at its first check. |
| + if os.Geteuid() == 0 { |
| + t.Skip("the census does not refuse at once as root") |
| + } |
| + _, _, err := productionGenerationKernelCensus(t.Context()) |
| + var refusal *generationCensusRefusal |
| + if !errors.As(err, &refusal) || !onlyCensusRefusal(err) || !errors.Is(err, os.ErrPermission) { |
| + t.Fatalf("census refusal = %#v, want a typed census refusal", err) |
| + } |
| +} |
diff --git a/internal/host/rootgeneration_refusal.go b/internal/host/rootgeneration_refusal.go| new file mode 100644 |
| index 000000000..77da17f38 |
| --- /dev/null |
| +++ b/internal/host/rootgeneration_refusal.go |
| @@ -0,0 +1,42 @@ |
| +package host |
| + |
| +import "errors" |
| + |
| +// generationCensusRefusal is a refusal of root's kernel generation census |
| +// (productionGenerationKernelCensus). Recovery with no journal open defers |
| +// a settlement this refused (settleWithoutJournal), and only this: an image |
| +// that does not verify, malformed state and a failed durable write keep |
| +// their own errors and still fail. |
| +type generationCensusRefusal struct{ err error } |
| + |
| +func (r *generationCensusRefusal) Error() string { return "generation census: " + r.err.Error() } |
| + |
| +func (r *generationCensusRefusal) Unwrap() error { return r.err } |
| + |
| +// onlyCensusRefusal reports whether every error err carries is a |
| +// generationCensusRefusal: a wrapped refusal is, a join with any other |
| +// error (a durable write that failed beside it) is not. |
| +func onlyCensusRefusal(err error) bool { |
| + switch e := err.(type) { |
| + case nil: |
| + return false |
| + case *generationCensusRefusal: |
| + return true |
| + case interface{ Unwrap() []error }: |
| + found := false |
| + for _, inner := range e.Unwrap() { |
| + if inner == nil { |
| + continue |
| + } |
| + if !onlyCensusRefusal(inner) { |
| + return false |
| + } |
| + found = true |
| + } |
| + return found |
| + } |
| + if inner := errors.Unwrap(err); inner != nil { |
| + return onlyCensusRefusal(inner) |
| + } |
| + return false |
| +} |
diff --git a/internal/host/systemparticipation.go b/internal/host/systemparticipation.go| index 120738853..9d14514ee 100644 |
| --- a/internal/host/systemparticipation.go |
| +++ b/internal/host/systemparticipation.go |
| @@ -224,11 +224,10 @@ func (a *systemApplier) checkUnarmedParticipationRecovery(ctx context.Context) e |
| return err |
| } |
| if recover { |
| - settle := a.controllersRecovered |
| - if j == nil { |
| - settle = a.settleWithoutJournal |
| - } |
| - if err := settle(ctx); err != nil { |
| + if err := a.controllersRecovered(ctx); err != nil { |
| + if j == nil { |
| + err = a.settleWithoutJournal(ctx, err) |
| + } |
| return err |
| } |
| } |