Title: http: Agent with keepAlive and no timeout ignores the server's Keep-Alive timeout hint · Issue #66734 · nodejs/node · GitHub
Open Graph Title: http: Agent with keepAlive and no timeout ignores the server's Keep-Alive timeout hint · Issue #66734 · nodejs/node
X Title: http: Agent with keepAlive and no timeout ignores the server's Keep-Alive timeout hint · Issue #66734 · nodejs/node
Description: Version v24.18.0. The same code is on main (lib/_http_agent.js, Agent.prototype.keepSocketAlive). Platform Linux 6.18 x86_64 (WSL2); also seen in GitHub Actions containers on Ubuntu 24.04. Subsystem http What steps will reproduce the bug...
Open Graph Description: Version v24.18.0. The same code is on main (lib/_http_agent.js, Agent.prototype.keepSocketAlive). Platform Linux 6.18 x86_64 (WSL2); also seen in GitHub Actions containers on Ubuntu 24.04. Subsyste...
X Description: Version v24.18.0. The same code is on main (lib/_http_agent.js, Agent.prototype.keepSocketAlive). Platform Linux 6.18 x86_64 (WSL2); also seen in GitHub Actions containers on Ubuntu 24.04. Subsyste...
Opengraph URL: https://github.com/nodejs/node/issues/66734
X: @github
Domain: github.com
IPv4 address: 140.82.112.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"http: Agent with keepAlive and no timeout ignores the server's Keep-Alive timeout hint","articleBody":"### Version\n\nv24.18.0. The same code is on `main` (`lib/_http_agent.js`, `Agent.prototype.keepSocketAlive`).\n\n### Platform\n\nLinux 6.18 x86_64 (WSL2); also seen in GitHub Actions containers on Ubuntu 24.04.\n\n### Subsystem\n\nhttp\n\n### What steps will reproduce the bug?\n\n```js\nconst http = require(\"node:http\");\n\nconst server = http.createServer((req, res) => res.end(\"ok\"));\nserver.keepAliveTimeout = 2000; // sends `Keep-Alive: timeout=2`\n\nfunction get(agent, port) {\n return new Promise((resolve) => {\n const req = http.get({ host: \"127.0.0.1\", port, agent }, (res) => {\n res.resume();\n res.on(\"end\", () => resolve({ hint: res.headers[\"keep-alive\"], reused: req.reusedSocket }));\n });\n });\n}\n\nserver.listen(0, \"127.0.0.1\", async () => {\n const { port } = server.address();\n for (const options of [{ keepAlive: true }, { keepAlive: true, timeout: 60_000 }]) {\n const agent = new http.Agent(options);\n const first = await get(agent, port);\n await new Promise((r) => setTimeout(r, 1500)); // past hint - agentKeepAliveTimeoutBuffer (1000 ms)\n const second = await get(agent, port);\n console.log(JSON.stringify(options), \"hint:\", first.hint, \"-> second request reused the socket:\", second.reused);\n agent.destroy();\n }\n server.closeAllConnections();\n server.close();\n});\n```\n\n### How often does it reproduce? Is there a required condition?\n\nEvery time. The condition is an `http.Agent` with `keepAlive: true` and no `timeout` option.\n\n### What is the expected behavior? Why is that the expected behavior?\n\n```\n{\"keepAlive\":true} hint: timeout=2 -> second request reused the socket: false\n{\"keepAlive\":true,\"timeout\":60000} hint: timeout=2 -> second request reused the socket: false\n```\n\nThe agent should retire a pooled socket at the server's `Keep-Alive: timeout=N` hint minus `agentKeepAliveTimeoutBuffer`, whether or not the agent has a timeout of its own. The comment in `keepSocketAlive` says the hint is applied to \"reduce the likelihood of ECONNRESET errors\". An agent with no timeout keeps sockets the longest, so it is the one that needs it most.\n\n### What do you see instead?\n\n```\n{\"keepAlive\":true} hint: timeout=2 -> second request reused the socket: true\n{\"keepAlive\":true,\"timeout\":60000} hint: timeout=2 -> second request reused the socket: false\n```\n\nWith no agent timeout, the hint is ignored:\n\n```js\nlet agentTimeout = this.options.timeout || 0;\n...\n} else if (serverHintTimeout \u003c agentTimeout) {\n agentTimeout = serverHintTimeout;\n}\n```\n\nWhen `agentTimeout` is `0`, meaning no timeout, `serverHintTimeout \u003c agentTimeout` is never true. So the socket gets no timeout and stays pooled until the server closes it. A request that reuses it at that moment fails with `ECONNRESET`.\n\n### Additional information\n\nWe hit this through Playwright, whose API client is `new http.Agent({ keepAlive: true })` with no timeout. Its requests failed intermittently with `read ECONNRESET` against a Node 24 server (`keepAliveTimeout` 5000). The failing request reused a socket that had been idle 5.9 s, against a hint of 5 s.\n\nA possible fix is to treat `agentTimeout === 0` as \"no limit\":\n\n```js\n} else if (agentTimeout === 0 || serverHintTimeout \u003c agentTimeout) {\n```\n\nRelated, but a different case: #52649 (the race just before the server's timeout).\n","author":{"url":"https://github.com/xantius","@type":"Person","name":"xantius"},"datePublished":"2026-10-14T15:40:30Z","url":"https://github.com/nodejs/node/issues/66734"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:8cc2997f-5218-2ee6-c761-a7266f2cdd5d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DFD4:2356C0:6E5A9B:93507E:6B062CF5 |
| html-safe-nonce | 9c8879bdb40bf25e1d242f065f42cbe9f595cdf92f2b20b644b2e5d86095c3c4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERkQ0OjIzNTZDMDo2RTVBOUI6OTM1MDdFOjZCMDYyQ0Y1IiwidmlzaXRvcl9pZCI6Ijg2Mzg1NjY2ODQ1ODE4MzM5NzMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 5093f8cc0fb46ea3f64cbf369d556d9b7e80d50e67002715a159dbf912d2c992 |
| hovercard-subject-tag | issue:5850795966 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/66734/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bd591cb9d8777a399795d38b54fc4d07f6490218502db17704acaa9f59fddd31/nodejs/node/issues/66734 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bd591cb9d8777a399795d38b54fc4d07f6490218502db17704acaa9f59fddd31/nodejs/node/issues/66734 |
| og:image:alt | Version v24.18.0. The same code is on main (lib/_http_agent.js, Agent.prototype.keepSocketAlive). Platform Linux 6.18 x86_64 (WSL2); also seen in GitHub Actions containers on Ubuntu 24.04. Subsyste... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | xantius |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7f961de5dd5c5d4aca24d33f8b3ffb3fb8b2ef53d0a4f32f01f0a790e512f6d3 |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 494ea0bb1e0feef734a83e2628697647755e8864 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width