Title: http: 'drain' is never emitted for chunked responses with multi-byte strings (v24.22.0 regression) · Issue #66781 · nodejs/node · GitHub
Open Graph Title: http: 'drain' is never emitted for chunked responses with multi-byte strings (v24.22.0 regression) · Issue #66781 · nodejs/node
X Title: http: 'drain' is never emitted for chunked responses with multi-byte strings (v24.22.0 regression) · Issue #66781 · nodejs/node
Description: Version v24.22.0 (also main / v27.0.0-pre; v24.21.0 is not affected) Platform Darwin arm64 (macOS); also linux x64 in Docker (node:24-alpine) Subsystem http What steps will reproduce the bug? Run the following single-file script with nod...
Open Graph Description: Version v24.22.0 (also main / v27.0.0-pre; v24.21.0 is not affected) Platform Darwin arm64 (macOS); also linux x64 in Docker (node:24-alpine) Subsystem http What steps will reproduce the bug? Run t...
X Description: Version v24.22.0 (also main / v27.0.0-pre; v24.21.0 is not affected) Platform Darwin arm64 (macOS); also linux x64 in Docker (node:24-alpine) Subsystem http What steps will reproduce the bug? Run t...
Opengraph URL: https://github.com/nodejs/node/issues/66781
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"http: 'drain' is never emitted for chunked responses with multi-byte strings (v24.22.0 regression)","articleBody":"### Version\n\nv24.22.0 (also main / v27.0.0-pre; v24.21.0 is not affected)\n\n### Platform\n\n```text\nDarwin arm64 (macOS); also linux x64 in Docker (node:24-alpine)\n```\n\n### Subsystem\n\nhttp\n\n### What steps will reproduce the bug?\n\nRun the following single-file script with `node repro.js`. It starts an HTTP server that writes 30k multi-byte string chunks to a chunked `http.ServerResponse`, awaiting `'drain'` whenever `write()` returns `false`. A forked child process reads the response slowly.\n\n```js\n'use strict';\nconst http = require('node:http');\nconst { once } = require('node:events');\nconst { fork } = require('node:child_process');\n\nif (process.argv[2] === 'client') {\n // Slow consumer in a separate process.\n http.get(`http://127.0.0.1:${process.argv[3]}/`, (res) => {\n let bytes = 0;\n res.on('data', (chunk) => {\n bytes += chunk.length;\n const t = performance.now();\n while (performance.now() - t \u003c 2); // simulate per-chunk work\n });\n res.on('end', () => process.send({ done: true, bytes }));\n });\n return;\n}\n\nconst ROWS = 30000;\n// Rows of mixed ASCII + 2-byte UTF-8 characters, ~200-600 bytes each.\nconst rows = Array.from({ length: ROWS }, (_, i) =>\n 'a'.repeat(50 + (i * 37) % 200) + 'ع'.repeat(50 + (i * 53) % 150) + '\\n');\n\nconst server = http.createServer(async (req, res) => {\n for (let i = 0; i \u003c ROWS; i++) {\n if (!res.write(rows[i])) {\n const timer = setTimeout(() => {\n console.log(`${process.version}: STALLED at row ${i}`, {\n writableNeedDrain: res.writableNeedDrain,\n writableLength: res.writableLength,\n socketWritableLength: res.socket.writableLength,\n socketWritableNeedDrain: res.socket.writableNeedDrain,\n });\n process.exit(1);\n }, 3000);\n await once(res, 'drain');\n clearTimeout(timer);\n }\n }\n res.end();\n});\n\nserver.listen(0, () => {\n const child = fork(__filename, ['client', server.address().port]);\n child.on('message', ({ bytes }) => {\n console.log(`${process.version}: OK, client received ${bytes} bytes`);\n child.kill();\n process.exit(0);\n });\n});\n```\n\n### How often does it reproduce? Is there a required condition?\n\nEvery time in our environment (it stalls at the same row on every run).\n\nRequired conditions:\n- a chunked response (no `Content-Length`)\n- **multi-byte string chunks**: the same script with ASCII-only rows of identical *byte* length does not stall\n- a reader slow enough to apply backpressure (a fast reader such as `curl` does not trigger it)\n\n### What is the expected behavior? Why is that the expected behavior?\n\nAfter `res.write()` returns `false`, `'drain'` is emitted once the buffered data has been flushed, as documented for writable streams. v24.21.0 completes:\n\n```\nv24.21.0: OK, client received 11985000 bytes\n```\n\n### What do you see instead?\n\n`'drain'` is never emitted, even though nothing is buffered anymore:\n\n```\nv24.22.0: STALLED at row 2140 {\n writableNeedDrain: true,\n writableLength: 0,\n socketWritableLength: 0,\n socketWritableNeedDrain: false\n}\n```\n\nThe writer waits forever. In production the client (undici) eventually aborts with `UND_ERR_BODY_TIMEOUT` after 300 s.\n\n### Additional information\n\n- This started with v24.22.0, which includes #64987 (`http: coalesce chunked writes during auto-corking`). With that change, `write()` decides its return value from the message-level buffer (`msg.writableLength`), where `kChunkedLength` is measured in **bytes** (`Buffer.byteLength`). Once that reaches the high-water mark it returns `false` and sets `kNeedDrain`.\n- The coalesced chunks are then flushed in the next-tick `uncork()` via `socket.write()` with string data, and `net.Socket` accounts string chunks in **UTF-16 code units**, not bytes:\n\n ```js\n socket.cork(); socket.write('ع'.repeat(1000)); // 2000 bytes on the wire\n socket.writableLength; // 1000 (same on v24.21.0 and v24.22.0)\n ```\n\n So with multi-byte text the socket believes it is below its high-water mark, `socket.write()` returns `true`, the socket never sets `needDrain` and never emits `'drain'`. The other emit path (`uncork()` checking `writableLength === 0` right after the flush) is missed because bytes are still pending at that instant. `kNeedDrain` stays `true` with nothing buffered.\n- Before #64987 the return value came from the socket itself, so the two accountings could not disagree. With ASCII-only data bytes equal code units, which is why only multi-byte bodies are affected.\n- #66039 (`http: count buffered outgoing data in bytes`) looks related but does not cover this: it changes the buffered branch of `_writeRaw()` (`outputData` / `outputSize`), and instrumenting this repro shows that branch is never taken (every write goes straight to the socket).\n- I have a fix with a test that re-checks the drain condition once the flushed data has been written, and will open a PR referencing this issue.","author":{"url":"https://github.com/junyup0503","@type":"Person","name":"junyup0503"},"datePublished":"2026-10-16T06:11:16Z","url":"https://github.com/nodejs/node/issues/66781"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| 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:e0e46dfe-6777-40f0-8565-d593e86b0432 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A792:F5278:765900:9CDCE3:6AED2344 |
| html-safe-nonce | 70484e06bce7160fb61d92d582785b9e977b493a862963ff9e685802c51320a4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNzkyOkY1Mjc4Ojc2NTkwMDo5Q0RDRTM6NkFFRDIzNDQiLCJ2aXNpdG9yX2lkIjoiNTY4MTM2MjEwNzkxOTgzNTk3MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 9e9bb455822b2e7f51df5901d7045644079f1216cc029c5a93d5c89fcf03d1ed |
| hovercard-subject-tag | issue:5879299858 |
| 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/66781/issue_layout |
| twitter:image | https://opengraph.githubassets.com/372567ba60c59b20b5e07275dfe9d3d7367c33179684209fe50e349b0e0f8ba5/nodejs/node/issues/66781 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/372567ba60c59b20b5e07275dfe9d3d7367c33179684209fe50e349b0e0f8ba5/nodejs/node/issues/66781 |
| og:image:alt | Version v24.22.0 (also main / v27.0.0-pre; v24.21.0 is not affected) Platform Darwin arm64 (macOS); also linux x64 in Docker (node:24-alpine) Subsystem http What steps will reproduce the bug? Run t... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | junyup0503 |
| hostname | github.com |
| expected-hostname | github.com |
| None | e3e0f7f63e50c6fe879198a93a0a023a648ecc0a5e9bc3b5e033ac57172237e6 |
| 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 | 707a24b346ac8a82878b9c084efaa38a997660cb |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width