Skip to content

packages/django-dramatiq-postgres/broker: don't unlock messages that weren't locked - #26252

Merged
rissson merged 2 commits into
goauthentik:mainfrom
ryanpesek:packages/django-dramatiq-postgres/dont-unlock-unowned
Sep 22, 2026
Merged

rissson merged 2 commits into
goauthentik:mainfrom
ryanpesek:packages/django-dramatiq-postgres/dont-unlock-unowned

Conversation

@ryanpesek

Copy link
Copy Markdown
Contributor

Details

What does this PR change?

_PostgresConsumer._consume_one() now takes the message's advisory lock with its own SELECT pg_try_advisory_lock(...) before running the UPDATE. If the lock isn't acquired, it returns without calling pg_advisory_unlock(). If the lock is acquired but the UPDATE matches 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 NOTIFY for every message and races to claim it. The losers' UPDATE ... AND pg_try_advisory_lock(...) matches 0 rows, and they then call pg_advisory_unlock() for a lock they never took. PostgreSQL logs this each time:

WARNING:  you don't own a lock of type ExclusiveLock

How was this tested?

New tests in authentik/tasks/tests/test_broker.py:

  • Mocked cursor: lock not acquired -> no UPDATE and no unlock; lock acquired but message not consumable -> unlock; message consumed -> no unlock.
  • TestPostgresConsumerAdvisoryLocks uses real consumers and captures PostgreSQL notices on the locks connection:
    • an unowned unlock emits the warning (sanity check for the notice capture)
    • a losing consumer emits no warning, and the winner still holds its lock
    • a consumer attempting a non-consumable message leaves no advisory lock held

Linked issues

None.


Checklist

  • The project has been linted, built, and tested (make all)
  • The documentation has been updated and formatted (make docs)
  • I have read the AI usage policy.

…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.
@ryanpesek
ryanpesek requested a review from a team as a code owner September 18, 2026 03:24
@netlify

netlify Bot commented Sep 18, 2026

Copy link
Copy Markdown

✅ Deploy Preview for authentik-docs ready!

Name Link
🔨 Latest commit cdf2633
🔍 Latest deploy log https://app.netlify.com/projects/authentik-docs/deploys/6aacaefba2c8aa000828a680
😎 Deploy Preview https://deploy-preview-26252--authentik-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@codecov

codecov Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.98%. Comparing base (095195c) to head (3b6a91a).
⚠️ Report is 49 commits behind head on main.

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              
Flag Coverage Δ
conformance 33.43% <0.00%> (-0.11%) ⬇️
e2e 38.19% <0.00%> (+0.34%) ⬆️
integration 29.73% <0.00%> (-0.55%) ⬇️
rust 42.89% <ø> (ø)
unit 93.57% <100.00%> (+0.03%) ⬆️
unit-migrate 93.58% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

# 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)",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@netlify

netlify Bot commented Sep 19, 2026

Copy link
Copy Markdown

✅ Deploy Preview for authentik-storybook ready!

Name Link
🔨 Latest commit 3b6a91a
🔍 Latest deploy log https://app.netlify.com/projects/authentik-storybook/deploys/6aadf1a7fa773c0008188919
😎 Deploy Preview https://deploy-preview-26252--authentik-storybook.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@rissson
rissson merged commit 1436c66 into goauthentik:main Sep 22, 2026
139 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to In Progress in authentik Core Sep 22, 2026
@github-project-automation github-project-automation Bot moved this from In Progress to Done in authentik Core Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants