Skip to content

A body failing before its first chunk is sent as a complete, empty 200 when overrideGlobalObjects is false #413

Closed

Description

@joanniclaborde

Hello!

With overrideGlobalObjects: false, if a handler's Response body errors before its first chunk, the client gets 200 with Content-Length: 0 and a complete, empty body. With the default (true), the connection is cut, as I'd expect.

We run the adapter in a gateway, where this turns a failing upstream into an empty success that the client can't tell from a real one.

It looks like responseViaResponseObject's early read catches the error from readWithoutBlocking and treats it as the end of the body.
Possibly related to #272, which is about the same early-read code, though a different case.

Reproduced on @hono/node-server 2.1.5, Node 26:

import { serve } from "@hono/node-server";
import { request } from "node:http";

const server = serve(
  {
    fetch: () =>
      new Response(new ReadableStream({ start: (controller) => controller.error(new Error("failed")) })),
    overrideGlobalObjects: false,
    port: 0,
  },
  ({ port }) => {
    request({ port }, (response) => {
      let body = "";
      response.on("data", (chunk) => (body += chunk));
      // Prints: 200 0 true ""
      response.on("end", () => {
        console.log(response.statusCode, response.headers["content-length"], response.complete, JSON.stringify(body));
        server.close();
      });
      response.on("error", (error) => {
        console.log("cut:", error.message);
        server.close();
      });
    }).end();
  },
);

Expected: the connection cut, or a 500 since no head was sent yet; never a complete answer.

Activity

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