Skip to content

packages/django-dramatiq-postgres/broker: close unusable PostgreSQL connections - #24023

Merged
BeryJu merged 2 commits into
goauthentik:mainfrom
HassanRady:feat/fix-postgres-broker-connection-leak
Jul 14, 2026
Merged

BeryJu merged 2 commits into
goauthentik:mainfrom
HassanRady:feat/fix-postgres-broker-connection-leak

Conversation

@HassanRady

Copy link
Copy Markdown
Contributor

Details

What does this PR change?

This PR modifies the locks_connection and listen_connection properties in _PostgresConsumer to explicitly call .close() on their previous, unusable connection wrappers before instantiating new connection instances via connections.create_connection(self.db_alias). It also updates exception handling in the properties to specifically catch DATABASE_ERRORS rather than using blind/broad Exception catching, complying with Python linting best practices.

Why is this change needed?

In django-dramatiq-postgres, _PostgresConsumer instantiates dedicated connection wrappers for handling advisory locks and notifications via connections.create_connection(self.db_alias). Because these connection objects are created dynamically and are not managed in Django's central thread-local/context-local connection registry (connections._connections), they are invisible to standard lifecycle hooks and cleanup operations like connections.close_all().

When a connection fails the .is_usable() check:

The property getter re-instantiates a new connection and overwrites the instance reference.
The old connection wrapper is orphaned in memory without first being closed.
This leaks the client-side TCP sockets and database file descriptors until Python's garbage collection runs.
During database reconnect storms, failovers, or network partitioning, this leaks connections linearly, exhausting PostgreSQL connection limits (OperationalError: FATAL: remaining connection slots are reserved for non-replication superuser connections) and causing a Denial of Service (DoS) for the entire application.
In addition, orphaned connections keep their advisory locks active on the PostgreSQL server, blocking other healthy worker nodes from reclaiming/executing those tasks until TCP/GC timeout occurs.
Explicitly calling .close() on the discarded wrappers frees client-side socket/FD resources instantly and forces PostgreSQL to drop the session and release its locks immediately.

How was this tested?

Validated via codebase review of connection registry mechanics.
Verified file formatting and linting rules pass using black and ruff (ensuring catching DATABASE_ERRORS satisfies BLE001).

Linked issues

#24022


Checklist

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

@HassanRady
HassanRady requested a review from a team as a code owner July 14, 2026 14:45
@BeryJu
BeryJu requested a review from rissson July 14, 2026 14:45
@netlify

netlify Bot commented Jul 14, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for authentik-integrations ready!

Name Link
🔨 Latest commit 35ba22f
🔍 Latest deploy log https://app.netlify.com/projects/authentik-integrations/deploys/6a564c9dbc7ae50008da1a67
😎 Deploy Preview https://deploy-preview-24023--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 Jul 14, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for authentik-docs ready!

Name Link
🔨 Latest commit 35ba22f
🔍 Latest deploy log https://app.netlify.com/projects/authentik-docs/deploys/6a564c9dd0b5e300085c49a6
😎 Deploy Preview https://deploy-preview-24023--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.

@dominic-r dominic-r changed the title fix: close unusable PostgreSQL connection wrappers in dramatiq broker… packages/django-dramatiq-postgres/broker: close unusable PostgreSQL connections Jul 14, 2026
@codecov

codecov Bot commented Jul 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.26%. Comparing base (ba4d3ab) to head (35ba22f).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #24023      +/-   ##
==========================================
- Coverage   93.31%   93.26%   -0.06%     
==========================================
  Files        1045     1045              
  Lines       61401    61401              
  Branches      424      424              
