Skip to content

node:http: request body bytes that arrived with the headers are dropped when the 'request' listener destroys the connection #43513

Closed

Description

@robobun

What happens

A request arrives with its body in the same TCP segment as its headers. The 'request' listener reads the request and destroys the connection in the same tick. Node.js still delivers the body bytes that it already has. When the body is complete, Node.js emits 'data' and 'end' and does not abort the request. Bun drops the bytes and aborts the request.

Repro

const http = require("node:http");
const net = require("node:net");
const events = [];
const server = http.createServer((req, res) => {
  req.on("data", chunk => events.push("req data:" + chunk.length));
  req.on("aborted", () => events.push("req aborted"));
  req.on("error", e => events.push("req error:" + e.message));
  req.on("end", () => events.push("req end"));
  req.on("close", () => events.push(`req close (complete=${req.complete})`));
  req.socket.destroy();
});
server.listen(0, "127.0.0.1", () => {
  const c = net.connect(server.address().port, "127.0.0.1", () =>
    c.write("POST / HTTP/1.1\r\nHost: a\r\nContent-Length: 10\r\n\r\nabcdefghij"));
  c.on("error", () => {});
  c.on("close", () => setTimeout(() => { console.log(events.join(", ")); server.close(); }, 200));
});
  • Node v26.3.0: req data:10, req end, req close (complete=true)
  • Bun 1.4.3 (canary 367d939): req aborted, req error:aborted, req close (complete=false)

With a partial body (Content-Length: 10, 3 bytes sent) Node.js emits req data:3 and then aborts the request. Bun aborts the request without the 'data' event.

Cause (from a read of the code)

uWS calls the request handler first and delivers the body bytes of the same read after the handler returns. packages/bun-uws/src/HttpContext.h stops parsing at that point when the socket is closed (the us_socket_is_closed check after the router call). The body bytes that are already in the read buffer never reach NodeHTTPResponse::on_data.

The Node.js parser (parserOnBody, parserOnMessageComplete) continues through the buffer in the same execute() call. The socket 'close' event that aborts open requests comes later.

Related: #38196 keeps the body of the current read for a response that ends early with res.end(). This report is the destroy() side.

Activity

  1. robobun commented on Sep 19, 2026

    @robobun
    CollaboratorAuthor

    Reproduced with Bun 1.4.3 and a debug build of main: bun repro.js 10 prints req aborted, req error:aborted, req close (complete=false).
    Fix in progress: #43602 (covers req.socket.destroy() and res.destroy()).

  2. robobun commented on Sep 19, 2026

    @robobun
    CollaboratorAuthor

    Reproduced with Bun 1.4.3 and a debug build of main: bun repro.js 10 printed req aborted, req error:aborted, req close (complete=false).
    Fixed by #43557 (merged, 5d5f03f). #43602 was superseded by it.

  3. added 2 commits that reference this issue on Sep 21, 2026
    1d6c7c1
    18f02bd
  4. added a commit that references this issue on Sep 26, 2026
    5d5f03f
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions