Skip to content

server, lifecycle: drop idle upstream connections before uvicorn does - #27192

Merged
dominic-r merged 3 commits into
mainfrom
dominic/server-upstream-pool-idle-timeout
Oct 29, 2026
Merged

dominic-r merged 3 commits into
mainfrom
dominic/server-upstream-pool-idle-timeout

Conversation

@dominic-r

@dominic-r dominic-r commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

Details

What does this PR change?

The server now drops idle connections to uvicorn after 4 seconds instead of 1 minute. gunicorn's keepalive is raised from its default of 2 seconds to 5 and set explicitly in lifecycle/gunicorn.conf.py, and the two values refer to each other.

Why is this change needed?

uvicorn closes an idle connection after gunicorn's keepalive. The server kept idle connections to uvicorn pooled for a minute, so it could pick up a connection uvicorn was closing at that moment. The request was then reset after it had been sent, and the server answered with the 503 "authentik starting" page. Users see this as an occasional 503 on a request that arrives about 2 seconds after the previous one.

The pool now drops a connection a second before uvicorn would close it. Raising the keep-alive to 5 seconds lets requests up to 4 seconds apart still reuse a connection.

This depends on uvicorn 0.53 or later. Older uvicorn keeps a connection open after a request with Connection: Close and silently discards the next request sent on it (#27181), so a longer keep-alive would leave such a connection in the pool for longer. version-2026.8 has uvicorn 0.54 since #27199, so a backport needs that.

How was this tested?

Added a test with a fake upstream that keeps connections open: a request sent 4.8 seconds after the previous one has to open a new connection. It fails with the old one-minute timeout. A second test checks that back-to-back requests still share a connection.

With the first version of this change (2 second keep-alive, 1 second pool timeout), I ran the 2026.8.5 image limited to 0.2 CPU and sent 255 pairs of requests spaced 1.96 to 2.01 seconds apart. The stock server answered 3 of them with a 503. A server built from 2026.8.5 with this change answered all 255. I did not repeat that run after raising the values to 5 and 4 seconds.

Linked issues

refs #27181


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.

The server kept idle connections to uvicorn pooled for a minute, but
uvicorn closes them after gunicorn's keepalive of 2 seconds. A request
that reused a connection uvicorn was closing got reset after it was
sent, and the client received a 503 "authentik starting".

Drop idle connections a second before uvicorn's keep-alive runs out.
Raising gunicorn's keepalive above the pool timeout would also work,
but uvicorn before 0.53 keeps a connection open after a request with
`Connection: Close` and silently drops the next request on it, so on
2026.8 a longer keepalive would leave that connection in the pool for
a minute instead of 2 seconds.
@dominic-r
dominic-r requested a review from a team as a code owner October 26, 2026 15:39
@dominic-r dominic-r added area:backend backport/version-2026.8 Add this label to PRs to backport changes to version-2026.8 labels Oct 26, 2026
@dominic-r dominic-r self-assigned this Oct 26, 2026
@netlify

netlify Bot commented Oct 26, 2026

Copy link
Copy Markdown

✅ Deploy Preview for authentik-docs ready!

Name Link
🔨 Latest commit 704b214
🔍 Latest deploy log https://app.netlify.com/projects/authentik-docs/deploys/6adf7452d78436654dc48d30
😎 Deploy Preview https://deploy-preview-27192--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 Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 76.19048% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.68%. Comparing base (98c4b28) to head (1881fbe).
⚠️ Report is 30 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/server/mod.rs 76.19% 15 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #27192      +/-   ##
==========================================
+ Coverage   92.48%   92.68%   +0.19%     
==========================================
  Files        1197     1197              
  Lines       81116    81766     +650     
  Branches     4497     4686     +189     
==========================================
+ Hits        75017    75781     +764     
+ Misses       6033     5909     -124     
- Partials       66       76      +10     
Flag Coverage Δ
conformance 31.03% <ø> (-0.12%) ⬇️
e2e 35.87% <ø> (-0.14%) ⬇️
integration 28.51% <ø> (-0.11%) ⬇️
rust 49.31% <76.19%> (+3.33%) ⬆️
unit 94.24% <ø> (+0.11%) ⬆️
unit-migrate 94.24% <ø> (+0.11%) ⬆️

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.

@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Playwright e2e

Download this run's HTML report and traces, then open the report:

gh run download 40909795863 -D playwright-artifacts
npx playwright show-report playwright-artifacts/playwright-report

Installation instructions

Instructions for docker-compose

Add the following block to your .env file:

AUTHENTIK_IMAGE=ghcr.io/goauthentik/dev-server
AUTHENTIK_TAG=gh-1881fbeb3d90afa7eff751121a7bbf8f58ee5222
# Distroless image: AUTHENTIK_TAG=gh-1881fbeb3d90afa7eff751121a7bbf8f58ee5222-distroless
AUTHENTIK_OUTPOSTS__CONTAINER_IMAGE_BASE=ghcr.io/goauthentik/dev-%(type)s:gh-%(build_hash)s

Afterwards, run the upgrade commands from the latest release notes.

Instructions for Kubernetes

Add the following block to your values.yml file:

authentik:
    outposts:
        container_image_base: ghcr.io/goauthentik/dev-%(type)s:gh-%(build_hash)s
global:
    image:
        repository: ghcr.io/goauthentik/dev-server
        tag: gh-1881fbeb3d90afa7eff751121a7bbf8f58ee5222
        # Distroless image: tag: gh-1881fbeb3d90afa7eff751121a7bbf8f58ee5222-distroless

Afterwards, run the upgrade commands from the latest release notes.

@BeryJu BeryJu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

tempted to say to set it to 5 just to be safe but also 2 is the default so /shrug

@dominic-r

Copy link
Copy Markdown
Member Author

todo: 5 python, 4 rust

@dominic-r
dominic-r enabled auto-merge (squash) October 29, 2026 16:16
@dominic-r
dominic-r merged commit 62d919f into main Oct 29, 2026
169 of 170 checks passed
@dominic-r
dominic-r deleted the dominic/server-upstream-pool-idle-timeout branch October 29, 2026 16:50
@authentik-cherry-pick

Copy link
Copy Markdown
Contributor

⚠️ Cherry-pick to version-2026.8 has conflicts: #27353

kensternberg-authentik added a commit that referenced this pull request Oct 30, 2026
* main: (114 commits)
  website/integrations: add planka sso requirement note (#27376)
  core: bump ruff from 0.17.2 to 0.17.3 (#27370)
  core: bump cachetools from 7.2.5 to 7.2.6 (#27372)
  core: bump coverage[toml] from 7.16.2 to 7.17.0 (#27373)
  core: bump django from 5.2.17 to 5.2.18 (#27369)
  core: bump google-api-core from 2.36.0 to v2.43.0 (#27214)
  core: bump boto3 from 1.43.90 to v1.43.119 (#27208)
  core: bump googleapis-common-protos from 1.75.3 to v1.76.0 (#27217)
  core: bump msgpack from 1.2.3 to v1.2.4 (#27235)
  packages/django-dramatiq-postgres: limit task fetching (#27197)
  web/admin: make flow execute links base-path aware (#27314)
  lib: fix capture_logs not thread safe (#27355)
  web/user: keep configure URL query strings when adding next (#27313)
  web/flow: refresh WebAuthn challenge on authentication retry (#26616)
  server, lifecycle: drop idle upstream connections before uvicorn does (#27192)
  outposts: only write changed permissions, add endpoint to rebuild them (#26928)
  flows: fix planner not resolving stages causing extra queries (#27320)
  outposts/proxy: honor X-Forwarded-Host in envoy forward auth (#26888)
  website/docs: Update wording for internal and external users (#27345)
  server: strip hop-by-hop headers before forwarding to the application (#27191)
  ...
kensternberg-authentik added a commit that referenced this pull request Oct 30, 2026
* main: (115 commits)
  lib/tracing: use BatchSpanProcessor (#27387)
  website/integrations: add planka sso requirement note (#27376)
  core: bump ruff from 0.17.2 to 0.17.3 (#27370)
  core: bump cachetools from 7.2.5 to 7.2.6 (#27372)
  core: bump coverage[toml] from 7.16.2 to 7.17.0 (#27373)
  core: bump django from 5.2.17 to 5.2.18 (#27369)
  core: bump google-api-core from 2.36.0 to v2.43.0 (#27214)
  core: bump boto3 from 1.43.90 to v1.43.119 (#27208)
  core: bump googleapis-common-protos from 1.75.3 to v1.76.0 (#27217)
  core: bump msgpack from 1.2.3 to v1.2.4 (#27235)
  packages/django-dramatiq-postgres: limit task fetching (#27197)
  web/admin: make flow execute links base-path aware (#27314)
  lib: fix capture_logs not thread safe (#27355)
  web/user: keep configure URL query strings when adding next (#27313)
  web/flow: refresh WebAuthn challenge on authentication retry (#26616)
  server, lifecycle: drop idle upstream connections before uvicorn does (#27192)
  outposts: only write changed permissions, add endpoint to rebuild them (#26928)
  flows: fix planner not resolving stages causing extra queries (#27320)
  outposts/proxy: honor X-Forwarded-Host in envoy forward auth (#26888)
  website/docs: Update wording for internal and external users (#27345)
  ...
BeryJu pushed a commit that referenced this pull request Nov 6, 2026
… (cherry-pick #27192 to version-2026.8) (#27353)

* Cherry-pick #27192 to version-2026.8 (with conflicts)

This cherry-pick has conflicts that need manual resolution.

Original PR: #27192
Original commit: 62d919f

* server: resolve cherry-pick conflicts for version-2026.8

version-2026.8 has no ServerClient alias and no retry client, so build
only the pooled client through upstream_client and name the client type
directly.

---------

Co-authored-by: Dominic Roy <[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.8 Add this label to PRs to backport changes to version-2026.8

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants