Skip to content

fix(api): raise gunicorn keep-alive above client idle reuse window - #44839

Merged
fatelei merged 1 commit into
langgenius:mainfrom
fgfsgsgvo:fix/gunicorn-keepalive-ssr-econnreset
Oct 27, 2026
Merged

fatelei merged 1 commit into
langgenius:mainfrom
fgfsgsgvo:fix/gunicorn-keepalive-ssr-econnreset

Conversation

@fgfsgsgvo

Copy link
Copy Markdown
Contributor

Problem

The api container 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 with ECONNRESET.

This is reachable from the web image: server-side rendering calls the api container 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 from GUNICORN_CMD_ARGS="--keep-alive 10".

Solution

Pass --keep-alive and 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=0 still 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 gunicorn
  • docker/.env.example — default for the all-in-one compose path
  • docker/envs/core-services/shared.env.example — default for the api / api_websocket services, which both take their env from x-shared-api-worker-config -> ./envs/core-services/shared.env

api_websocket inherits 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_CLASS defaults to a gevent-family worker.
  • gunicorn/workers/ggevent.py:53-54 — GeventWorker.timeout_ctx() returns gevent.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.keepalive is 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):

gap between requests pooled socket reused
3960ms yes (3/3)
3990ms mixed (1/3)
4020ms no (0/3)

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 PATH in place of gunicorn, so the argument list below is produced by the script itself:

GUNICORN_KEEP_ALIVE resulting argument
unset --keep-alive 10
0 --keep-alive 0
25 --keep-alive 25

The pre-existing arguments (--bind, --workers, --worker-class, --worker-connections, --timeout) are unchanged, and bash -n api/docker/entrypoint.sh passes.

gunicorn side checked against the installed source (26.2.0): Keepalive.cli == ["--keep-alive"], Keepalive.default == 2, and the timeout_ctx / handle references above.

I could not reproduce the ECONNRESET itself 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.example comments if you'd rather keep those files to plain assignments.

Closes #44834

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
@github-actions github-actions Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Oct 26, 2026
@codecov

codecov Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.46%. Comparing base (8ef979e) to head (9e45ce5).
⚠️ Report is 6 commits behind head on main.

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           
Flag Coverage Δ
api 89.27% <ø> (-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.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@fatelei
fatelei added this pull request to the merge queue Oct 27, 2026
Merged via the queue into langgenius:main with commit cd7ed2c Oct 27, 2026
46 checks passed
@laipz8200 laipz8200 mentioned this pull request Nov 3, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Web SSR requests to api intermittently fail with ECONNRESET (500) because gunicorn keep-alive (2s) is shorter than Node fetch's idle socket reuse (4s)

2 participants