Skip to content

fix(dev): do not leak a rejection when a proxied upgrade fails - #4494

Merged
pi0 merged 2 commits into
nitrojs:v2from
kedrzu:fix/dev-unhandled-upgrade-rejection
Nov 10, 2026
Merged

pi0 merged 2 commits into
nitrojs:v2from
kedrzu:fix/dev-unhandled-upgrade-rejection

Conversation

@kedrzu

@kedrzu kedrzu commented Jul 30, 2026 •

Copy link
Copy Markdown

🔗 Linked issue

Resolves #4493

❓ 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 to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

DevServer.handleUpgrade is async, and both callers drop its promise: the upgrade listener in listen(), and the exported upgrade(), which Nuxt calls without awaiting.

It rejects whenever a proxied websocket dies. httpxy's ws() promise stays pending for the whole life of the socket and rejects with ECONNRESET when either side resets it, for example a browser refresh or a client abort. With no worker, handleUpgrade also threw a 503 createError. Either way the result was an unhandled rejection. Under Nuxt that restarts the dev server (nuxt/cli#247, nuxt/cli#968, nuxt/cli#994).

Changes:

  • handleUpgrade never rejects. A failed upgrade destroys the socket. Peer disconnects (ECONNRESET, ECONNABORTED, EPIPE, ERR_STREAM_PREMATURE_CLOSE) are not logged; other errors are logged with consola.warn.
  • With no worker (for example a broken build), the client gets an HTTP/1.1 503 response and the socket is closed. This isn't logged, because the build error is already reported and reconnecting clients would flood the console.
  • devProxy websocket routes (fix(dev): proxy websocket upgrades for devProxy routes with ws #4788) skip the warning on peer disconnects too.
  • DevWorker["handleUpgrade"] is typed Promise<void> | void, which is what NodeDevWorker already returns.
  • New test in test/dev-shutdown covering: client resets of an established websocket, worker reload with an open websocket, and the 503 after dev:error. On v2 without this fix it fails with 3 ECONNRESET unhandled rejections.

main has the same pattern in src/dev/server.ts, so it needs its own PR.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

🤖 Generated with AI assistant

@kedrzu
kedrzu requested a review from pi0 as a code owner July 30, 2026 11:29
@vercel

vercel Bot commented Jul 30, 2026

Copy link
Copy Markdown

@kedrzu 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 Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f9d857bc-5b50-41c8-90ac-58debfeca7f0

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · 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

@pi0x pi0x added v2 bug Something isn't working (PRs; issues use the Bug type) dev Development server proxy Proxy route rule and devProxy labels Sep 2, 2026
@pi0x

pi0x commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

This conflicts with #4788 now. After rebasing, handleUpgrade should keep if (this.proxyUpgrade(req, socket, head)) return; as its first statement, before the new try/catch.

Triaged with AI assistance.

Michał Kędrzyński and others added 2 commits November 10, 2026 09:34
`DevServer.handleUpgrade` is async and both callers drop the promise it
returns: the `upgrade` method handed out by `createDevServer`, and the
`server.on("upgrade")` listener in `listen`. It rejects when no worker is
available, and — far more often — from `NodeDevWorker.handleUpgrade`,
which returns `proxy.ws(...)` unawaited. Replacing the dev worker kills
every websocket proxied to it, so that promise rejects with `ECONNRESET`
and nothing is there to catch it.

For Nitro alone that is a stray unhandled rejection. Under Nuxt it ends
the session, because the CLI treats any unhandled rejection as fatal and
restarts the dev server; with a client that reconnects it loops.

Make `handleUpgrade` never reject. There is nothing left to reply with
once a handshake is in flight, so a failed upgrade closes the socket, and
only errors that are not a peer disconnect are reported.

Resolves nitrojs#4493

Co-Authored-By: Claude Opus 5 <[email protected]>
@pi0x
pi0x force-pushed the fix/dev-unhandled-upgrade-rejection branch from d53479b to 27d131d Compare November 10, 2026 09:37
@pi0
pi0 merged commit 72a9ce9 into nitrojs:v2 Nov 10, 2026
4 of 5 checks passed
@pi0x pi0x removed the v2 label Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working (PRs; issues use the Bug type) dev Development server proxy Proxy route rule and devProxy

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants