Repository navigation
server, lifecycle: drop idle upstream connections before uvicorn does - #27192
Conversation
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.
✅ Deploy Preview for authentik-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Codecov Report❌ Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
Playwright e2eDownload 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-reportInstallation instructionsInstructions for docker-composeAdd the following block to your 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)sAfterwards, run the upgrade commands from the latest release notes. Instructions for KubernetesAdd the following block to your 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-distrolessAfterwards, run the upgrade commands from the latest release notes. |
# Conflicts: # src/server/mod.rs
BeryJu
left a comment
There was a problem hiding this comment.
tempted to say to set it to 5 just to be safe but also 2 is the default so /shrug
|
todo: 5 python, 4 rust |
|
|
* 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) ...
* 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) ...
… (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]>
Details
What does this PR change?
The server now drops idle connections to uvicorn after 4 seconds instead of 1 minute. gunicorn's
keepaliveis raised from its default of 2 seconds to 5 and set explicitly inlifecycle/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: Closeand 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.8has 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
make all)make docs)