==========================================
- Hits        57296    57265      -31     
- Misses       4105     4136      +31     
Flag Coverage Δ
conformance 36.12% <ø> (+<0.01%) ⬆️
e2e 41.29% <ø> (+<0.01%) ⬆️
integration 32.06% <ø> (-0.49%) ⬇️
rust 0.00% <ø> (ø)
unit 92.40% <ø> (-0.01%) ⬇️
unit-migrate 92.43% <ø> (-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 merged commit 7cc1667 into goauthentik:main Jul 14, 2026
116 checks passed
@BeryJu BeryJu added 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 labels Jul 14, 2026
@authentik-cherry-pick

Copy link
Copy Markdown
Contributor

🍒 Cherry-pick to version-2026.2 created: #24034

@authentik-cherry-pick

Copy link
Copy Markdown
Contributor

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

BeryJu pushed a commit that referenced this pull request Jul 14, 2026
…onnections (cherry-pick #24023 to version-2026.2) (#24034)

packages/django-dramatiq-postgres/broker: close unusable PostgreSQL connections (#24023)

fix: close unusable PostgreSQL connection wrappers in dramatiq broker to prevent resource leaks

Co-authored-by: Hassan Rady <[email protected]>
BeryJu pushed a commit that referenced this pull request Jul 14, 2026
…onnections (cherry-pick #24023 to version-2026.5) (#24035)

packages/django-dramatiq-postgres/broker: close unusable PostgreSQL connections (#24023)

fix: close unusable PostgreSQL connection wrappers in dramatiq broker to prevent resource leaks

Co-authored-by: Hassan Rady <[email protected]>
kensternberg-authentik added a commit that referenced this pull request Jul 15, 2026
* main:
  web/flow: revert locale-driven flow re-request from FlowExecutor (#24050)
  web/bug: harmonize and make routine the display of tasks (part 3: problem children) (#23840)
  root: PostgreSQL direct-endpoint support for LISTEN/NOTIFY and advisory locks (#23013)
  docs/social-logins: Add jumpcloud oidc and saml (#24048)
  core, web: update translations (#24037)
  web: bump @goauthentik/eslint-config from 2.0.0 to 2.0.2 in /web in the goauthentik group across 1 directory (#24007)
  ci: bump taiki-e/install-action from 2.82.9 to 2.83.2 in /.github/actions/setup (#24045)
  website: bump openapi-to-postmanv2 from 6.2.0 to 6.3.0 in /website (#24041)
  core: bump pnpm/pnpm from 11.11.0 to 11.12.0 in /lifecycle/container (#24042)
  core: bump pnpm/pnpm from 11.11.0 to 11.12.0 in /website (#24043)
  web: bump @sentry/browser from 10.63.0 to 10.64.0 in /web in the sentry group across 1 directory (#24044)
  web: bump aws-cdk from 2.1128.1 to 2.1129.0 (#24046)
  stages/authenticator_webauthn: Update FIDO MDS3 & Passkey aaguid blobs (#24039)
  packages/django-dramatiq-postgres/broker: close unusable PostgreSQL connections (#24023)
  api/search: add number and boolean support in AKQL queries on JSON fields (#23418) (#24028)
  enterprise: simplify tests (#18404)
  website/docs: Remove Public Release Docs. (#24031)
  website/docs: sys-mgmt: tweak settings wording (#24029)
  website/docs: cleanup 07-12: update release process guidance (#23970)
  website/integrations: add infiscal (#23996)
kensternberg-authentik added a commit that referenced this pull request Jul 15, 2026
* main: (104 commits)
  web/flow: revert locale-driven flow re-request from FlowExecutor (#24050)
  web/bug: harmonize and make routine the display of tasks (part 3: problem children) (#23840)
  root: PostgreSQL direct-endpoint support for LISTEN/NOTIFY and advisory locks (#23013)
  docs/social-logins: Add jumpcloud oidc and saml (#24048)
  core, web: update translations (#24037)
  web: bump @goauthentik/eslint-config from 2.0.0 to 2.0.2 in /web in the goauthentik group across 1 directory (#24007)
  ci: bump taiki-e/install-action from 2.82.9 to 2.83.2 in /.github/actions/setup (#24045)
  website: bump openapi-to-postmanv2 from 6.2.0 to 6.3.0 in /website (#24041)
  core: bump pnpm/pnpm from 11.11.0 to 11.12.0 in /lifecycle/container (#24042)
  core: bump pnpm/pnpm from 11.11.0 to 11.12.0 in /website (#24043)
  web: bump @sentry/browser from 10.63.0 to 10.64.0 in /web in the sentry group across 1 directory (#24044)
  web: bump aws-cdk from 2.1128.1 to 2.1129.0 (#24046)
  stages/authenticator_webauthn: Update FIDO MDS3 & Passkey aaguid blobs (#24039)
  packages/django-dramatiq-postgres/broker: close unusable PostgreSQL connections (#24023)
  api/search: add number and boolean support in AKQL queries on JSON fields (#23418) (#24028)
  enterprise: simplify tests (#18404)
  website/docs: Remove Public Release Docs. (#24031)
  website/docs: sys-mgmt: tweak settings wording (#24029)
  website/docs: cleanup 07-12: update release process guidance (#23970)
  website/integrations: add infiscal (#23996)
  ...
kensternberg-authentik added a commit that referenced this pull request Jul 15, 2026
…card-2

* web/cleanup/schedule-card-1: (104 commits)
  web/flow: revert locale-driven flow re-request from FlowExecutor (#24050)
  web/bug: harmonize and make routine the display of tasks (part 3: problem children) (#23840)
  root: PostgreSQL direct-endpoint support for LISTEN/NOTIFY and advisory locks (#23013)
  docs/social-logins: Add jumpcloud oidc and saml (#24048)
  core, web: update translations (#24037)
  web: bump @goauthentik/eslint-config from 2.0.0 to 2.0.2 in /web in the goauthentik group across 1 directory (#24007)
  ci: bump taiki-e/install-action from 2.82.9 to 2.83.2 in /.github/actions/setup (#24045)
  website: bump openapi-to-postmanv2 from 6.2.0 to 6.3.0 in /website (#24041)
  core: bump pnpm/pnpm from 11.11.0 to 11.12.0 in /lifecycle/container (#24042)
  core: bump pnpm/pnpm from 11.11.0 to 11.12.0 in /website (#24043)
  web: bump @sentry/browser from 10.63.0 to 10.64.0 in /web in the sentry group across 1 directory (#24044)
  web: bump aws-cdk from 2.1128.1 to 2.1129.0 (#24046)
  stages/authenticator_webauthn: Update FIDO MDS3 & Passkey aaguid blobs (#24039)
  packages/django-dramatiq-postgres/broker: close unusable PostgreSQL connections (#24023)
  api/search: add number and boolean support in AKQL queries on JSON fields (#23418) (#24028)
  enterprise: simplify tests (#18404)
  website/docs: Remove Public Release Docs. (#24031)
  website/docs: sys-mgmt: tweak settings wording (#24029)
  website/docs: cleanup 07-12: update release process guidance (#23970)
  website/integrations: add infiscal (#23996)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

None yet

Development

Successfully merging this pull request may close these issues.

3 participants