Skip to content

fix(node): enable SO_REUSEPORT for reusePort option - #258

Merged
pi0 merged 4 commits into
mainfrom
fix/249-node-reuseport
Jul 16, 2026
Merged

pi0 merged 4 commits into
mainfrom
fix/249-node-reuseport

Conversation

@pi0x

@pi0x pi0x commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Problem

On the Node adapter, options.reusePort was only mapped to exclusive: !reusePort in the listen() options (src/adapters/node.ts). The exclusive flag controls cluster handle sharing — it does not enable SO_REUSEPORT. Node added its own reusePort listen option in v22.12/v23.1, and srvx never set it.

As a result the cross-process, kernel-level port-sharing that Bun and Deno already implement for this same srvx option (src/adapters/bun.ts, src/adapters/deno.ts both pass reusePort: this.options.reusePort) simply never happened on Node.

Fix

Pass reusePort through to the Node listen options alongside the existing exclusive mapping:

this.serveOptions = {
  port,
  host,
  exclusive: !this.options.reusePort,
  reusePort: this.options.reusePort,
  ...tls,
  ...this.options.node,
};
  • Node >= 22.12 now enables SO_REUSEPORT, matching Bun/Deno semantics.
  • Older Node versions ignore the unknown listen option (no throw).
  • Unsupported platforms (e.g. Windows, macOS) throw when reusePort: true — the expected behavior for an explicit opt-in, consistent with Node's own documented contract.

reusePort is already part of the accepted node listen options type (NodeNet.ListenOptions), so no type changes are needed. The diff is confined to the listen-options region to minimize conflicts with the other in-flight Node adapter PR.

Tests

Added a reusePort describe block in test/node-adapters.test.ts:

  • An always-run test asserting the option is mapped into the computed listen options ({ reusePort: true, exclusive: false }).
  • A Linux + Node >= 22.12 behavioral test that two srvx servers bind the same port simultaneously — which only succeeds because SO_REUSEPORT is now enabled (it would EADDRINUSE otherwise). Skipped gracefully on unsupported runtimes/platforms.

pnpm vitest run test/node.test.ts test/node-adapters.test.ts → 160 passed / 6 skipped. pnpm typecheck clean.

Part of #249

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added support for sharing a server port across multiple processes when reusePort is enabled.
    • Improved compatibility across supported Node.js versions and platforms.
  • Bug Fixes

    • Ensured the server correctly passes the port-reuse setting to the underlying HTTP or HTTPS server.

pi0 and others added 2 commits July 16, 2026 18:10
The WebIncomingMessage "end" handler called `this.off("data", onData)`,
but `onData` is registered on the source `socket`, not on the
IncomingMessage. Call `socket.off("data", onData)` so the listener is
actually removed from where it was added.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The Node adapter only mapped `options.reusePort` to `exclusive`
(cluster handle sharing) and never set Node's own `reusePort` listen
option, so the cross-process port-sharing that Bun/Deno implement for
this same srvx option never happened on Node.

Pass `reusePort` through to the listen options so Node >= 22.12 enables
SO_REUSEPORT. Older Node versions ignore the unknown option, and
unsupported platforms throw (the expected opt-in behavior).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@pi0x
pi0x requested a review from pi0 as a code owner July 16, 2026 18:14
@coderabbitai

coderabbitai Bot commented Jul 16, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Node’s adapter now forwards reusePort to HTTP/HTTPS server creation. Tests verify option translation and, where supported, confirm two servers can bind the same port.

Changes

Node reusePort support

Layer / File(s) Summary
Forward reusePort and validate shared listeners
src/adapters/node.ts, test/node-adapters.test.ts
NodeServer passes reusePort through serveOptions; tests verify reusePort: true, exclusive: false, and supported shared-port startup.

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

Suggested reviewers: pi0, pi0

Poem

A rabbit hops where shared ports gleam,
Two servers bind like a tidy dream.
Options flow with ears held high,
Tests watch both listeners occupy.
Hop, hop—reusePort is spry!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: enabling SO_REUSEPORT support via the Node reusePort option.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/249-node-reuseport

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.

@pkg-pr-new

pkg-pr-new Bot commented Jul 16, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/srvx@258

commit: 53000b2

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@test/node-adapters.test.ts`:
- Around line 533-539: Update the Node version parsing near the supported
calculation to safely handle missing process.versions.node by using optional
access and fallback values before splitting and mapping. Preserve the existing
Linux, runtime exclusion, and Node 22.12-or-newer checks.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: db8751b7-cf37-4bbe-9e9a-8cd3bbca746e

📥 Commits

Reviewing files that changed from the base of the PR and between d792737 and 603e8be.

📒 Files selected for processing (2)
  • src/adapters/node.ts
  • test/node-adapters.test.ts

Comment on lines +533 to +539
// SO_REUSEPORT is only supported on Linux (and a few BSDs) with Node >= 22.12.
const [major, minor] = process.versions.node.split(".").map(Number);
const supported =
!globalThis.Deno &&
!globalThis.Bun &&
process.platform === "linux" &&
(major > 22 || (major === 22 && minor >= 12));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Prevent potential crashes in non-Node environments.

Accessing .split() directly on process.versions.node assumes it is always defined. If these tests are ever executed in a runtime without a fully populated process.versions object (such as Deno without Node compat), this will throw a TypeError and crash the suite before reaching the !globalThis.Deno check.

Adding optional chaining and fallback values ensures it safely resolves in all runtimes.

🛡️ Proposed fix
   // SO_REUSEPORT is only supported on Linux (and a few BSDs) with Node >= 22.12.
-  const [major, minor] = process.versions.node.split(".").map(Number);
+  const [major = 0, minor = 0] = process.versions?.node?.split(".").map(Number) || [];
   const supported =
     !globalThis.Deno &&
     !globalThis.Bun &&
-    process.platform === "linux" &&
+    process?.platform === "linux" &&
     (major > 22 || (major === 22 && minor >= 12));
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// SO_REUSEPORT is only supported on Linux (and a few BSDs) with Node >= 22.12.
const [major, minor] = process.versions.node.split(".").map(Number);
const supported =
!globalThis.Deno &&
!globalThis.Bun &&
process.platform === "linux" &&
(major > 22 || (major === 22 && minor >= 12));
// SO_REUSEPORT is only supported on Linux (and a few BSDs) with Node >= 22.12.
const [major = 0, minor = 0] = process.versions?.node?.split(".").map(Number) || [];
const supported =
!globalThis.Deno &&
!globalThis.Bun &&
process?.platform === "linux" &&
(major > 22 || (major === 22 && minor >= 12));
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/node-adapters.test.ts` around lines 533 - 539, Update the Node version
parsing near the supported calculation to safely handle missing
process.versions.node by using optional access and fallback values before
splitting and mapping. Preserve the existing Linux, runtime exclusion, and Node
22.12-or-newer checks.

@pi0
pi0 merged commit 7411a0f into main Jul 16, 2026
13 of 14 checks passed
@pi0
pi0 deleted the fix/249-node-reuseport branch July 16, 2026 18:37
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.

2 participants