From 13f65b4844893f1d741e4d5dd0cf2c03d5cb548f Mon Sep 17 00:00:00 2001 From: npeter83 Date: Sun, 25 Oct 2026 13:51:26 +0100 Subject: [PATCH] test(db): lock probes read only their own database's locks pg_locks is the whole server's: under the parallel lane a worker's own advisory claim on the same playlist id, or its own lock wait, was counted by another worker's probe (a leak reported where there was none, a commit made before the second tab blocked). --- backend/tests/test_discover.py | 10 ++++++++-- backend/tests/test_playlist_sync_claim.py | 8 ++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/backend/tests/test_discover.py b/backend/tests/test_discover.py index 31566b26..37c9e5d4 100644 --- a/backend/tests/test_discover.py +++ b/backend/tests/test_discover.py @@ -1105,10 +1105,16 @@ def test_two_overlapping_reorders_never_share_a_slot(db, as_user): worker = threading.Thread(target=second_tab) worker.start() - # Commit A only once B is blocked on a row lock, so B's read is the stale snapshot. + # Commit A only once B is blocked on a row lock, so B's read is the stale snapshot. pg_locks is + # the whole server's: only a wait in this database is B's (a parallel worker's is not). waiting = 0 for _ in range(200): - waiting = probe.scalar(text("SELECT count(*) FROM pg_locks WHERE NOT granted")) + waiting = probe.scalar( + text( + "SELECT count(*) FROM pg_locks WHERE NOT granted AND pid IN" + " (SELECT pid FROM pg_stat_activity WHERE datname = current_database())" + ) + ) probe.rollback() if waiting: break diff --git a/backend/tests/test_playlist_sync_claim.py b/backend/tests/test_playlist_sync_claim.py index fb133640..afce741e 100644 --- a/backend/tests/test_playlist_sync_claim.py +++ b/backend/tests/test_playlist_sync_claim.py @@ -89,9 +89,13 @@ def test_a_claim_is_released_when_its_block_raises(db, make_user): except RuntimeError: pass # Asked of pg_locks, not by claiming again: a lock leaked on a pooled connection would be granted - # again to that same connection (advisory locks are re-entrant per session). + # again to that same connection (advisory locks are re-entrant per session). pg_locks is the whole + # server's, so only this database's: a parallel worker's own claim on the same id counted here. held = db.scalar( - text("SELECT count(*) FROM pg_locks WHERE locktype = 'advisory' AND objid = :id"), + text( + "SELECT count(*) FROM pg_locks WHERE locktype = 'advisory' AND objid = :id" + " AND database = (SELECT oid FROM pg_database WHERE datname = current_database())" + ), {"id": pl.id}, ) assert held == 0