Skip to content

fix(node): add the upgrade listener once - #212

Closed
boluwatifeajayi wants to merge 2 commits into
h3js:mainfrom
boluwatifeajayi:fix/node-serve-duplicate-upgrade-handler
Closed

boluwatifeajayi wants to merge 2 commits into
h3js:mainfrom
boluwatifeajayi:fix/node-serve-duplicate-upgrade-handler

Conversation

@boluwatifeajayi

@boluwatifeajayi boluwatifeajayi commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #205 (the issue is filed under unjs/crossws, which now lives here).

The node server plugin replaces server.serve with a function that adds an upgrade listener to the node http server and then calls the original serve. The listener was added on every call, so with repeated serve() calls each websocket upgrade went through ws.handleUpgrade several times, which is the unexpected behaviour from the issue.

The issue suggests either doing nothing or throwing on a repeated call. I went with not adding the listener again and still calling the original serve, so what srvx does for a second call (error or not) stays the same and the plugin does not decide that on its own. If you would rather throw here, that is a small change.

Added a test in test/unit.test.ts that installs the plugin on a fake server, calls serve() three times and checks the upgrade listener count is one and the original serve ran three times. It fails on main. pnpm lint passes. The full vitest run has the same 33 failures with and without this change on my machine (they need the deno and bun runtimes and the proxy setup), and the unit test file passes.

Summary by CodeRabbit

  • Bug Fixes
    • Prevented duplicate WebSocket upgrade handling when the server is started multiple times.
    • Preserved normal server startup behavior on each invocation.
  • Tests
    • Added coverage to verify upgrade handling is registered only once while server startup continues to run for every call.

The plugin wrapped server.serve() and added a new upgrade listener to the
node server on every call, so a repeated serve() made each websocket
upgrade run through the handler several times. Only add the listener the
first time.
Copilot AI balanced review requested due to automatic review settings October 21, 2026 03:03
@vercel

vercel Bot commented Oct 21, 2026

Copy link
Copy Markdown

@boluwatifeajayi is attempting to deploy a commit to the unjs Team on Vercel.

A member of the Team first needs to authorize it.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@coderabbitai

coderabbitai Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f2d004fa-5c2f-4f3b-91a8-afdf78d8e3d3
📥 Commits

Reviewing files that changed from the base of the PR and between 2e523be and dea8e09.

⛔ Files ignored due to path filters: 1
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing: 2
  • src/server/node.ts
  • test/unit.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

📝 Walkthrough

The Node plugin now adds one upgrade listener across repeated serve() calls. The original server serve method still runs on every call. A unit test checks this behavior across three calls.

Changes: Node listener registration

Layer / File(s) Summary
Serve listener registration
src/server/node.ts, test/unit.test.ts
The plugin registers the upgrade listener only once. The test verifies that three serve() calls add one listener and invoke the original serve method three times.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: pi0, nekomeowww

Merge Risk: ⚪ Minimal · up to 7af66

Merge Risk: ⚪ Minimal · up to 7af66

This change prevents duplicate WebSocket upgrade handlers when serve() is called repeatedly in the Node plugin, and the original serve behavior is preserved. No actionable merge risk was identified.

Pre-merge checks: Passed 3 · Failed 2

Failed Failed checks: 2 warnings

Check name Status Explanation Resolution
Linked Issues check Warning Issue [#205] requires repeated serve() calls to either do nothing or throw. The change prevents duplicate upgrade listeners, but the repeated calls still invoke the original serve() function. Th… Make repeated serve() calls no-ops or throw an exception, then update the unit test to verify the selected behavior while retaining coverage that only one upgrade listener is registered.
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 3 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: registering the Node upgrade listener only once.
Out of Scope Changes check Passed The changes are limited to the Node server plugin and its unit test. Both changes directly support issue [#205]. No unrelated source or test changes are shown.
Full details: Linked Issues check

Explanation

Issue [#205] requires repeated serve() calls to either do nothing or throw. The change prevents duplicate upgrade listeners, but the repeated calls still invoke the original serve() function. The added test explicitly expects three original calls, so the implementation does not satisfy the stated repeated-call behavior.

Resolution

Make repeated serve() calls no-ops or throw an exception, then update the unit test to verify the selected behavior while retaining coverage that only one upgrade listener is registered.

✨ Finishing Touches: 💡 1
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Create a new PR
  • 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

A rabbit counts each call with care
One upgrade listener waits there
Three serves run along their way
No extra listener joins the play
The rabbit hops through tests today

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

@codecov

codecov Bot commented Oct 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

pi0x pushed a commit that referenced this pull request Nov 11, 2026
…rade listener once

- track raw upgrade sockets in the srvx plugin and destroy them on `close(true)`, so an upgrade whose hook is still pending can't complete afterwards and keep the server open
- add the `upgrade` listener only once across repeated `serve()` calls (fixes #205, supersedes #212)
- respect `silent` when logging failed upgrades

Co-authored-by: boluwatifeajayi <bolu4good@gmail.com>
@pi0x

pi0x commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Thanks @boluwatifeajayi! This fix is folded into #225 (with you as co-author on the commit), since both PRs touched the same upgrade listener lines. #225 also fixes #205. Closing in favor of it.

@pi0x pi0x closed this Nov 11, 2026
pi0 added a commit that referenced this pull request Nov 11, 2026
…eers on forced server close (#225)

* fix(node): catch `handleUpgrade` rejections and terminate WebSocket peers on forced server close

- catch rejections from `handleUpgrade` itself (e.g. writing the rejection response) in the srvx plugin: destroy the socket and log the error instead of raising an unhandled rejection
- `close(true)` now terminates crossws peers and stops the idle sweep (Node's `closeAllConnections()` doesn't reach upgraded sockets); a graceful `close()` leaves them open, matching srvx docs and Bun
- build the upgrade `NodeRequest` with the server's `trustProxy` and `waitUntil`, like srvx does for regular requests

* fix(node): terminate pending upgrades on forced close and add the upgrade listener once

- track raw upgrade sockets in the srvx plugin and destroy them on `close(true)`, so an upgrade whose hook is still pending can't complete afterwards and keep the server open
- add the `upgrade` listener only once across repeated `serve()` calls (fixes #205, supersedes #212)
- respect `silent` when logging failed upgrades

Co-authored-by: boluwatifeajayi <bolu4good@gmail.com>

---------

Co-authored-by: Pooya Parsa <pooya@pi0.io>
Co-authored-by: boluwatifeajayi <bolu4good@gmail.com>
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.

Node: prevent repeated serve() calls from registering duplicate upgrade handlers

4 participants