Skip to content

Add configurable stall timeout to auto-prune long-stalled workers (ephemeral is too aggressive for cache-heavy spot fleets) #9603

Closed

Description

@blarghmatey

Summary

Add a configurable grace period after which stalled (non-ephemeral) workers are automatically pruned, e.g. CONCOURSE_WORKER_STALL_TIMEOUT / --worker-stall-timeout. Today operators on autoscaling/spot infrastructure are forced to choose between two unsatisfying options: the ephemeral flag (which deletes workers on a ~60–90s heartbeat gap and wipes their cache state) or leaving genuinely-dead workers in stalled forever and pruning them by hand / via external tooling.

Current behavior

The GC worker collector (atc/gc/worker_collector.go) handles unresponsive workers in one of two ways depending on the ephemeral flag (atc/db/worker_lifecycle.go):

  • ephemeral = true → DeleteUnresponsiveEphemeralWorkers():
    DELETE FROM workers WHERE ephemeral AND expires < NOW()
  • ephemeral = false → StallUnresponsiveWorkers():
    UPDATE workers SET state='stalled' WHERE state='running' AND expires < NOW()

A worker's expires is set to NOW() + ttl on each heartbeat, where ttl = heartbeat-interval * 2 (default heartbeat interval 30s → 60s TTL, tsa/heartbeater.go). The GC sweep runs every 30s. So a worker is eligible for stall/delete ~60–90s after its last successful heartbeat.

Crucially, there is no path that ever deletes a non-ephemeral worker once it is stalled — it lingers indefinitely until an operator runs fly prune-worker.

The problem

On EC2 autoscaling groups using spot instances, workers regularly disappear without completing a graceful retire/land (hard spot reclamation, instance failure, etc.). Those workers go stalled and accumulate. Stalled workers are excluded from new placement (atc/worker/pool.go isWorkerCompatibleAndRunning), but their worker_resource_caches rows persist and can still be selected as streaming sources, adding build friction until they are pruned.

Why ephemeral is not an acceptable answer for cache-heavy fleets

ephemeral deletes the worker row, and every worker-scoped table (containers, volumes, worker_base_resource_types, worker_task_caches, worker_resource_caches) is ON DELETE CASCADE on workers(name). So deletion wipes the worker's entire DB cache footprint; baggageclaim then sweeps the now-"orphaned" on-disk volumes. For workers using volume-locality placement with large caches (hundreds of GB to TB), that is very expensive to rebuild.

The damaging part is a timing asymmetry:

  • Delete trigger: ~60–90s after a heartbeat gap.
  • Worker reconnect detection: up to 5 minutes — the worker→TSA keepalive timeout is hardcoded to time.Minute * 5 (tsa/client.go). On an unclean break (network partition, web/TSA node hang, TSA CPU saturation delaying heartbeat delivery) there's no TCP RST, so the worker doesn't notice the dead tunnel and re-dial for up to 5 minutes.

This means a perfectly healthy worker whose TSA connection is briefly disrupted — e.g. when a web/TSA node is rotated or restarted — can be deleted and cache-wiped while it is still running builds, only to reconnect moments later and re-register empty. In a fleet where the web nodes themselves autoscale/recycle, this happens routinely, producing worker "flapping" and cache thrash.

By contrast, a stalled worker recovers for free: HeartbeatWorker (atc/db/worker_factory.go) maps any non-landing/landed/retiring state back to running, so a transiently-disconnected worker returns to service with its container/volume/cache rows intact and zero re-streaming. The only thing missing is cleanup of workers that never come back.

Proposed solution

Add a stall-prune grace period so the GC deletes workers that have been stalled longer than a configurable duration. Something like:

CONCOURSE_WORKER_STALL_TIMEOUT (e.g. default 0 = disabled / never, opt-in)

Implementation sketch: track when a worker entered stalled (a stalled_since / reuse of a timestamp column), and add a DeleteStalledWorkers(timeout) step to workerCollector.Run analogous to DeleteUnresponsiveEphemeralWorkers, deleting rows where state='stalled' AND stalled_since < NOW() - timeout.

This gives operators the cleanup behavior of ephemeral (no manual pruning, no indefinite accumulation) without the hair-trigger cache wipe, because the grace period (minutes/hours, operator's choice) comfortably exceeds the worst-case reconnect window. Transiently-disconnected workers recover; only genuinely-dead ones are reaped.

Workaround we're using

In the meantime we run an external reconciliation reaper on the web nodes: a systemd timer that lists stalled workers via fly workers --json, cross-checks each against EC2 (the worker's hostname-derived private IP vs. the set of still-running worker instances), and only fly prune-workers those whose backing instance is confirmed gone. This is safe but is bespoke per-cloud glue that we think the scheduler could express natively with a stall timeout.

Happy to contribute a PR if the maintainers are open to the approach — would appreciate guidance on the preferred config surface (a global --worker-stall-timeout vs. a per-worker setting) and on whether reusing expires/adding a stalled_since column is preferred for tracking stall age.

Activity

  1. taylorsilva commented on Jun 16, 2026

    @taylorsilva
    Member

    Very reasonable ask, I'd accept a PR adding a CONCOURSE_WORKER_STALL_TIMEOUT config as described. Building upon what's in atc/gc/worker_collector.go is probably the approach I would take.

    Adding a column to the workers table to track how long a worker has been stalled sounds good to me. I'm not seeing any obvious issues with this approach. Let us know if you're able to make time to implement this just so myself and others know that it's being worked on.

  2. added 2 commits that reference this issue on Jun 17, 2026
    00c3e04
    7f2264f
  3. blarghmatey commented on Jun 17, 2026

    @blarghmatey
    ContributorAuthor

    Thanks for the feedback @taylorsilva. I just opened #9604 to address this

  4. taylorsilva commented on Jun 17, 2026

    @taylorsilva
    Member

    Thanks! I see the PR. I'll review it sometime this week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions