From 941ce0df99b06b6186e640306df022d0b3cdfaed Mon Sep 17 00:00:00 2001 From: barbaro-br <108009971+barbaro-br@users.noreply.github.com> Date: Wed, 2 Dec 2026 06:11:23 -0300 Subject: [PATCH 1/2] fix(server): keep idle keep-alive sockets open longer than client/proxy 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. --- .env.example | 3 +++ src/config/env.config.ts | 2 ++ src/main.ts | 7 +++++++ 3 files changed, 12 insertions(+) diff --git a/.env.example b/.env.example index fa7d9e7bd3..19e891bf58 100644 --- a/.env.example +++ b/.env.example @@ -3,6 +3,9 @@ SERVER_TYPE=http SERVER_PORT=8080 # Server URL - Set your application url SERVER_URL=http://localhost:8080 +# How long (ms) idle keep-alive connections stay open. Keep it above the idle timeout of +# your clients and reverse proxy (usually 60 s) to avoid ECONNRESET on reused connections. +SERVER_KEEPALIVE_TIMEOUT=65000 SSL_CONF_PRIVKEY=/path/to/cert.key SSL_CONF_FULLCHAIN=/path/to/cert.crt diff --git a/src/config/env.config.ts b/src/config/env.config.ts index 1b17a83b53..69b5d09a5a 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -10,6 +10,7 @@ export type HttpServer = { URL: string; DISABLE_DOCS: boolean; DISABLE_MANAGER: boolean; + KEEPALIVE_TIMEOUT: number; }; export type HttpMethods = 'POST' | 'GET' | 'PUT' | 'DELETE'; @@ -478,6 +479,7 @@ export class ConfigService { 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: { ORIGIN: process.env.CORS_ORIGIN?.split(',') || ['*'], diff --git a/src/main.ts b/src/main.ts index f9884ba34b..f8fd982f01 100644 --- a/src/main.ts +++ b/src/main.ts @@ -171,6 +171,13 @@ async function bootstrap() { server = ServerUP[httpServer.TYPE]; } + // Node closes idle keep-alive sockets after 5 s (+1 s buffer). Clients that pool connections and ignore the + // `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; + eventManager.init(server); const sentryConfig = configService.get('SENTRY'); From fe4581218fa9996b61aea1bcc75547d9eefd1f14 Mon Sep 17 00:00:00 2001 From: barbaro-br <108009971+barbaro-br@users.noreply.github.com> Date: Wed, 2 Dec 2026 14:18:30 -0300 Subject: [PATCH 2/2] fix(server): default keep-alive to 95 s and validate SERVER_KEEPALIVE_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. --- .env.example | 5 +++-- src/config/env.config.ts | 6 +++++- src/main.ts | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index 19e891bf58..b3ea205335 100644 --- a/.env.example +++ b/.env.example @@ -4,8 +4,9 @@ SERVER_PORT=8080 # Server URL - Set your application url SERVER_URL=http://localhost:8080 # How long (ms) idle keep-alive connections stay open. Keep it above the idle timeout of -# your clients and reverse proxy (usually 60 s) to avoid ECONNRESET on reused connections. -SERVER_KEEPALIVE_TIMEOUT=65000 +# your clients and reverse proxy (Go net/http: 90 s; most proxies: 60 s) to avoid ECONNRESET +# on reused connections. Must be a positive integer; invalid values fall back to the default. +SERVER_KEEPALIVE_TIMEOUT=95000 SSL_CONF_PRIVKEY=/path/to/cert.key SSL_CONF_FULLCHAIN=/path/to/cert.crt diff --git a/src/config/env.config.ts b/src/config/env.config.ts index 69b5d09a5a..abc19ed328 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -471,6 +471,10 @@ export class ConfigService { } private envProcess(): Env { + // Must exceed the idle timeout of pooled clients (Go net/http: 90 s) so the client closes idle sockets first. + // Only positive integers are accepted; 0 would disable the timeout and negatives break headersTimeout. + const keepAliveTimeout = Number.parseInt(process.env?.SERVER_KEEPALIVE_TIMEOUT); + return { SERVER: { NAME: process.env?.SERVER_NAME || 'evolution', @@ -479,7 +483,7 @@ export class ConfigService { 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, + KEEPALIVE_TIMEOUT: Number.isInteger(keepAliveTimeout) && keepAliveTimeout > 0 ? keepAliveTimeout : 95000, }, CORS: { ORIGIN: process.env.CORS_ORIGIN?.split(',') || ['*'], diff --git a/src/main.ts b/src/main.ts index f8fd982f01..e3c7f69a7c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -174,7 +174,7 @@ async function bootstrap() { // Node closes idle keep-alive sockets after 5 s (+1 s buffer). Clients that pool connections and ignore the // `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. + // than typical client and proxy idle timeouts (Go: 90 s, load balancers/proxies: 60 s) so the client closes them. server.keepAliveTimeout = httpServer.KEEPALIVE_TIMEOUT; server.headersTimeout = httpServer.KEEPALIVE_TIMEOUT + 1000;