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).
This commit is contained in:
2026-10-25 13:51:26 +01:00
parent 2e1934689f
commit 13f65b4844
2 changed files with 14 additions and 4 deletions
+8 -2
View File
@@ -1105,10 +1105,16 @@ def test_two_overlapping_reorders_never_share_a_slot(db, as_user):
worker = threading.Thread(target=second_tab) worker = threading.Thread(target=second_tab)
worker.start() 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 waiting = 0
for _ in range(200): 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() probe.rollback()
if waiting: if waiting:
break break
+6 -2
View File
@@ -89,9 +89,13 @@ def test_a_claim_is_released_when_its_block_raises(db, make_user):
except RuntimeError: except RuntimeError:
pass pass
# Asked of pg_locks, not by claiming again: a lock leaked on a pooled connection would be granted # 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( 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}, {"id": pl.id},
) )
assert held == 0 assert held == 0