Repository navigation
fix(node): enable SO_REUSEPORT for reusePort option - #258
Conversation
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>
📝 WalkthroughWalkthroughNode’s adapter now forwards ChangesNode reusePort support
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
commit: |
There was a problem hiding this comment.
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
📒 Files selected for processing (2)
src/adapters/node.tstest/node-adapters.test.ts
| // 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)); |
There was a problem hiding this comment.
🎯 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.
| // 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.
Problem
On the Node adapter,
options.reusePortwas only mapped toexclusive: !reusePortin thelisten()options (src/adapters/node.ts). Theexclusiveflag controls cluster handle sharing — it does not enableSO_REUSEPORT. Node added its ownreusePortlisten 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.tsboth passreusePort: this.options.reusePort) simply never happened on Node.Fix
Pass
reusePortthrough to the Node listen options alongside the existingexclusivemapping:SO_REUSEPORT, matching Bun/Deno semantics.reusePort: true— the expected behavior for an explicit opt-in, consistent with Node's own documented contract.reusePortis already part of the acceptednodelisten 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
reusePortdescribe block intest/node-adapters.test.ts:{ reusePort: true, exclusive: false }).SO_REUSEPORTis now enabled (it wouldEADDRINUSEotherwise). Skipped gracefully on unsupported runtimes/platforms.pnpm vitest run test/node.test.ts test/node-adapters.test.ts→ 160 passed / 6 skipped.pnpm typecheckclean.Part of #249
🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
reusePortis enabled.Bug Fixes