You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[PR] fix: keep listen retries within the valid port range - #7362
Port 65535 is accepted as a zone base or sticky port. If a launcher-managed listener must move off it while retry attempts remain, both the EADDRINUSE policy and the post-bind ownership policy return 65536. Node rejects that value with ERR_SOCKET_BAD_PORT, preventing startup on a supported configuration.
Changes
Share the retry-port calculation between both policies. Exhausting the numeric port range now chooses the existing port-0 fallback, just as exhausting the retry budget does. A retry from 65534 still tries 65535.
The calculation remains after the existing retry-rights checks. Strict-port/manual launches, upgrade replacements, proven twins/newer holders and predecessor waits retain their existing outcomes.
Before the fix: 2 regressions fail when the production decisions reach real net.Server.listen calls with port 65536; 2 controls pass.
After the fix: 57 targeted tests pass across listen-retry-boundary, listen-policy, port-owner and port-zone. Both boundary retries bind OS-assigned ports, with no assumption that a fixed port is free.
npm run typecheck, npm run build, npm run check:unused-exports, npm run check:test-import-graph and git diff --check: passed.
Published tree eed65d72a6db4d2f85974ad864fe19b1a11b0970 exactly matches the locally validated tree.
Node 24.22.3 / Linux x86_64. No live host-client or Windows run; the full-suite verdict remains repository CI on this head.
Related #1670, #3055 and #2947 established the existing zone/replacement/ownership contracts. This fixes the remaining port-range boundary in both retry paths as one change.
ranxianglei
changed the title
fix: keep listen retries within the valid port range
[PR] fix: keep listen retries within the valid port range
Nov 20, 2026
查重: 不重复。 Searched open+closed issues and PRs for the failure signature (65536, ERR_SOCKET_BAD_PORT): the only matches are #7361 (this bug) and this PR. The related contracts #1670/#3055/#2947 are the zone/replacement/ownership foundations this builds on, not parallel fixes. No other open PR targets this thread.
Review — PR #7362 (head bf39ca608f0, base 1e357bf2d66 = current master)
Direction (①). Issue #7361 asks: when the numeric port range ends, use the existing port-0 fallback; 65534 must still advance to 65535; strict/manual/replacement/predecessor precedence untouched. The PR does exactly that. I verified the boundary is reachable through both documented entry points: resolveZonePortBase (src/config.ts:2917) accepts fromEnv < 65536, and readZonePort (src/instance.ts:110) accepts the same range from the sticky record. The fix lands at the decision point (the producer of the invalid value), not at the listen call — root-cause, not a mask.
Regression audit (②). Existing behaviors on every touched path, item by item:
decideEaddrinuse (src/listen-policy.ts): twin-collision, upgrade-replacement (wait/fail), strict-port / no-launch-token / budget-exhausted fail-fast, and predecessor-wait are all early branches BEFORE the ladder line — untouched, and the new test 4 pins strict-port / no-token / upgrade-replacement / predecessor-wait / post-bind die.
Ladder 65534→65535 preserved (test 3 pins it on both paths); budget exhaustion → 0 preserved. The ===→>= unification in nextListenPort is behavior-neutral for decideEaddrinuse: the guard ctx.attempts >= ctx.maxAttempts fail-fasts before the ladder line, so nextAttempts <= maxAttempts there; resolvePortClaim already used >=.
resolvePortClaim (src/port-owner.ts): claimed and die outcomes untouched.
server.ts call sites untouched: both log lines already render nextPort === 0 as "retrying on an ephemeral port", and attemptListen(0) is the established budget-exhaustion path.
Grep of src/ confirms these two retry paths are the ONLY port-increment sites; no third path has the same boundary.
The single behavior change is 65535 → 0 where it was 65536. The old value was never consumable (Node rejects it with ERR_SOCKET_BAD_PORT), so nothing could have depended on it. Intended fix, disclosed in the PR body.
Code (③).nextListenPort is a small pure function with a why-comment; the 65535 boundary matches the existing v < 65536 validation style in config.ts/instance.ts. No dead code, no new dependencies, style consistent.
Tests (④). Real teeth, verified locally: with the PRE-fix src/listen-policy.ts + src/port-owner.ts, the two new regression tests fail with exactly ERR_SOCKET_BAD_PORT while the two controls pass; at the PR head all 57 targeted tests pass (listen-retry-boundary, listen-policy, port-owner, port-zone). The boundary tests drive the production entry points into a real net.Server.listen on port 0 — no fixed-port-free assumption.
Diff (⑤). 3 files, 75+/2−, every line on-topic, no whitespace churn, no unrelated files.
CI. Head bf39ca608f0: 57 success / 27 skipped (gated e2e lanes — this file list doesn't trigger them) / 0 failures; all 11 test shards green (ubuntu+windows × node 22/24). The PR body's "no Windows run" refers to the author's local harness; CI covers Windows. PR-body claims independently verified: base is master tip, head tree eed65d72a6… matches, 57-test count reproduces.
结构化八问
是什么问题: zone 端口取最大合法值 65535(BILI_ZONE_PORT=65535 或 sticky zone 记录)且该端口被占用时,两条重试路径都算出 65536,Node 以 ERR_SOCKET_BAD_PORT 拒绝,launcher 托管的 proxy 无法启动。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Port 65535 is accepted as a zone base or sticky port. If a launcher-managed listener must move off it while retry attempts remain, both the EADDRINUSE policy and the post-bind ownership policy return 65536. Node rejects that value with
ERR_SOCKET_BAD_PORT, preventing startup on a supported configuration.Changes
Share the retry-port calculation between both policies. Exhausting the numeric port range now chooses the existing port-0 fallback, just as exhausting the retry budget does. A retry from 65534 still tries 65535.
The calculation remains after the existing retry-rights checks. Strict-port/manual launches, upgrade replacements, proven twins/newer holders and predecessor waits retain their existing outcomes.
Verification
1e357bf2d6632bfbd2a74ffb70ecee1af1ec7cc3(currentmaster, 0.1.216).net.Server.listencalls with port 65536; 2 controls pass.listen-retry-boundary,listen-policy,port-ownerandport-zone. Both boundary retries bind OS-assigned ports, with no assumption that a fixed port is free.npm run typecheck,npm run build,npm run check:unused-exports,npm run check:test-import-graphandgit diff --check: passed.eed65d72a6db4d2f85974ad864fe19b1a11b0970exactly matches the locally validated tree.Related #1670, #3055 and #2947 established the existing zone/replacement/ownership contracts. This fixes the remaining port-range boundary in both retry paths as one change.
Fixes #7361