Repository navigation
fix(taskmanager): keep task history cleanup from stalling a one-connection pool - #5197
Conversation
Prune took its advisory lock on a pooled connection and then ran its queries through the same pool. With database.max_connections set to 1, its first query waited on the connection the lock held, and every other query on the server waited with it. The lock now lives on a session outside the pool, as the cluster task lock does since Silo-Server#5110. pglock.TryAcquireSession is the shared form of that pattern: it opens the session from the pool's configuration, and Release unlocks before closing so another server trying right after finds the lock free.
…cquireSession advisoryClusterLock held its own copy of the session-lock steps the new helper now owns.
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
/silo review summaryReviewed head: 82912a1 (the PR commit 2b3e5cf with main merged in; the merge doesn't change the PR's diff). Fit: 9/10 · Readiness: 9/10 — ready The fix holds up. On a dev sandbox at What changed on the branch
Review findings and how they were handledNo defect needs a code change. A council of three models from different providers reviewed this head, alongside a multi-agent code review:
Bots: CodeRabbit generated no actionable comments, and Silo Kody left no inline findings. Pre-existing defects#5247: Checks
🤖 Claude Code (claude-opus-5-5) |
Problem
Closes #5111
Related issue: N/A
Validation tasks: none
Other clients: Apple N/A (no client-visible change); Android N/A (no client-visible change); Roku N/A (no client-visible change)
With
database.max_connectionsset to 1, running the task history cleanup (a step of Database Maintenance) stopped the server from answering any request that needed the database./api/v1/readyreportedpostgres: falseand authenticated API calls timed out until the run's context ended. Only servers that lower the pool to 1 are affected; the default of 20 hides it.PgExecutionRepository.Prunetook its advisory lock withpglock.TryAcquire, which keeps a pooled connection for as long as the lock is held. Its queries then went through the same pool, so with one connection they waited on the connection the lock was holding, and so did everything else on the server.Approach
The lock now lives on a database session of its own, opened with the pool's configuration but outside the pool, which is how #5110 fixed the cluster task lock. The steps for that are now
pglock.TryAcquireSession: open the session, try the lock, and on release unlock first and then close, so another server trying right after finds the lock free.advisoryClusterLockfrom #5110 now uses the same helper instead of its own copy; its behaviour is unchanged apart from also warning when the unlock reports the lock was not held.This costs one PostgreSQL connection beyond the pool while a prune runs, the same trade the cluster task lock, storage maintenance, and the notification interest rebuild already make. The prune runs as a step of Database Maintenance, whose own cluster lock already holds one such session, so a maintenance run uses two sessions outside the pool while the prune step runs.
Other
pglock.TryAcquirecallers are left for follow-ups. Three of them stall a one-connection pool at startup on their own, before any cleanup runs:generate_extra_stills(internal/extrastills/generator.go),release_stored_artwork(internal/metadata/artwork_release.go), andcleanup_chapter_thumbnail_originals(internal/chapterthumbs/originals_cleanup.go). With them scheduled, a server atdatabase.max_connections=1still stops answering at boot. #5247 tracks them.Validation
gofmt,go mod tidy -diff,go veton the changed packages: clean.make lint-changed,make lint-router-recovery: 0 issues.make test-go: passes exceptinternal/notificationsTestPushSenderRegistersRelayOnFirstSendWithoutCredential, which fails the same way onorigin/main. Five packages that check temp-directory permissions failed under the local umask and pass withumask 022.make test-db-contractsagainst a migrated Postgres: the new test and the existing task lock tests pass. Backup tests that need S3 orpg_dumpwere skipped.make verify-local-paths,verify-bidi,verify-case-collisions,verify-size-ratchet: pass.mainwithlisting task history keys: context deadline exceeded. It also checks that Prune still skips while another session holds the lock.Evidence
Evidence: https://evidence.siloserver.org/r/silo-server/pr-5197/
Checked on a local build with
SILO_DB_MAX_CONNECTIONS=1, 500 synthetic task history rows older than the retention window, and the two startup tasks named above unscheduled so the server boots. Same database and steps before and after: rundatabase_maintenancethroughPOST /api/v2/admin/tasks/database_maintenance/run, then probe the server every few seconds.Risks
A prune now opens one PostgreSQL connection outside the pool for the length of the run, on top of the one Database Maintenance's lock already holds. A deployment whose PostgreSQL connection limit equals the pool size exactly could have the prune fail to connect while the pool is full; the run then fails and the next one retries. The cluster task lock has had the same trade since #5110.
Checklist
AI Disclosure
Note
Fix task history cleanup stalling with a one-connection pool
pglock.TryAcquireSessionandSessionLock.Releasein session.go. Advisory locks are held on a dedicated connection outside the pool, bounded by areleaseTimeout.PgExecutionRepository.Prunein postgres_history.go now holds its cleanup lock on a separate session, so prune queries can use pool connections. Previously the lock occupied a pool connection, and a pool withMaxConns=1stalled.advisoryClusterLock.TryAcquirein refresh_all_library_metadata.go to delegate to the newpglockhelpers, removing its inline connection lifecycle.MaxConns=1and skips when another session holds the lock.SessionLock.Releaseuses a context not canceled with the caller's context; check release/close error joining in session.go if callers relied on cancellation.Macroscope summarized 82912a1.