Skip to content

packages/django-dramatiq-postgres/broker: fix race condition in broker causing completed tasks to be repeated - #23218

Merged
rissson merged 1 commit into
goauthentik:mainfrom
ryanpesek:fix-race-in-broker
Jun 22, 2026
Merged

rissson merged 1 commit into
goauthentik:mainfrom
ryanpesek:fix-race-in-broker

Conversation

@ryanpesek

Copy link
Copy Markdown
Contributor

Details

Fixes a race in the Postgres Dramatiq broker where a completed task could be picked up again immediately after successful processing.

_post_process_message() removed the task from in_processing and queued its advisory lock for release before updating the DB row to DONE/REJECTED. Under load, the consumer could briefly see the same non-terminal row again, consume it, and then fail to decode it after the original ack cleared message to b"".

This showed up in worker logs as:

Failed to decode task, rejecting

For affected rows, the task logs showed Task finished processing without errors immediately before the decode failure, and the DB row had octet_length(message) = 0.
This also hardens requeue() so it does not move already terminal DONE/REJECTED rows back to QUEUED.

History

The underlying race appears to have been introduced by #17335, which added the current post-processing flow and removed tasks from in_processing before the terminal DB update.
The visible zero-byte decode symptom became possible after #19340, which intentionally clears successful task messages by storing message = b"".
The current log message was added by #22110, which catches decode failures and rejects the task instead of letting task processing stop.

Changes

  • Update task DB state before removing the message from in_processing.
  • Keep the message in processing if the terminal DB update fails.
  • Prevent requeue() from requeueing terminal rows.
  • Add regression tests for post-processing ordering and terminal requeue handling.

Checklist

  • The project has been linted, built, and tested (make all)
  • The documentation has been updated and formatted (make docs)

@ryanpesek
ryanpesek requested a review from a team as a code owner June 20, 2026 02:26
@netlify

netlify Bot commented Jun 20, 2026

Copy link
Copy Markdown

✅ Deploy Preview for authentik-integrations ready!

Name Link
🔨 Latest commit aae5dca
🔍 Latest deploy log https://app.netlify.com/projects/authentik-integrations/deploys/6a35fa650120020008aab0a6
😎 Deploy Preview https://deploy-preview-23218--authentik-integrations.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.

@netlify

netlify Bot commented Jun 20, 2026

Copy link
Copy Markdown

✅ Deploy Preview for authentik-storybook ready!

Name Link
🔨 Latest commit aae5dca
🔍 Latest deploy log https://app.netlify.com/projects/authentik-storybook/deploys/6a35fa65ac8ea70008730730
😎 Deploy Preview https://deploy-preview-23218--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.

@netlify

netlify Bot commented Jun 20, 2026

Copy link
Copy Markdown

✅ Deploy Preview for authentik-docs ready!

Name Link
🔨 Latest commit aae5dca
🔍 Latest deploy log https://app.netlify.com/projects/authentik-docs/deploys/6a35fa67c5c5a30008bd9a42
😎 Deploy Preview https://deploy-preview-23218--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 Jun 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.24%. Comparing base (cea7d63) to head (aae5dca).
⚠️ Report is 44 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #23218      +/-   ##
==========================================
+ Coverage   93.16%   93.24%   +0.08%     
==========================================
  Files        1035     1036       +1     
  Lines       60338    60411      +73     
  Branches      400      400              
==========================================
+ Hits        56212    56329     +117     
+ Misses       4126     4082      -44     
Flag Coverage Δ
conformance 36.42% <0.00%> (-0.05%) ⬇️
e2e 41.66% <0.00%> (+1.55%) ⬆️
integration 32.38% <0.00%> (-0.53%) ⬇️
rust 0.00% <ø> (ø)
unit 92.21% <100.00%> (+5.17%) ⬆️
unit-migrate 92.25% <100.00%> (+0.01%) ⬆️

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.

@BeryJu
BeryJu requested a review from rissson June 21, 2026 19:30
@rissson rissson self-assigned this Jun 22, 2026
@github-project-automation github-project-automation Bot moved this from Todo to In Progress in authentik Core Jun 22, 2026
@rissson rissson changed the title dramatiq/broker: fix race condition in broker causing completed tasks to be repeated packages/django-dramatiq-postgres/broker: fix race condition in broker causing completed tasks to be repeated Jun 22, 2026
@rissson rissson added the backport/version-2026.5 Add this label to PRs to backport changes to version-2026.5 label Jun 22, 2026
@rissson
rissson merged commit 9a9b4b5 into goauthentik:main Jun 22, 2026
107 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in authentik Core Jun 22, 2026
@authentik-automation

Copy link
Copy Markdown
Contributor

🍒 Cherry-pick to version-2026.5 created: #23257

@authentik-automation

Copy link
Copy Markdown
Contributor

⚠️ Cherry-pick to version-2026.2 has conflicts: #23258

rissson pushed a commit that referenced this pull request Jun 22, 2026
…r causing completed tasks to be repeated (cherry-pick #23218 to version-2026.5) (#23257)

Co-authored-by: Ryan Pesek <[email protected]>
fix race condition in broker causing completed tasks to be repeated (#23218)
kensternberg-authentik added a commit that referenced this pull request Jun 22, 2026
* main: (30 commits)
  core, flows: drop maximum-scale=1 from viewport meta (#23261)
  web: bump vite from 8.0.12 to 8.0.16 in /web (#23129)
  web: bump the storybook group across 1 directory with 4 updates (#23246)
  ci: bump actions/checkout from 6.0.3 to 7.0.0 (#23250)
  core: bump structlog from 25.5.0 to 26.1.0 (#23241)
  packages/django-dramatiq-postgres/broker: fix race condition in broker causing completed tasks to be repeated (#23218)
  core, web: update translations (#23237)
  core: bump pytest from 9.0.3 to 9.1.0 (#23238)
  core: bump aws-cdk-lib from 2.258.1 to 2.259.0 (#23240)
  core: bump goauthentik/fips-python from `9b938f7` to `cbec346` in /lifecycle/container (#23242)
  core: bump ruff from 0.15.17 to 0.15.18 (#23243)
  ci: bump int128/docker-manifest-create-action from 2.22.0 to 2.23.0 (#23244)
  core: bump pytest-github-actions-annotate-failures from 0.4.0 to 0.4.1 (#23245)
  ci: bump softprops/action-gh-release from 3.0.0 to 3.0.1 (#23247)
  core: bump astral-sh/uv from 0.11.22 to 0.11.23 in /lifecycle/container (#23248)
  ci: bump taiki-e/install-action from 2.81.11 to 2.82.0 in /.github/actions/setup (#23252)
  web: bump date-fns from 4.1.0 to 4.4.0 in /web (#23253)
  core: bump which from 8.0.3 to 8.0.4 (#23254)
  translate: Updates for project authentik and language ar_AA (#23255)
  packages/django-postgres-cache: fix naive datetime warning (#23033)
  ...
rissson added a commit that referenced this pull request Jun 29, 2026
…r causing completed tasks to be repeated (cherry-pick #23218 to version-2026.2) (#23258)

Co-authored-by: Ryan Pesek <[email protected]>
Co-authored-by: Marc 'risson' Schmitt <[email protected]>
Signed-off-by: Marc 'risson' Schmitt <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:backend backport/version-2026.2 Add this label to PRs to backport changes to version-2026.2 backport/version-2026.5 Add this label to PRs to backport changes to version-2026.5

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants