Skip to content

ops(migrate): bound migration advisory-lock acquisition with statement_timeout - #3509

Merged
bokelley merged 1 commit into
mainfrom
bokelley/migration-lock-timeout
Apr 29, 2026
Merged

bokelley merged 1 commit into
mainfrom
bokelley/migration-lock-timeout

Conversation

@bokelley

Copy link
Copy Markdown
Contributor

Summary

Defensive follow-up to PR #3491. Today, if a previous `runMigrations()` session crashes while holding `pg_advisory_lock` (release_command OOM, machine kill mid-migration, etc.), the next deploy waits until Postgres' TCP keepalive reaps the dead session — by default ≈2 hours. Fly's `release_command_timeout` is 15 minutes, so the deploy fails with no diagnostic about why it's waiting.

This PR caps the wait at 5 minutes via `SET statement_timeout = '5min'` on the lock-acquisition query, then clears the timeout immediately after the lock is held so the migration itself isn't capped. On timeout (pg code `57014`), we throw a clear error that names the lock key and the SQL to find and terminate the wedged session:

```
SELECT pid, state, query_start, query FROM pg_stat_activity
WHERE pid IN (SELECT pid FROM pg_locks WHERE locktype='advisory' AND objid=1835102578);
```

Happy path is unchanged: legitimate concurrent callers (release_command + a dev's docker-compose containers) still serialize on the blocking `pg_advisory_lock`. We just bound the stale-session wait.

Test plan

  • 19 migrate.test.ts cases pass (added 2: `statement_timeout` is set→cleared in order, and the 57014 path produces a clear error and still releases the connection)
  • `npx tsc --noEmit` clean
  • Pre-commit hook (unit + dynamic imports + typecheck) green
  • Manually verify the diagnostic error fires on a deliberately wedged session in dev

🤖 Generated with Claude Code

…t_timeout

If a prior runMigrations() session crashed while holding pg_advisory_lock,
the next deploy waits until Postgres' TCP keepalive reaps the dead session
(default ≈2 hours). With Fly's 15-minute release_command_timeout, the
deploy "hangs" until manual intervention.

acquireMigrationLock now sets statement_timeout to 5 minutes while waiting
for the lock, then clears it once acquired so the migration itself isn't
capped. On timeout (pg code 57014), we throw a diagnostic error naming the
lock key and the SQL to find/terminate the wedged session.

Behavior is unchanged in the happy path: legitimate concurrent callers
(release_command + a dev's docker-compose) still serialize on the
blocking pg_advisory_lock, just with a bound on stale-session waits.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@bokelley
bokelley merged commit 355742c into main Apr 29, 2026
14 checks passed
@bokelley
bokelley deleted the bokelley/migration-lock-timeout branch April 29, 2026 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant