Skip to content

fix(vite): suppress errors after client disconnect - #4708

Merged
pi0 merged 1 commit into
nitrojs:mainfrom
AdarshYadav9:fix/vite-aborted-post
Oct 3, 2026
Merged

pi0 merged 1 commit into
nitrojs:mainfrom
AdarshYadav9:fix/vite-aborted-post

Conversation

@AdarshYadav9

Copy link
Copy Markdown
Contributor

🔗 Linked issue

Resolves #4697

❓ Type of change:-

  • 📖 Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

Description

When a client aborts a POST request while the body is still being received, Node.js emits an Error: aborted on the request stream. Nitro's Vite development middleware currently forwards this error to Vite via next(error), which logs an internal server error and broadcasts the error overlay to connected browser tabs, even though the client has already disconnected and no response can be sent.

Fix — src/build/vite/dev.ts:

// **Before**
} catch (error) {
  return next(error);
}

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

The added nodeRes.destroyed check prevents the aborted-request error from being forwarded to Vite when the client connection has already been terminated.

nodeRes.destroyed is used instead of nodeReq.destroyed because the request stream can become destroyed after the request body has been fully consumed while the client connection is still active. Using nodeReq.destroyed could therefore suppress legitimate errors.

Other errors continue to be forwarded to Vite normally.

This change is intentionally scoped to the Vite development middleware and does not affect production request handling.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly. No documentation changes are needed because this only affects internal Vite development middleware behavior.

@AdarshYadav9
AdarshYadav9 requested a review from pi0 as a code owner October 3, 2026 14:13
@vercel

vercel Bot commented Oct 3, 2026

Copy link
Copy Markdown

@AdarshYadav9 is attempting to deploy a commit to the Nitro Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f3ca2d7b-df93-4859-ac31-baa52a2dbb36
📥 Commits

Reviewing files that changed from the base of the PR and between 420873e and 570c9f3.

📒 Files selected for processing (1)
  • src/build/vite/dev.ts
 ________________________
< Tree-sitter is my GPS. >
 ------------------------
  \
   \   \
        \ /\
        ( )
      .( o ).
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pi0 pi0 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thnx!

@pi0
pi0 merged commit 6619e00 into nitrojs:main Oct 3, 2026
3 of 5 checks passed
@dennev

dennev commented Oct 13, 2026

Copy link
Copy Markdown

Thanks for the fix! One gap remains on HTTPS dev servers. When server.https is set, Vite creates its server with node:http2's createSecureServer, and browsers negotiate HTTP/2 with it. Node's HTTP/2 compatibility Http2ServerResponse has no destroyed property, so nodeRes.destroyed stays undefined after the client has gone. The error then still reaches Vite's error middleware and is broadcast to every open tab over HMR.

Checked on Node.js 26.10.0 with http2.createSecureServer({ allowHTTP1: true }): after the client cancels a POST stream (NGHTTP2_CANCEL), the response's close handler sees

{ resDestroyed: undefined, streamDestroyed: true, reqAborted: true, writableEnded: false }
// res.destroyed, res.stream.destroyed, req.aborted, res.writableEnded

A check that covers HTTP/1.1 and HTTP/2 (what we use in a dev-server error middleware for now):

function isClientGone(req, res) {
  if (req.aborted || req.errored) return true;
  if (res.writableEnded) return false;
  if (req.stream?.destroyed) return true; // HTTP/2 compatibility API
  return (req.destroyed && !req.complete) || res.destroyed || Boolean(req.socket?.destroyed);
}

With it, server-function POSTs dropped after 0 to 50 ms over HTTP/1.1 and HTTP/2 stopped reaching the open page: 6 of 84 did before, 0 of 84 after.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants