From d2cbe2581467e6558efcd0048337bc8f8aaca56a Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 25 Oct 2026 02:46:16 +1100 Subject: [PATCH] fix(daemon): archied --once drains until callees of a waiting call finish --- internal/app/archied/bootstrap.go | 6 +----- internal/daemon/daemon.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index 42368180d..32c503530 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -795,11 +795,7 @@ func (b *boot) runLoop(ctx context.Context, once bool) error { b.health.markServing() b.announceReady() if once { - b.d.Cycle(ctx) - // A drain pass no longer waits for the tasks it submitted, so a - // single-cycle invocation waits for them here: exiting while their - // containers still run would kill work this pass accepted. - b.d.WaitForTasks() + b.d.RunOnce(ctx) return nil } b.log.Info("archied running", "repos", len(b.cfg.Repos), "poll", b.cfg.PollInterval.Std().String(), "label", b.cfg.Label) diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index c22738b60..02040ecf2 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -411,6 +411,28 @@ func (d *Daemon) pollIssuesWithConfig(ctx context.Context, fg forge.Forge, cfg c } } +// RunOnce runs one cycle and drains until no task is in flight. A run waiting +// on a callee stays in flight while the callee sits queued, so the queue is +// drained again until every task, callees included, has returned. +func (d *Daemon) RunOnce(ctx context.Context) { + defer d.WaitForTasks() + d.watchCallWaits(ctx) + d.Cycle(ctx) + if d.Tasks == nil { + return + } + ticker := time.NewTicker(time.Second) + defer ticker.Stop() + for d.taskDispatcher().inFlight() > 0 { + select { + case <-ctx.Done(): + return + case <-ticker.C: + d.drainNATS(ctx) + } + } +} + // Cycle is one poll-and-drain pass: enqueue newly assigned issues, // reconcile open PRs, run the binding dispatch loop, then process // queued tasks concurrently up to containers.max_concurrency. @@ -1113,6 +1135,13 @@ func (d *taskDispatcher) Submit( }() } +// inFlight is how many submitted tasks have not returned yet. +func (d *taskDispatcher) inFlight() int { + d.mu.Lock() + defer d.mu.Unlock() + return d.pending +} + func (d *taskDispatcher) Wait() { d.mu.Lock() defer d.mu.Unlock()