Skip to content

vite dev: a client that aborts a POST mid-body triggers "Internal server error: aborted" and the Vite error overlay #4697

Closed

Description

@marinushv

Environment

  • nitro 3.0.260903-beta (current latest), and nitro-nightly 3.0.1-20260928-103646-f72e2ba6, which is built from f72e2ba, the latest commit to touch src/build/vite/dev.ts
  • vite 8.3.2
  • Node.js v24.21.0, Linux x86_64
  • Integration: nitro/vite with the default Node worker runner

Reproduction

package.json:

{
  "type": "module",
  "devDependencies": { "nitro": "3.0.260903-beta", "vite": "8.3.2" }
}

vite.config.mjs:

import { defineConfig } from "vite";
import { nitro } from "nitro/vite";

export default defineConfig({ plugins: [nitro({ serverDir: "./server" })] });

server/routes/echo.post.ts:

import { defineHandler } from "nitro";

export default defineHandler(async (event) => {
  const body = await event.req.text();
  return { received: body.length };
});

abort.mjs sends a POST, writes part of the declared body, then closes the connection:

import http from "node:http";

const req = http.request("http://localhost:5173/echo", {
  method: "POST",
  headers: { "content-length": 100_000 },
});
req.on("error", () => {});
req.write("x".repeat(1000));
setTimeout(() => req.destroy(), 200);

Steps:

  1. Run npm install.
  2. Run npx vite dev --port 5173.
  3. In a second terminal, run node abort.mjs.

A browser produces the same request when it cancels a POST before the server has received the whole body. One common case is a React effect that aborts its fetch in the cleanup function, because StrictMode mounts, cleans up, and mounts every component again in development.

Expected

The client went away, so nobody is waiting for a response. The dev server stays quiet, or at most logs a debug line.

Actual

Vite logs an internal server error:

[vite] Internal server error: aborted
      at abortIncoming (node:_http_server:921:17)
      at socketOnClose (node:_http_server:914:3)
      at Socket.emit (node:events:526:24)
      at TCP.<anonymous> (node:net:362:12)

Vite also sends this HMR message to every connected page, which opens the error overlay in each open tab (stack trimmed):

{"type":"error","err":{"message":"aborted","stack":"    at abortIncoming (node:_http_server:921:17)\n    at socketOnClose (node:_http_server:914:3) ...","frame":""}}

Both versions listed above behave the same way.

Root cause

nitroDevMiddleware in src/build/vite/dev.ts (main, L322-L323) passes every rejection from ctx.devApp.fetch(req) and nitroEnv.dispatchFetch(req) to Vite:

    } catch (error) {
      return next(error);
    }

The request body is the Node IncomingMessage, wrapped by srvx's NodeRequest. When the socket closes before the full body has arrived, Node errors that stream with Error: aborted (code: 'ECONNRESET'). The proxied fetch to the dev worker rejects with that error, and the middleware forwards it to next(error). Vite's error middleware then logs Internal server error and broadcasts the overlay message.

Suggested fix

Skip next(error) when the response socket is already gone:

    } catch (error) {
      if (nodeRes.destroyed) return;
      return next(error);
    }

If nodeRes is destroyed, no response can reach the client. Reporting the error only produces a misleading overlay.

nodeReq.destroyed looks like an equivalent check, but it is not. Node sets nodeReq.destroyed to true as soon as the request body has been fully read, even though the client is still connected. A plain node:http server on Node v24.21.0 shows this:

[server] body read (5 bytes): req.destroyed=true res.destroyed=false socket.destroyed=false
[server] 50ms later, client still connected: req.destroyed=true res.destroyed=false
[client] status=200 body=ok

With a nodeReq.destroyed guard, any failure that happens after the body was read is swallowed. next is never called, and the client waits forever. A route that reads its body and then makes the worker connection fail shows the difference:

// server/routes/crash.post.ts
import { defineHandler } from "nitro";

export default defineHandler(async (event) => {
  await event.req.text();
  event.runtime!.node!.req.socket.destroy();
  return new Promise(() => {});
});

Results on both versions, with a full 1000-byte POST to /crash and an aborted POST to /echo:

Guard in catch Aborted POST /crash, client connected
none (current) Internal server error: aborted and overlay HTTP 500, Internal server error: socket hang up and overlay
if (nodeRes.destroyed) return; no log, no overlay HTTP 500, Internal server error: socket hang up and overlay
if (nodeReq.destroyed) return; no log, no overlay no response after 5 s, no log, no overlay

Filtering on error.code === "ECONNRESET" would also be wrong. A dead worker connection (for example #4693) rejects with ECONNRESET too, and that error should still reach the developer.

Activity

  1. AdarshYadav9 commented on Oct 3, 2026

    @AdarshYadav9
    Contributor

    I would Like to work on this issue can you assign me this issue

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