Repository navigation
packages/django-dramatiq-postgres/broker: don't unlock messages that weren't locked - #26252
Conversation
…weren't locked Every consumer on a queue is notified of every message and races to claim it. Losing consumers called pg_advisory_unlock() for a lock they never acquired, which makes PostgreSQL log "you don't own a lock of type ExclusiveLock" (consumers - 1) times per task. Take the advisory lock with its own SELECT first and return without unlocking when it isn't acquired. Only unlock when the lock is held but the message isn't consumable.
✅ Deploy Preview for authentik-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #26252 +/- ##
==========================================
+ Coverage 91.87% 91.98% +0.11%
==========================================
Files 1174 1175 +1
Lines 76184 76421 +237
Branches 4056 4056
==========================================
+ Hits 69991 70299 +308
+ Misses 6151 6080 -71
Partials 42 42
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
| # attempts lose the race to another consumer. Unlocking a lock that isn't held logs a | ||
| # "you don't own a lock" warning in PostgreSQL, so only unlock when we acquired it. | ||
| cursor.execute( | ||
| "SELECT pg_try_advisory_lock(%s)", |
There was a problem hiding this comment.
Just wondering, with the lock happening before the UPDATE now, could delayed messages cause each consumer to grab and release the lock even though the message isn't ready yet?
Could this add unnecessary lock churn with a lot of delayed messages?
There was a problem hiding this comment.
Good catch, thanks. Delayed messages mostly don't get here: the enqueue NOTIFY only fires for eta IS NULL, and _fetch_pending_messages() already excludes eta >= now + timeout, which is the same window the UPDATE checks.
But you're right that there was still an acquire/release for messages that weren't consumable, e.g. finished or rejected messages when a consumer handles a notification late. I've moved the eligibility conditions into the lock query (SELECT pg_try_advisory_lock(...) FROM table WHERE ). Missing, finished and not-yet-due messages now cost a single query and never take the lock. There's a new test covering missing / done / rejected / not due, which asserts one query, no unlock and no lock held.
There was a problem hiding this comment.
Nice, moving the eligibility check into the lock query makes sense. Thanks for adding the regression coverage too.
…or consumable messages Move the consumable conditions (message id, state, eta window) into the pg_try_advisory_lock query, so the lock is only attempted for a message that can be consumed. Missing, finished, rejected and not yet due messages are skipped with a single query and no lock acquire/release.
✅ Deploy Preview for authentik-storybook ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Details
What does this PR change?
_PostgresConsumer._consume_one()now takes the message's advisory lock with its ownSELECT pg_try_advisory_lock(...)before running theUPDATE. If the lock isn't acquired, it returns without callingpg_advisory_unlock(). If the lock is acquired but theUPDATEmatches no row (message done, rejected or not due yet), it still unlocks, since the lock is now owned.Why is this change needed?
Every consumer on a queue gets the
NOTIFYfor every message and races to claim it. The losers'UPDATE ... AND pg_try_advisory_lock(...)matches 0 rows, and they then callpg_advisory_unlock()for a lock they never took. PostgreSQL logs this each time:How was this tested?
New tests in
authentik/tasks/tests/test_broker.py:UPDATEand no unlock; lock acquired but message not consumable -> unlock; message consumed -> no unlock.TestPostgresConsumerAdvisoryLocksuses real consumers and captures PostgreSQL notices on the locks connection:Linked issues
None.
Checklist
make all)make docs)