Repository navigation
fix(server): keep idle keep-alive sockets open longer than client/proxy idle timeouts - #2813
Open
barbaro-br wants to merge 2 commits into
Open
barbaro-br wants to merge 2 commits into
barbaro-br wants to merge 2 commits into
Conversation
…xy idle timeouts Node closes idle keep-alive sockets after 5 s (+1 s keepAliveTimeoutBuffer on Node 24). Clients that pool connections and do not honor the Keep-Alive: timeout header (Go net/http, Python urllib3, Java, most reverse proxies) can reuse a socket at the moment the server closes it, and the POST fails with ECONNRESET / socket hang up without ever reaching the API. Set server.keepAliveTimeout from SERVER_KEEPALIVE_TIMEOUT (default 65000 ms, above the usual 60 s client/proxy idle timeout) and headersTimeout 1 s above it, so the client side is the one that closes idle sockets.
Contributor
Reviewer's GuideConfigurationAdd a validated
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey - I've found 3 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="src/main.ts" line_range="178" />
<code_context>
+ // `Keep-Alive: timeout` header (Go, Python, Java, most reverse proxies) can reuse a socket right as it closes
+ // and get ECONNRESET / "socket hang up" on a POST that never reached the API. Keep idle sockets open longer
+ // than typical client and proxy idle timeouts (60 s) so the client is the one that closes them.
+ server.keepAliveTimeout = httpServer.KEEPALIVE_TIMEOUT;
+ server.headersTimeout = httpServer.KEEPALIVE_TIMEOUT + 1000;
+
</code_context>
<issue_to_address>
**Idle sockets amplify connection exhaustion**
When an unauthenticated remote client opens many HTTP/1.1 keep-alive connections and leaves them idle, `server.keepAliveTimeout` retains each completed HTTP/1.1 connection for 65 seconds, so enough idle sockets consume process file descriptors and memory and deny service to other clients.
Limit concurrent connections or otherwise enforce per-client connection limits when using the longer default timeout.
Also at `src/config/env.config.ts:482`.
</issue_to_address>
### Comment 2
<location path="src/config/env.config.ts" line_range="482" />
<code_context>
URL: process.env.SERVER_URL,
DISABLE_DOCS: process.env?.SERVER_DISABLE_DOCS === 'true',
DISABLE_MANAGER: process.env?.SERVER_DISABLE_MANAGER === 'true',
+ KEEPALIVE_TIMEOUT: Number.parseInt(process.env?.SERVER_KEEPALIVE_TIMEOUT) || 65000,
},
CORS: {
</code_context>
<issue_to_address>
**Go clients get reset POSTs**
When a pooled Go client reuses a connection around the server's 65-second idle close, before its default transport's 90-second idle timeout, `KEEPALIVE_TIMEOUT` configures `server.keepAliveTimeout` shorter than the Go transport's idle timeout, so Node closes the socket before the client retires it and a reused POST fails with ECONNRESET or "socket hang up".
Configure the server keep-alive timeout to exceed the Go client's idle timeout so the client closes the connection first.
Also at `src/main.ts:178`.
</issue_to_address>
### Comment 3
<location path="src/config/env.config.ts" line_range="482" />
<code_context>
URL: process.env.SERVER_URL,
DISABLE_DOCS: process.env?.SERVER_DISABLE_DOCS === 'true',
DISABLE_MANAGER: process.env?.SERVER_DISABLE_MANAGER === 'true',
+ KEEPALIVE_TIMEOUT: Number.parseInt(process.env?.SERVER_KEEPALIVE_TIMEOUT) || 65000,
},
CORS: {
</code_context>
<issue_to_address>
**Malformed timeout breaks requests**
When `SERVER_KEEPALIVE_TIMEOUT` is set to a negative integer, `Number.parseInt` accepts negative values, and `main.ts` derives `headersTimeout` by adding only 1,000 ms; for a negative setting this leaves some otherwise valid requests with less than a one-second header deadline, so Node rejects slow requests before they reach the API.
Validate the parsed timeout as a positive, supported value and fall back to the default or fail startup when it is invalid.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 3 findings to address first, and keeping idle sockets open for 65 seconds can retain substantially more connections and consume server resources; if that causes a production outage, reverting the timeout cannot undo the outage that already occurred. The new setting can be reverted to restore the prior socket behavior.
Blocking findings: src/main.ts:178, src/config/env.config.ts:482, src/config/env.config.ts:482
…_TIMEOUT - 95 s exceeds Go net/http's default IdleConnTimeout (90 s), so pooled Go clients retire idle sockets before the server closes them (65 s did not). - Accept only positive integers; 0 (no timeout), negatives and garbage fall back to the default instead of producing a broken headersTimeout.
This branch has not been deployed
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.
📋 Description
Node's HTTP server closes idle keep-alive sockets after 5 s (
server.keepAliveTimeout; on Node 24 there is also a 1 skeepAliveTimeoutBuffer, so ~6 s in practice). Evolution never changes it.Clients that pool connections but don't honor the
Keep-Alive: timeout=5header (Gonet/http, Pythonurllib3/requests, Java, most reverse proxies) keep idle sockets for 60–90 s. A bulk sender that pauses a few seconds between messages will sometimes reuse a socket at the exact moment Evolution closes it. The request is written, the socket is reset, and the call fails withECONNRESET/socket hang up. For aPOST /message/sendMediathe client can't safely retry, because it can't tell whether the message was sent.We hit this in production (v2.3.8, Node 24.22, client in Go, both in the same Docker network):
read: connection reset by peeron onesendMediain a ~3000-message day.This PR:
SERVER_KEEPALIVE_TIMEOUT(ms, default 95000, above Gonet/http's 90 s idle timeout and the common 60 s proxy/load balancer idle timeout) and setsserver.keepAliveTimeoutfrom it. Only positive integers are accepted; anything else falls back to the default;server.headersTimeout1 s above it (Node requiresheadersTimeout > keepAliveTimeout);.env.example.With the server keeping idle sockets longer than the client, the client closes them first, so the race goes away. The cost is idle sockets staying open up to 95 s instead of ~6 s. They hold no request state, and
headersTimeout/requestTimeoutstill bound slow or incomplete requests. For comparison, nginx keeps idle client connections for 75 s by default.🔗 Related Issue
No existing issue that I could find. Happy to open one if you prefer.
🧪 Type of Change
🧪 Testing
Reproduction with a plain Node 24 server (same defaults as Evolution), a Go client with the default
http.Client, and a TCP relay adding 30 ms one-way latency (as between containers/hosts). The client sends 40 POSTs, each after a pause of 5.95–6.04 s on a pooled connection:keepAliveTimeout5 s + 1 s buffer)connection reset by peer, 18 okkeepAliveTimeout = 65000Reproduction scripts
server.mjsrelay/main.go(adds latency)goclient/main.gonpx tsc --noEmit: 0 errors (afterprisma generatefor postgresql).npx eslint src/main.ts src/config/env.config.ts: clean.Manual testing completed
Functionality verified in development environment
No breaking changes introduced
Tested with different connection types (if applicable)
✅ Checklist
📝 Additional Notes
If you'd rather not change the default, I can keep Node's 5 s as the default and leave
SERVER_KEEPALIVE_TIMEOUTas opt-in. I picked 95 s so the server outlives the idle timeout of the default Go client (90 s) and of common proxies and load balancers (60 s, e.g. AWS ALB), so the client side always closes idle sockets first.Updated after the Sourcery review: the default went from 65 s to 95 s (Go's 90 s idle timeout), and the value is now validated.
Summary by Sourcery
Keep idle HTTP connections open longer than typical client and proxy idle timeouts to prevent resets during connection reuse.
Bug Fixes:
Enhancements:
SERVER_KEEPALIVE_TIMEOUTin milliseconds, defaulting to 95,000; invalid or non-positive values use the default.Documentation:
SERVER_KEEPALIVE_TIMEOUTin.env.example.