diff --git a/CHANGELOG.md b/CHANGELOG.md index 5663a6da4..ab8b02f38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ release's per-component sections are labelled beneath its heading. ### archied +- The systemd watchdog now also watches the task intake loop, and its warning names the loop that stalled. - archied no longer restarts about a minute after it starts. Under systemd, the watchdog never saw a poll pass and killed it on every start since 1.56.0. ## [1.58.0] - 2026-11-01 diff --git a/internal/app/archied/sd_notify.go b/internal/app/archied/sd_notify.go index 35d6b9a92..f5bac6b5a 100644 --- a/internal/app/archied/sd_notify.go +++ b/internal/app/archied/sd_notify.go @@ -7,7 +7,7 @@ // because archie-messaging, a separate process that cannot import this // package, is the next process that must be able to announce itself. // What stays here is what is the daemon's own: which -// marker counts as progress (daemon.LastPollAt) and how long the loop may go +// marker counts as progress (daemon.StalestLoop) and how long a loop may go // without beginning a poll pass. // // Nothing here is fatal -- a process with no NOTIFY_SOCKET (a plain terminal, @@ -53,7 +53,7 @@ func (b *boot) startWatchdog(ctx context.Context) { sampler := sdnotify.Watchdog{ Interval: interval, Cadence: b.loopCadence, - Progress: b.d.LastPollAt, + Progress: b.d.StalestLoop, Beat: func() { n.Send(sdnotify.WatchdogState) }, Log: b.log, } diff --git a/internal/daemon/cycle_test.go b/internal/daemon/cycle_test.go index 46aaad5dc..c5dbe8030 100644 --- a/internal/daemon/cycle_test.go +++ b/internal/daemon/cycle_test.go @@ -5,12 +5,12 @@ import ( "testing" ) -// The systemd watchdog beats only while LastPollAt is fresh, so a cycle that +// The systemd watchdog beats only while StalestLoop is fresh, so a cycle that // doesn't stamp it gets archied killed shortly after every start. -func TestCycleMarksPollPass(t *testing.T) { +func TestCycleMarksPass(t *testing.T) { d := &Daemon{} d.Cycle(context.Background()) - if d.LastPollAt().IsZero() { - t.Fatal("Cycle didn't record a poll pass") + if loop, at := d.StalestLoop(); loop != "cycle" || at.IsZero() { + t.Fatalf("StalestLoop() = %q, %v; want a cycle pass", loop, at) } } diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 15413069a..6e2335277 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -240,10 +240,10 @@ type Daemon struct { // to use. running runningTasks - // lastPollAt maps each poll loop (an identity ID, or "" for the root - // loop) to when its latest pass began, as Unix nanoseconds. LastPollAt - // reports the oldest, so one wedged loop is not hidden by a healthy one. - lastPollAt sync.Map + // loopPasses maps each run loop ("cycle", "drain") to when its latest + // pass began, as Unix nanoseconds. StalestLoop reports the oldest, so one + // wedged loop isn't hidden by a healthy one. + loopPasses sync.Map } // IssueSource is one forge connection's repositories as the identity @@ -389,6 +389,7 @@ func (d *Daemon) drainLoop(ctx context.Context) { ticker := time.NewTicker(time.Second) defer ticker.Stop() for { + d.markPass("drain") d.drainNATS(ctx) select { case <-ctx.Done(): @@ -489,7 +490,7 @@ func (d *Daemon) RunOnce(ctx context.Context) { // tasks; maintenance runs as built-in schedules. Draining queued tasks is the caller's job // (drainLoop in Run, drainNATS in RunOnce) so there is one drainer. func (d *Daemon) Cycle(ctx context.Context) { - d.markPoll("") + d.markPass("cycle") d.dispatchBindings(ctx) d.drainReactions(ctx) } @@ -1206,7 +1207,6 @@ func (d *taskDispatcher) Wait() { // afresh each cycle so a rotated credential or a new connection applies to // the next poll. func (d *Daemon) poll(ctx context.Context) error { - d.markPoll("") // Webhook intake replaces polling; an empty pass is not a failure. if d.Cfg.Get().Forge.Intake == config.ForgeIntakeWebhook { return nil @@ -2604,26 +2604,28 @@ func (d *Daemon) allowConcurrentForTask(task *workflow.Task) bool { return ok && repo.AllowConcurrent } -// LastPollAt reports when the least recently polled loop last began a pass, -// or the zero time when no pass has started yet. -func (d *Daemon) LastPollAt() time.Time { +// StalestLoop reports the run loop that least recently began a pass and when, +// or the zero time when no loop has started yet. The systemd watchdog beats +// only while this is fresh, so every loop Run starts must mark its passes. +func (d *Daemon) StalestLoop() (string, time.Time) { + var loop string var oldest int64 - d.lastPollAt.Range(func(_, v any) bool { - if ns, ok := v.(int64); ok && (oldest == 0 || ns < oldest) { - oldest = ns + d.loopPasses.Range(func(k, v any) bool { + name, okName := k.(string) + if ns, ok := v.(int64); ok && okName && (oldest == 0 || ns < oldest) { + loop, oldest = name, ns } return true }) if oldest == 0 { - return time.Time{} + return "", time.Time{} } - return time.Unix(0, oldest).UTC() + return loop, time.Unix(0, oldest).UTC() } -// markPoll stamps the start of a poll pass for one loop, so a hung pass shows -// as stale. -func (d *Daemon) markPoll(loop string) { - d.lastPollAt.Store(loop, time.Now().UnixNano()) +// markPass stamps the start of one loop's pass, so a hung pass shows as stale. +func (d *Daemon) markPass(loop string) { + d.loopPasses.Store(loop, time.Now().UnixNano()) } // errContainerExited is the cancellation cause when a task's agent container diff --git a/internal/sdnotify/sdnotify.go b/internal/sdnotify/sdnotify.go index fe4548a23..78a234f16 100644 --- a/internal/sdnotify/sdnotify.go +++ b/internal/sdnotify/sdnotify.go @@ -114,9 +114,9 @@ type Watchdog struct { Interval time.Duration // Cadence reports how long the loop may go between passes. Cadence func() time.Duration - // Progress reports the start of the loop's most recent pass, or the zero - // time when no pass has begun yet. - Progress func() time.Time + // Progress reports the stalest loop and the start of its most recent + // pass, or the zero time when no pass has begun yet. + Progress func() (string, time.Time) // Beat emits one WATCHDOG=1. Beat func() Log *slog.Logger @@ -138,7 +138,7 @@ func (w Watchdog) Run(ctx context.Context) error { case <-ticker.C: } - last := w.Progress() + loop, last := w.Progress() if last.IsZero() { // No pass has begun at all. Measure staleness from arming, so a // loop that never starts polling is caught like one that stopped. @@ -149,7 +149,7 @@ func (w Watchdog) Run(ctx context.Context) error { case age > cadence: if !stalled { w.Log.WarnContext(ctx, "systemd watchdog: run loop has not begun a poll pass within its interval; withholding the heartbeat until it does", - "stale_for", age.String(), "loop_cadence", cadence.String()) + "loop", loop, "stale_for", age.String(), "loop_cadence", cadence.String()) stalled = true } default: