Repository navigation
fix(api): raise gunicorn keep-alive above client idle reuse window - #44839
Merged
fatelei merged 1 commit intoOct 27, 2026
Merged
Conversation
The api container starts gunicorn without --keep-alive, so the gevent worker closes idle connections after the 2s default. Node's global fetch (undici) keeps a pooled socket idle for 4s before dropping it, so any request that lands in that window reuses a connection the server has already closed and fails with ECONNRESET. This is reachable from the web image, where server-side rendering calls the api container over the compose network and surfaces the failure as a 500. Measured on this machine (Node v24.18.0), a socket is reused at a 3960ms gap and dropped at 4020ms, which confirms the 4s client window and puts the race in the (2s, 4s) interval. Pass --keep-alive and default it to 10s so the server outlives the client window. Setting GUNICORN_KEEP_ALIVE=0 still disables keep-alive for anyone who wants the old single-request behaviour. Closes langgenius#44834
fgfsgsgvo
requested review from
QuantumGhost,
crazywoola and
laipz8200
as code owners
October 26, 2026 15:55
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #44839 +/- ##
=======================================
Coverage 89.46% 89.46%
=======================================
Files 5997 5998 +1
Lines 376020 376042 +22
Branches 76178 76180 +2
=======================================
+ Hits 336398 336420 +22
Misses 33598 33598
Partials 6024 6024
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
fatelei
approved these changes
Oct 27, 2026
1 task done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
apicontainer starts gunicorn without--keep-alive, so gunicorn falls back to its 2s default and the gevent worker closes an idle keep-alive connection after two seconds.Node's global
fetch(undici) does the opposite: it keeps a pooled socket idle for about 4s before dropping it. Between those two numbers the client and the server disagree about whether a connection is still usable. A request sent in that window is written to a socket the server has already closed, and the call fails withECONNRESET.This is reachable from the
webimage: server-side rendering calls theapicontainer over the compose network, and the failure surfaces to the user as a 500. #44834 has the container-level reproduction and the before/after numbers fromGUNICORN_CMD_ARGS="--keep-alive 10".Solution
Pass
--keep-aliveand default it to 10s, so the server's idle timeout is always longer than the client's reuse window and the disagreement cannot happen.--keep-alive ${GUNICORN_KEEP_ALIVE:-10}GUNICORN_KEEP_ALIVE=0still disables keep-alive for anyone who wants the previous single-request behaviour, and any other value is passed through unchanged.Three files, because the value has to reach the container and be discoverable by operators:
api/docker/entrypoint.sh— pass the flag to gunicorndocker/.env.example— default for the all-in-one compose pathdocker/envs/core-services/shared.env.example— default for theapi/api_websocketservices, which both take their env fromx-shared-api-worker-config->./envs/core-services/shared.envapi_websocketinherits the same env file, so it picks up the new default too; its 360s timeout and 1000 worker connections are untouched.Why the gevent worker honours it
Worth stating, because a sync worker would ignore the flag:
SERVER_WORKER_CLASSdefaults to agevent-family worker.gunicorn/workers/ggevent.py:53-54—GeventWorker.timeout_ctx()returnsgevent.Timeout(self.cfg.keepalive, False).gunicorn/workers/base_async.py:74— with keepalive set,AsyncWorker.handle()loops on the same socket instead of serving one request and closing.So
cfg.keepaliveis exactly the idle window in question.Verification
Client window measured, not assumed. A local TCP server with no server-side timeout, swept against Node's global
fetch(Node v24.18.0):So the client keeps the socket until ~4s, which is what puts the race in the (2s, 4s) interval.
The real entrypoint was executed with a stub on
PATHin place ofgunicorn, so the argument list below is produced by the script itself:GUNICORN_KEEP_ALIVE--keep-alive 100--keep-alive 025--keep-alive 25The pre-existing arguments (
--bind,--workers,--worker-class,--worker-connections,--timeout) are unchanged, andbash -n api/docker/entrypoint.shpasses.gunicorn side checked against the installed source (26.2.0):
Keepalive.cli == ["--keep-alive"],Keepalive.default == 2, and thetimeout_ctx/handlereferences above.I could not reproduce the
ECONNRESETitself on my machine. Reproducing it needs the two sides inside the same container network with the real worker, and my local TCP stand-in only managed it intermittently even with the server closing at the exact gap — which matches the issue author seeing roughly one failure per 186 requests rather than a deterministic failure. The container-level numbers in #44834 are the reliable reproduction; what I verified locally is the mechanism (the 4s client window) and that the fix reaches gunicorn with the intended value.Note
Happy to switch the default or the variable name if you'd prefer something else, and to drop the
.env.examplecomments if you'd rather keep those files to plain assignments.Closes #44834