Skip to content

vite dev: the client-disconnect check from #4708 misses HTTP/2 (server.https) #4772

Closed

Description

@dennev

Environment

  • Nitro: main at cef5305 (also 3.0.260903-beta)
  • Vite: 8.3.3
  • Node.js: 26.10.0 on Windows 11, and 24.21.0 on Linux
  • Integration: the nitro/vite dev server, running HTTP/2 because server.https is set

Reproduction

You don't need a certificate to reproduce it. Put cleartext HTTP/2 in front of Vite's middleware mode, and the middlewares get the same Http2ServerRequest and Http2ServerResponse objects that Vite's own server.https server gives them.

A project with vite.config.ts using plugins: [nitro({ serverDir: "./" })] and routes/upload.post.ts:

import { defineHandler } from "nitro";

export default defineHandler(async (event) => `received ${(await event.req.text()).length}`);

repro.mjs, run with node repro.mjs in that project:

import http2 from "node:http2";
import { createServer } from "vite";

let onRequest;
const vite = await createServer({ server: { middlewareMode: true, ws: false } });
const h2 = http2.createServer((req, res) => {
  onRequest?.();
  vite.middlewares(req, res);
});
await new Promise((resolve) => h2.listen(0, resolve));
const session = http2.connect(`http://localhost:${h2.address().port}`);

const post = (body) =>
  new Promise((resolve) => {
    const stream = session.request({ ":method": "POST", ":path": "/upload" });
    let text = "";
    stream.setEncoding("utf8");
    stream.on("data", (chunk) => (text += chunk));
    stream.on("end", () => resolve(text));
    stream.end(body);
  });

console.log("complete upload:", await post("hello"));

// Start an upload and cancel it as soon as the server has it, as a browser does
// when the page navigates away while the request is pending.
const stream = session.request({ ":method": "POST", ":path": "/upload" }, { endStream: false });
stream.on("error", () => {});
onRequest = () => stream.close(http2.constants.NGHTTP2_CANCEL);
stream.write("partial body");

setTimeout(async () => {
  session.close();
  h2.close();
  await vite.close();
}, 1000);

On main the script prints the error below every time (3 of 3 runs). With the check suggested below it prints only the first line (0 of 2 runs).

Describe the bug

#4708 (for #4697) skips next(error) when nodeRes.destroyed is set. When server.https is set, Vite creates its server with node:http2's createSecureServer, and browsers then use HTTP/2. Node's HTTP/2 compatibility Http2ServerResponse has no destroyed property, so the check stays undefined after the client has gone.

Suppose a browser cancels a POST before Nitro has read its body, for example because the page navigated away while the request was pending. The abort error still reaches Vite's error middleware, which logs Internal server error: This operation was aborted and sends the error overlay to every open tab. Over HTTP/1.1 this no longer happens since #4708.

This check covers both protocols:

"stream" in res ? res.stream.destroyed : res.destroyed

I have this change ready with a regression test under test/vite/, covering HTTP/1.1 and HTTP/2. Would you like a PR for it? I first mentioned the gap in a comment on #4708: #4708 (comment)

Additional context

We found this through intermittent end-to-end test failures. A page navigated away while a server-function POST was pending, and the overlay broadcast reached the next test's page.

Logs

complete upload: received 5
[vite] Internal server error: This operation was aborted
      at new DOMException (node:internal/per_context/domexception:79:18)
      at AbortController.abort (node:internal/abort_controller:579:18)
      at abort (.../srvx/dist/adapters/node.mjs:399:51)
      at onClose (.../srvx/dist/adapters/node.mjs:404:36)
      at get _abortController (.../srvx/dist/adapters/node.mjs:407:46)
      at #bodyError (.../srvx/dist/adapters/node.mjs:447:24)
      at get body (.../srvx/dist/adapters/node.mjs:430:81)
      at toInit (.../httpxy/dist/index.mjs:670:14)
      at proxyFetch (.../httpxy/dist/index.mjs:605:7)
      at NodeWorkerEnvRunner.fetch (.../env-runner/dist/_chunks/common-base-runner.mjs:607:10)

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