Skip to content

http: client stops forwarding socket 'drain' to the request once the response is complete #67763

In review

Description

@AZMCode

Version

v20.20.2, v22.23.5, v24.22.3, v26.13.1 (all tested versions)

Platform

Linux 192.168.1.15 5.14.0-687.15.1.el9_8.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Jun 11 16:33:25 UTC 2026 x86_64 x86_64 x86_64 GNU/Linux

(It must be noted this is the Hosts' command. The replication is done inside a Podman Container)

Subsystem

http

What steps will reproduce the bug?

The server answers as soon as it has the request headers, while the client is still writing a body with backpressure:

'use strict';
// node repro.js        -> request never finishes (exit 1)
// node repro.js fix    -> forwards the socket's 'drain' to the request (exit 0)
const http = require('node:http');
const FIX = process.argv[2] === 'fix';
const BODY = 8 * 1024 * 1024;

const srv = http.createServer((q, r) => { r.writeHead(201, { 'Content-Length': '0' }); r.end(); q.resume(); });
srv.listen(0, '127.0.0.1', () => {
  const req = http.request({ port: srv.address().port, method: 'PUT', path: '/', headers: { 'Content-Length': BODY } }, (res) => {
    if (FIX) req.socket.on('drain', () => req.emit('drain'));
    res.resume();
    res.on('end', () => setTimeout(() => {
      console.log(`node ${process.version} fix=${FIX}: writableFinished=${req.writableFinished} writableNeedDrain=${req.writableNeedDrain}`);
      process.exit(req.writableFinished ? 0 : 1);
    }, 1500));
  });
  const chunk = Buffer.alloc(64 * 1024, 7);
  let left = BODY;
  (function write() {
    while (left > 0) {
      const n = Math.min(left, chunk.length); left -= n;
      if (!req.write(chunk.subarray(0, n))) return req.once('drain', write);
    }
    req.end();
  })();
});

How often does it reproduce? Is there a required condition?

Always with the script above. The required condition is that the response is completely parsed while the request is still waiting for 'drain'.

With a server that replies only after reading the whole body, the same state is reachable over HTTPS. There, the completion of the final write is reported one event-loop turn later, and the response can be parsed in that gap. On v26.11.0+ this happens in normal use because of #67762. With 22 concurrent 8 MiB PUTs over one keep-alive HTTPS agent and the client pinned to one CPU, 6 of 110 requests hung on v26.13.1.

What is the expected behavior? Why is that the expected behavior?

node v26.13.1 fix=false: writableFinished=true writableNeedDrain=false

A request that is still writing its body should keep receiving 'drain' from its socket until it has ended, whether or not the response has already arrived. Otherwise any writer that honors backpressure, including stream.pipe(req), can never finish the request.

What do you see instead?

node v26.13.1 fix=false: writableFinished=false writableNeedDrain=true

The request never finishes, writableNeedDrain stays true, and the socket stays in flight, which keeps the process alive.

Additional information

In lib/_http_client.js, socketOnData() calls socket.removeListener('drain', ondrain) as soon as parser.incoming.complete is set, without checking whether the request still has kNeedDrain set. responseOnEnd() does not release the socket in this state, because req.writableFinished is false, so the socket stays referenced indefinitely.

I have a patch, with a test, that keeps ondrain attached until the request has ended and removes it in responseKeepAlive(). I can open a PR.

AI Disclaimer

This report was made almost entirely with an AI. With the exception of this precise disclaimer.
In particular, this was a bug I found in production for our local infrastructure, where CI runs in a self-hosted Forgejo, and a Forgejo Runner, suddenly hung when using forgejo/upload-artifacts@v4 and @v5 with a ~200MB artifact, despite everything working with @V3, and in earlier versions of node.

For now we are reverting to @V3, but I am obviously notifying the maintainers of the forgejo runner so this can be worked around upstream.

The AI agent has made a patch that schedules endReadable() in flowSync() under the same condition as read(), with a test.

I don't post a PR, because the AI contribution guidelines specify that the user proposing a PR must fully understand the changes, which I don't yet fully. Thus I post the patch and other relevant files, so the maintainers can check its accuracy, and maybe implement their own patch/test if needed

I am open to any other data that might be needed to clarify the situation, to the best of my ability.

Found at the same time as issue #67762

0002-http-keep-forwarding-drain-after-response-completes.patch

Activity

  1. AZMCode commented on Nov 25, 2026

    @AZMCode
    Author

    As just mentioned in the other issue's PR (#67769), this issue may only have surfaced in my case due to another change in behavior in streams.

  2. AZMCode commented on Nov 25, 2026

    @AZMCode
    Author

    Here's a table and some more description of what happened there:

    The HTTP bug doesn't need 26.11 at all. With an asynchronous source piped into an HTTPS request against a late-reply server, requests get stranded on every version:
    
    |Node   |No stall|20 ms stall|Stall + shim|
    |-------|--------|-----------|------------|
    |20.20.2|1/10    |8/10       |–           |
    |24.22.3|1/10    |6/10       |0/10        |
    |26.10.0|2/10    |10/10      |–           |
    |26.13.1|0/10    |10/10      |–           |
    
    
    Any stream backed by I/O, such as an fs stream, has this shape. The real defect is ondrain being removed while the request is still waiting for 'drain'. 26.11 only extended it to synchronous sources like Azure's BuffersStream.
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions