Title: http2: a destroyed `Http2Session` keeps its idle timer armed, so every closed session stays in memory until the server timeout fires · Issue #66885 · nodejs/node · GitHub
Open Graph Title: http2: a destroyed `Http2Session` keeps its idle timer armed, so every closed session stays in memory until the server timeout fires · Issue #66885 · nodejs/node
X Title: http2: a destroyed `Http2Session` keeps its idle timer armed, so every closed session stays in memory until the server timeout fires · Issue #66885 · nodejs/node
Description: Version v22.23.1, v24.21.0, v26.11.1 (and main: the code path below is unchanged) Platform Darwin 24.6.0 arm64 (macOS); the faulty logic is in lib/, not platform-specific Subsystem http2 What steps will reproduce the bug? // Run with: no...
Open Graph Description: Version v22.23.1, v24.21.0, v26.11.1 (and main: the code path below is unchanged) Platform Darwin 24.6.0 arm64 (macOS); the faulty logic is in lib/, not platform-specific Subsystem http2 What steps...
X Description: Version v22.23.1, v24.21.0, v26.11.1 (and main: the code path below is unchanged) Platform Darwin 24.6.0 arm64 (macOS); the faulty logic is in lib/, not platform-specific Subsystem http2 What steps...
Opengraph URL: https://github.com/nodejs/node/issues/66885
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"http2: a destroyed `Http2Session` keeps its idle timer armed, so every closed session stays in memory until the server timeout fires","articleBody":"### Version\n\nv22.23.1, v24.21.0, v26.11.1 (and `main`: the code path below is unchanged)\n\n### Platform\n\n```text\nDarwin 24.6.0 arm64 (macOS); the faulty logic is in lib/, not platform-specific\n```\n\n### Subsystem\n\nhttp2\n\n### What steps will reproduce the bug?\n\n```js\n// Run with: node --expose-gc repro.mjs\nimport http2 from \"node:http2\";\n\nconst TIMEOUT = 30_000;\nconst server = http2.createServer();\nserver.setTimeout(TIMEOUT, () => {});\nserver.on(\"request\", (_req, res) => res.end(\"ok\"));\nlet last;\nserver.on(\"session\", (s) => (last = s));\nawait new Promise((r) => server.listen(0, \"127.0.0.1\", r));\nconst url = `http://127.0.0.1:${server.address().port}`;\n\nasync function oneSession() {\n const client = http2.connect(url);\n await new Promise((r) => client.request({ \":path\": \"/\" }).resume().on(\"end\", r).end());\n await new Promise((r) => client.close(r));\n await new Promise((r) => setImmediate(r));\n}\n\n// 1. The session is destroyed, but its timeout was never cleared.\nawait oneSession();\nconsole.log(\"destroyed:\", last.destroyed, \"| timeout still armed:\", last.timeout); // true | 30000 (expected 0)\n\n// 2. Each closed session stays reachable until the timer fires.\nconst heap = () => (globalThis.gc(), globalThis.gc(), process.memoryUsage().heapUsed);\nfor (let i = 0; i \u003c 500; i++) await oneSession(); // warm up\nconst before = heap();\nfor (let i = 0; i \u003c 2000; i++) await oneSession();\nconsole.log(\"retained per closed session:\", ((heap() - before) / 2000 / 1024).toFixed(2), \"KiB\");\nserver.close();\n```\n\n\n### How often does it reproduce? Is there a required condition?\n\nAlways, as soon as the server has a session timeout (`server.setTimeout(ms)`, or `timeout` in the server options). Same result over TLS (`createSecureServer`).\n\n### What is the expected behavior? Why is that the expected behavior?\n\n```text\ndestroyed: true | timeout still armed: 0\nretained per closed session: ~0.00 KiB\n```\n\n`closeSession()` explicitly tries to do this — `// Clear timeout and remove timeout listeners.` — so a destroyed session should no longer be referenced by a timer.\n\n### What do you see instead?\n\n```text\nv26.11.1 destroyed: true | timeout still armed: 30000 retained per closed session: 1.77 KiB\nv24.21.0 destroyed: true | timeout still armed: 30000 retained per closed session: 1.80 KiB\nv22.23.1 destroyed: true | timeout still armed: 30000 retained per closed session: 2.47 KiB\n```\n\nThe retention is bounded by the timeout: with a 1 s timeout and a 1.5 s wait before measuring, it drops to 0. But under a steady flow of new connections it amounts to *new sessions/s × timeout × ~2 KiB* held at all times (e.g. 1,000 sessions/s with a 30 s timeout ≈ 60 MB).\n\n### Additional information\n\nThe cause is an ordering issue in `closeSession()` (`lib/internal/http2/core.js`):\n\n```js\nfunction closeSession(session, code, error) {\n ...\n state.flags |= SESSION_FLAGS_DESTROYED; // (1) the session is now `destroyed`\n state.destroyCode = code;\n\n // Clear timeout and remove timeout listeners.\n session.setTimeout(0); // (2) no-op\n session.removeAllListeners('timeout');\n```\n\n`Http2Session.prototype.setTimeout` is `setStreamTimeout` (`lib/internal/stream_base_commons.js`), whose first statement is:\n\n```js\nfunction setStreamTimeout(msecs, callback) {\n if (this.destroyed)\n return this;\n```\n\nSo (2) returns immediately: `clearTimeout(this[kTimeout])` is never reached, and the `Timeout` (whose callback is bound to the session) keeps the whole session — socket, handle, buffers — reachable until it fires. `sessionOnTimeout` then returns early on `this.destroyed`, so the only visible effect is the memory.\n\n`Http2Stream` is not affected: after a stream closes, `stream.timeout` is `0` as expected.\n\nA possible fix is to clear the timer before setting `SESSION_FLAGS_DESTROYED` (or to call `clearTimeout(session[kTimeout])` directly in `closeSession`).\n\nUserland workaround: clear the timer while the session is still alive, e.g. on GOAWAY:\n\n```js\nserver.on(\"session\", (session) => {\n session.once(\"goaway\", () => session.setTimeout(0));\n});\n```\n\nIt covers graceful closes (clients send GOAWAY on `close()` and `destroy()`), but not a TCP connection dropped without GOAWAY.\n","author":{"url":"https://github.com/ccamensuli","@type":"Person","name":"ccamensuli"},"datePublished":"2026-10-20T22:24:07Z","url":"https://github.com/nodejs/node/issues/66885"}
| 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:14c2f54e-4431-f197-180c-7fd7514ec55d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8E8E:30DACD:223A99:2DB35A:6AFBE268 |
| html-safe-nonce | 30f94f70ed004c394b1f1e86c5ca9b3a67a43233516ef686a88a92649e6295c5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RThFOjMwREFDRDoyMjNBOTk6MkRCMzVBOjZBRkJFMjY4IiwidmlzaXRvcl9pZCI6IjU4OTQ5OTEyODA1MjcwMzg0OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 7c8cc05874ff927c3fd2e10347d22eb34023ced2cbb1666b645e7e4040f8bf63 |
| hovercard-subject-tag | issue:5950639276 |
| 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/66885/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5981922eecd0ad7d2c98507c35a66903bcb5535f6fa03e6dcd9ece4455f3dd5d/nodejs/node/issues/66885 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5981922eecd0ad7d2c98507c35a66903bcb5535f6fa03e6dcd9ece4455f3dd5d/nodejs/node/issues/66885 |
| og:image:alt | Version v22.23.1, v24.21.0, v26.11.1 (and main: the code path below is unchanged) Platform Darwin 24.6.0 arm64 (macOS); the faulty logic is in lib/, not platform-specific Subsystem http2 What steps... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ccamensuli |
| hostname | github.com |
| expected-hostname | github.com |
| None | 744d4381b1c4dbd6d8736c7d7e5bbe7dc11217584e457a3555020c631764e8ec |
| 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 | df859c48a4c8b4c0009c19bb4e82101aacb4b998 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width