Skip to content

[bug] Task history cleanup stalls the server at database.max_connections=1 #5111

Done

Description

@Quick104

What happened

With database.max_connections set to 1, the task history cleanup stalls every database request on the server that runs it. PgExecutionRepository.Prune (internal/taskmanager/repository/postgres_history.go) takes its advisory lock with pglock.TryAcquire, which holds one of the pool's connections until the prune ends. It then runs its queries through that same pool, starting with listTaskKeys (r.pool.Query). With a one-connection pool, that query waits for the connection the lock is holding. It never gets it, so the prune and every other query on the server wait until the run's context is cancelled.

The setting accepts values down to 1 (normalizeAdminInt(key, value, 1, 10000) in internal/config/admin_settings.go, also settable through SILO_DB_MAX_CONNECTIONS), and the server supports a pool of that size elsewhere on purpose. The default of 20 doesn't hit this.

Steps to reproduce

Not reproduced live. Found by tracing the source on main at 1d6a023.

  1. Set database.max_connections to 1.
  2. Let the task history cleanup run, or start it.
  3. Prune takes the lock, and the SELECT DISTINCT task_key query that follows never gets a connection.

#5110 fixed the same stall for the cluster task lock (advisoryClusterLock). Its test opens a one-connection pool, holds the lock, and runs a pool query. On unfixed code that query hits its deadline, and the same harness applies here.

Expected behavior

The prune runs on a one-connection pool. It could hold the lock on its own session outside the pool, as #5110 does and as downloads.ArtifactRepository.WithMaintenanceLock already does, or run its queries on lock.Conn().

Silo version / commit

main at 1d6a023

Deployment

Any deployment with database.max_connections set to 1.

Clients affected

None directly. Server-side stall.

Relevant logs

No logs available.

Technical notes

Other pglock.TryAcquire callers that query the pool while they hold the lock may have the same stall. They are unchecked. Candidates: catalog search index maintenance, recommendations jobs, trickplay reconcile, storagetransition, metadata artwork release and storage sweep, chapterthumbs originals cleanup, extrastills, audiobooks author counts, and adminjob library refresh. Callers that do their work on lock.Conn(), such as the blob GC sweep, are unaffected. The playback plan store's session lock (planstore.Postgres.AcquireSessionLock) sizes its capacity as MaxConns/2 with a floor of 1. At a pool of 1 it pins the only connection while it issues store queries. That is also unchecked.

Defect: silo-server internal/taskmanager/repository PgExecutionRepository.Prune

AI disclosure

  • Harness: Claude Code in T3 Code
  • Tools: Claude Code
  • Model: claude-opus-5-5
  • Involvement: Fully AI-generated
  • Independent review: none

Confirmations

  • I searched existing issues.
  • Reproduced on a real deployment.

Source: found in review of #4642

Activity

  1. added
    bug
    on Nov 22, 2026
  2. added
    impact: usability
    priority: P2
    triaged
    on Nov 22, 2026
  3. Quick104 commented on Nov 22, 2026

    @Quick104
    ContributorAuthor

    One more candidate from the #5110 review: requestReconcileLocker (internal/taskmanager/tasks/reconcile_requests.go) takes requestTargetWriteAdvisoryLock with pglock.TryAcquire and holds the pooled connection while it waits. The download refresh task takes the same key, so at a pool size of 1 the two tasks may stall each other. Not checked against the source yet.

  4. Rhainland commented on Nov 23, 2026

    @Rhainland
    Contributor

    Reproduced on origin/main at 3b2b321: with SILO_DB_MAX_CONNECTIONS=1, running Database Maintenance (which runs the task history cleanup) made /api/v1/ready report postgres: false and authenticated API calls time out. The cleanup's advisory lock stayed on the pool's only connection while the cleanup sat idle.

    Cause: Prune held its lock on a pooled connection and then queried through the same pool, so its first query waited on itself and every other query waited behind it.

    Fix in #5197: the cleanup holds its lock on a separate database session, as the cluster task lock does since #5110, so the server keeps answering while it runs. Checked on a local build of the fix: the same steps on the same database keep /api/v1/ready at 200, the cleanup deletes the 500 expired rows, and no lock is left held. A new database test runs the prune on a one-connection pool and checks that it still skips while another server holds the lock.

    Separately, at a pool size of 1 two other tasks stall the server at startup on their own: generate_extra_stills (internal/extrastills) and cleanup_chapter_thumbnail_originals (internal/chapterthumbs) both hold the pool's only connection through pglock.TryAcquire while they query the pool. They are not part of this fix.

    AI disclosure: reproduced and fixed by Claude Code (claude-opus-5-5) at the maintainer's direction.

  5. added a commit that references this issue on Nov 24, 2026
    92ed2b4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugimpact: usabilitypriority: P2triaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions