activator: log why a just-started unit is killed and why a wake failed
When the activator refused root's answer to a generation start (an identity mismatch, no invocation, a lost reply), it killed the unit and wrote nothing to the journal; a failed wake left only the member's event. On Tito staging (#854) the only trace was systemd's "SIGKILL ... on client request" once a minute. The rootfs and binary generation kill sites now log the reason before the kill. wake() logs a failed wake that residentFailure counts as a failure (not a fence, shutdown or another box's lease), at once the first time and then at most once a minute per host with a count of the failures between; a wake that works resets it. (#861)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+250
−0
internal/activator/activator.go+8 −0internal/activator/backend_exec.go+2 −0internal/activator/rootfs.go+3 −0internal/activator/startkill_log_test.go+178 −0internal/activator/wakefaillog.go+59 −0
CI cancelled — superseded by a newer push — run details
diff --git a/internal/activator/activator.go b/internal/activator/activator.go| index 5ae375919..8dd51694d 100644 |
| --- a/internal/activator/activator.go |
| +++ b/internal/activator/activator.go |
| @@ -1498,6 +1498,10 @@ type Service struct { |
| rootfsGateMu sync.Mutex |
| rootfsGateRuns map[string]*rootfsGateRun |
| rootfsRefusedSeen map[string]string |
| + // wakeFailMu guards wakeFailLogged, the journal's rate limit on failed |
| + // wakes per host (logWakeFailure). A leaf lock, like rootfsGateMu. |
| + wakeFailMu sync.Mutex |
| + wakeFailLogged map[string]*wakeFailNote |
| // Residency (Plan 3 Task 8, rootfs_service.go). residentLister is the |
| // DueStore read as a ResidentLister at New (nil: no residents). |
| // residents is each listed resident's backoff and reservation, by host; |
| @@ -2160,7 +2164,11 @@ func (a *Service) wake(e *actEntry) { |
| } else if msg, ok := a.wakeFailureEvent(e, err); ok && !errors.Is(err, ErrDutyDeferred) { |
| emitEvent(a.Logs, e.route, "error", msg) |
| } |
| + if !genStartUnproven(err) && !errors.Is(err, errRootfsNotAdmitted) && !errors.Is(err, ErrDutyDeferred) && residentFailure(err) { |
| + a.logWakeFailure(e.host, err) |
| + } |
| } else { |
| + a.clearWakeFailure(e.host) |
| emitEvent(a.Logs, e.route, "info", wakeEventMessage(w, e.worker)) |
| if steps := w.steps.String(); steps != "" { |
| log.Printf("activator: %s: woke in %s: %s", e.host, w.wakeTook.Round(time.Millisecond), steps) |
diff --git a/internal/activator/backend_exec.go b/internal/activator/backend_exec.go| index 1bd77ff5e..64a4cb8a0 100644 |
| --- a/internal/activator/backend_exec.go |
| +++ b/internal/activator/backend_exec.go |
| @@ -2956,6 +2956,8 @@ func (b *ExecBackend) spawnUnitFrom(ctx context.Context, rt registry.Route, sock |
| // crashed at its start (errStartedThenExited, ruling 206). |
| err = exitedUnobserved{unit: runUnitName(rt.Host, gen)} |
| } |
| + // The journal's only account of this kill (#861). |
| + log.Printf("activator: %s: start of %s failed, it may have begun: %v; killing it", rt.Host, runUnitName(rt.Host, gen), err) |
| if kerr := b.killFailedGen(ctx, rt, gen); kerr != nil { |
| // Nothing is released: the generation may still be running. |
| return nil, "", markStartUncertain(fmt.Errorf("%s: start unit: %w; %w: %v", rt.Host, err, errGenUnproven, kerr)) |
diff --git a/internal/activator/rootfs.go b/internal/activator/rootfs.go| index 50e490119..5d390520a 100644 |
| --- a/internal/activator/rootfs.go |
| +++ b/internal/activator/rootfs.go |
| @@ -404,6 +404,9 @@ func (b *ExecBackend) spawnRootfs(ctx context.Context, rt registry.Route, sock s |
| if err == nil { |
| err = exitedUnobserved{unit: unit} |
| } |
| + // The journal's only account of this kill (#861): systemd's own |
| + // line says no more than "on client request". |
| + log.Printf("activator: %s: start of %s failed, it may have begun: %v; killing it", rt.Host, unit, err) |
| if kerr := b.killFailedRootfsGen(ctx, rt, gen); kerr != nil { |
| // Nothing is released: the generation may still be running. |
| return nil, "", markStartUncertain(fmt.Errorf("%s: start unit: %w; %w: %v", rt.Host, err, errGenUnproven, kerr)) |
diff --git a/internal/activator/startkill_log_test.go b/internal/activator/startkill_log_test.go| new file mode 100644 |
| index 000000000..441297ff2 |
| --- /dev/null |
| +++ b/internal/activator/startkill_log_test.go |
| @@ -0,0 +1,178 @@ |
| +package activator |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "fmt" |
| + "log" |
| + "strings" |
| + "sync" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/instancectl" |
| +) |
| + |
| +// #861: a start the activator refuses after root was asked ends in a kill |
| +// of the unit, and until now nothing in the journal said why: on Tito's |
| +// staging box (#854) the only trace was systemd's "SIGKILL ... on client |
| +// request" once a minute. These tests hold the two lines that now explain |
| +// it: the kill site's, and the failed wake's. |
| + |
| +// startKillLog captures the standard logger's output safely, since the |
| +// activator's own goroutines may log while a test reads. |
| +type startKillLog struct { |
| + mu sync.Mutex |
| + buf strings.Builder |
| +} |
| + |
| +func (l *startKillLog) Write(p []byte) (int, error) { |
| + l.mu.Lock() |
| + defer l.mu.Unlock() |
| + return l.buf.Write(p) |
| +} |
| + |
| +func (l *startKillLog) lines(substr string) []string { |
| + l.mu.Lock() |
| + defer l.mu.Unlock() |
| + var out []string |
| + for _, line := range strings.Split(l.buf.String(), "\n") { |
| + if strings.Contains(line, substr) { |
| + out = append(out, line) |
| + } |
| + } |
| + return out |
| +} |
| + |
| +func captureLogLocked(t *testing.T) *startKillLog { |
| + t.Helper() |
| + l := &startKillLog{} |
| + prev, flags := log.Writer(), log.Flags() |
| + log.SetOutput(l) |
| + log.SetFlags(0) |
| + t.Cleanup(func() { log.SetOutput(prev); log.SetFlags(flags) }) |
| + return l |
| +} |
| + |
| +// mismatchRootfsCtl answers a rootfs start with another host's unit, the |
| +// shape of an identity check that refuses root's answer (#854's |
| +// EXEC-SANDBOX-MISMATCH). |
| +type mismatchRootfsCtl struct{ *fakeControl } |
| + |
| +func (c mismatchRootfsCtl) StartRootfs(ctx context.Context, host string, gen uint64, max int64, img instancectl.StagedImage) (instancectl.StartResponse, error) { |
| + got, err := c.fakeControl.StartRootfs(ctx, host, gen, max, img) |
| + got.Unit = instancectl.RootfsGenUnitName("other.example.com", gen) |
| + return got, err |
| +} |
| + |
| +func TestARefusedRootfsGenerationStartSaysWhyBeforeItsKill(t *testing.T) { |
| + for _, tc := range []struct { |
| + name string |
| + set func(b *ExecBackend, ctl *fakeControl) |
| + want string |
| + }{ |
| + {"identity mismatch", func(b *ExecBackend, ctl *fakeControl) { b.Control = mismatchRootfsCtl{ctl} }, "EXEC-SANDBOX-MISMATCH"}, |
| + {"no invocation", func(_ *ExecBackend, ctl *fakeControl) { ctl.inv = "" }, "exited"}, |
| + {"start error", func(_ *ExecBackend, ctl *fakeControl) { |
| + ctl.startErr = errors.New("instance control /start: 502: bus timeout") |
| + }, "bus timeout"}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + logs := captureLogLocked(t) |
| + b, ctl := newRootfsBackend(t) |
| + b.GenContainment = true |
| + b.CgroupEvents = func(string, string) (bool, bool, error) { return false, false, nil } |
| + tc.set(b, ctl) |
| + rt := rootfsRoute(b) |
| + if _, _, err := b.StartGen(context.Background(), rt, 4, true); err == nil { |
| + t.Fatal("started") |
| + } |
| + ctl.mustHaveCallPrefix(t, "stop-rootfs "+rt.Host+" role=run gen=4 worker= kill=true") |
| + unit := instancectl.RootfsGenUnitName(rt.Host, 4) |
| + got := logs.lines("killing it") |
| + if len(got) != 1 || !strings.Contains(got[0], unit) || !strings.Contains(got[0], tc.want) { |
| + t.Errorf("kill-site lines = %q, want one naming %s and %q", got, unit, tc.want) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestARefusedBinaryGenerationStartSaysWhyBeforeItsKill(t *testing.T) { |
| + logs := captureLogLocked(t) |
| + ctl := &fakeControl{} |
| + b, rt, _, _ := genStartFixture(t, ctl) |
| + if _, _, err := b.StartGen(context.Background(), rt, 7, false); err == nil { |
| + t.Fatal("a start with no invocation succeeded") |
| + } |
| + got := logs.lines("killing it") |
| + if len(got) != 1 || !strings.Contains(got[0], instancectl.GenUnitName(rt.Host, 7)) || !strings.Contains(got[0], "exited") { |
| + t.Errorf("kill-site lines = %q", got) |
| + } |
| +} |
| + |
| +// A failed wake reaches the journal: at once the first time, then at most |
| +// once a minute per host with a count of the failures in between, and at |
| +// once again after a wake that worked. A routine refusal (a fence) is not |
| +// a failure and writes nothing. |
| +func TestAFailedWakeIsLoggedAndRateLimited(t *testing.T) { |
| + logs := captureLogLocked(t) |
| + a, b, clk, reg := testActivator(t, time.Minute) |
| + defer reg.Close() |
| + const host = "shop.example.com" |
| + wakeOnce := func() { _, _ = a.beginRequest(context.Background(), testRoute(host)) } |
| + failed := func() []string { return logs.lines("activator: " + host + ": wake failed") } |
| + |
| + b.mu.Lock() |
| + b.startErr = errors.New("start unit: boom") |
| + b.mu.Unlock() |
| + for range 3 { |
| + wakeOnce() |
| + } |
| + if got := failed(); len(got) != 1 || !strings.Contains(got[0], "boom") { |
| + t.Fatalf("after three failures in one minute: %q, want one line with the reason", got) |
| + } |
| + |
| + clk.advance(wakeFailureLogEvery + time.Second) |
| + wakeOnce() |
| + got := failed() |
| + if len(got) != 2 || !strings.Contains(got[1], "2 more since") { |
| + t.Fatalf("a minute later: %q, want a second line counting the 2 unlogged failures", got) |
| + } |
| + |
| + b.mu.Lock() |
| + b.startErr = nil |
| + b.mu.Unlock() |
| + wakeOnce() |
| + waitFor(t, "the wake to finish", func() bool { |
| + a.mu.Lock() |
| + defer a.mu.Unlock() |
| + e := a.entries[host] |
| + return e != nil && e.state == actAwake |
| + }) |
| + a.mu.Lock() |
| + if e := a.entries[host]; e != nil { |
| + a.beginStopLocked(e, StopIdle) |
| + } |
| + a.mu.Unlock() |
| + waitFor(t, "the stop to finish", func() bool { |
| + a.mu.Lock() |
| + defer a.mu.Unlock() |
| + return a.entries[host] == nil |
| + }) |
| + b.mu.Lock() |
| + b.startErr = errors.New("start unit: boom again") |
| + b.mu.Unlock() |
| + wakeOnce() |
| + if got := failed(); len(got) != 3 || !strings.Contains(got[2], "boom again") || strings.Contains(got[2], "more since") { |
| + t.Fatalf("after a good wake: %q, want a third line at once, with no carried count", got) |
| + } |
| + |
| + const fenced = "fenced.example.com" |
| + b.mu.Lock() |
| + b.startErr = fmt.Errorf("start: %w", ErrFenced) |
| + b.mu.Unlock() |
| + _, _ = a.beginRequest(context.Background(), testRoute(fenced)) |
| + if got := logs.lines("activator: " + fenced + ": wake failed"); len(got) != 0 { |
| + t.Errorf("a fence refusal was logged as a failed wake: %q", got) |
| + } |
| +} |
diff --git a/internal/activator/wakefaillog.go b/internal/activator/wakefaillog.go| new file mode 100644 |
| index 000000000..d2f90e982 |
| --- /dev/null |
| +++ b/internal/activator/wakefaillog.go |
| @@ -0,0 +1,59 @@ |
| +package activator |
| + |
| +import ( |
| + "log" |
| + "time" |
| +) |
| + |
| +// wakeFailureLogEvery is the most often one host's failed wakes reach the |
| +// journal (#861). An always-on resident retries on its backoff and a |
| +// request-driven host on its next request, so a host that cannot start |
| +// would otherwise write a line per attempt. |
| +const wakeFailureLogEvery = time.Minute |
| + |
| +// wakeFailNote is one host's last journalled failure and the failures |
| +// since that were not logged. |
| +type wakeFailNote struct { |
| + at time.Time |
| + suppressed int |
| +} |
| + |
| +// logWakeFailure writes a failed wake's reason to the journal (#861): |
| +// before this, a wake that failed after root was asked left only the |
| +// member's event, and on a box the operator read nothing but systemd's |
| +// "SIGKILL ... on client request". The first failure logs at once; after |
| +// that at most one line per wakeFailureLogEvery, counting the failures in |
| +// between. Callers pass only real failures (residentFailure), never a |
| +// routine refusal such as a fence or another box's lease. |
| +func (a *Service) logWakeFailure(host string, err error) { |
| + now := a.now() |
| + a.wakeFailMu.Lock() |
| + if a.wakeFailLogged == nil { |
| + a.wakeFailLogged = map[string]*wakeFailNote{} |
| + } |
| + n := a.wakeFailLogged[host] |
| + if n != nil && now.Sub(n.at) < wakeFailureLogEvery { |
| + n.suppressed++ |
| + a.wakeFailMu.Unlock() |
| + return |
| + } |
| + suppressed := 0 |
| + if n != nil { |
| + suppressed = n.suppressed |
| + } |
| + a.wakeFailLogged[host] = &wakeFailNote{at: now} |
| + a.wakeFailMu.Unlock() |
| + if suppressed > 0 { |
| + log.Printf("activator: %s: wake failed (%d more since the last line): %v", host, suppressed, err) |
| + return |
| + } |
| + log.Printf("activator: %s: wake failed: %v", host, err) |
| +} |
| + |
| +// clearWakeFailure forgets host's failure note after a wake that worked, |
| +// so its next failure is logged at once. |
| +func (a *Service) clearWakeFailure(host string) { |
| + a.wakeFailMu.Lock() |
| + delete(a.wakeFailLogged, host) |
| + a.wakeFailMu.Unlock() |
| +} |