Skip to content

fix(local-installation): avoid UDP close-rebind races and degrade LiveKit voice on port exhaustion - #683

Open
oldwinter53 wants to merge 2 commits into
mainfrom
oldwinter53/whi-1000-win-udp-port-allocation
Open

oldwinter53 wants to merge 2 commits into
mainfrom
oldwinter53/whi-1000-win-udp-port-allocation

Conversation

@oldwinter53

@oldwinter53 oldwinter53 commented Nov 24, 2026 •

Copy link
Copy Markdown

Summary

Fixes WHI-1000: Windows UDP port allocation failing across all 100 attempts (No free local port is available on this computer. UDP: 127.0.0.1 bind: EADDRINUSE) and blocking desktop installation during preflight.

Root Cause

  1. In packages/local-installation/src/ports.ts, freePort(..., { protocol: "udp" }) bound an ephemeral udp4 socket to 127.0.0.1:0, immediately closed it, and then called portConflict(port, "udp") to bind 127.0.0.1:port a second time. On Windows hosts with Hyper-V/WSL2/VPN excluded dynamic port ranges or firewall/socket-close hooks, immediately rebinding the just-closed port fails with EADDRINUSE on every attempt.
  2. When OS ephemeral UDP allocation fails or collides within the dynamic range (49152-65535), freePort had no fallback to unprivileged non-ephemeral ports and omitted the attempted port number from its error message.
  3. prepareLiveKitSettings ran unconditionally during preflight (source-checkout.ts), so a voice-only UDP port allocation failure aborted the entire desktop installation instead of keeping voice off (TEAMHUB_VOICE_MEDIA=off) like missing or failed voice tools.

Changes

  • Hold the bound udp4 socket open while probing ::1 (using ipv6Only: true) instead of closing and immediately rebinding 127.0.0.1:port.
  • Fall back to probing randomized unprivileged non-ephemeral UDP ports (10240-49151) when OS ephemeral UDP binds fail or repeatedly collide.
  • Introduce PortAllocationError including the protocol, conflict detail, and attempted port number ((port <n>)) in the error message for diagnostics.
  • Catch PortAllocationError around prepareLiveKitSettings in source-checkout.ts, log a warning, omit LIVEKIT_* env vars, return a structured liveKit?: LiveKitSettings field from prepareSourceConfiguration, and gate voice enablement on configured.liveKit in native-source.ts (so voice stays off with TEAMHUB_VOICE_MEDIA=off and LiveKit is not started even if a developer checkout .env defines LIVEKIT_URL); non-port configuration errors still fail preflight.
  • Update packages/local-installation/AGENTS.md and add unit tests in ports.test.ts, ports-udp.test.ts, and native-source-flow.test.ts.

Verification

  • pnpm --filter @linger/local-installation typecheck
  • pnpm --filter @linger/local-installation lint
  • pnpm --filter @linger/local-installation build
  • pnpm --filter @linger/local-installation exec vitest run src/ports.test.ts src/ports-udp.test.ts src/native-livekit.test.ts src/native-source.test.ts src/native-source-flow.test.ts src/instance.test.ts
  • pnpm --filter @linger/linger-installer typecheck && pnpm --filter @linger/linger-installer lint
  • scripts/ci/check-changed-files.sh, pnpm env:inventory:check, and node scripts/check-doc-links.mjs

@linger-review linger-review 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.

Approve with nits (1 P2, 2 P3)

Scope: base main, merge base afa1c47d; 1 commit, 7 files reviewed at eba2335f. Loaded coderbook, ponytail-review, karpathy-guidelines. Ran in a separate session.

Findings

P2

1. The voice degrade is inferred from whether LIVEKIT_URL is present, so a checkout .env hides the failure (packages/local-installation/src/native-source.ts:793)

  • voiceConfigured = Boolean(configured.env.LIVEKIT_URL) is meant to mean "LiveKit got its ports". But generated in source-checkout.ts:327-358 spreads supplied (the repo .env, read whenever externalComputer is false, source-checkout.ts:287) before ...liveKitEnv. .env.example:130 sets LIVEKIT_URL=ws://127.0.0.1:17880, so a developer checkout always has it.
  • Failure scenario: a non-external install (apps/linger-installer/src/server/core.ts:41, externalComputer = false by default) on a host where UDP allocation fails. prepareLiveKitSettings throws PortAllocationError, which is caught, and liveKitEnv stays {}. LIVEKIT_URL still comes from .env, so voiceConfigured is true. As a result TEAMHUB_VOICE_MEDIA=off is not set, the services' voice setting stays on, and bindLiveKit (native-source.ts:1085) starts LiveKit from a stale livekit.env if one exists. That LiveKit runs on the port that was just found to be taken. If no livekit.env exists, voice is advertised and no LiveKit runs.
  • The new flow test uses only the externalComputer: true fixture, so it does not cover this case.
  • Fix: return a structured flag from prepareSourceConfiguration (for example liveKit: LiveKitSettings | undefined) and use it in native-source.ts instead of reading an env key. This also follows the repo rule to carry decisions in structured fields.

P3

2. PortAllocationError has more surface than any caller uses (packages/local-installation/src/ports.ts:23-57)

  • The only consumer is source-checkout.ts:323, and it needs only instanceof PortAllocationError. Only tests read the duck-typed code === NO_FREE_PORT branch in isPortAllocationError, the exported NO_FREE_PORT constant, and the public protocol/port/conflict fields. The error message already includes the port.
  • Simpler version: keep the class with its message and use instanceof at the catch site. Drop isPortAllocationError and NO_FREE_PORT.

3. The UDP allInterfaces probing has no caller (packages/local-installation/src/ports.ts:153-157, freeUdpPort options at :231)

  • The only callers that pass allInterfaces are localmodels-tool.ts:744/748, and both use TCP. Before this PR, UDP ignored allInterfaces, so this is new speculative behavior.
  • Fix: drop the non-loopback loop from udpSecondaryConflict and the allInterfaces option from freeUdpPort.

Verified as fine

  • The ephemeral UDP path no longer closes and rebinds 127.0.0.1. freeUdpPort holds the bindUdp(0, "127.0.0.1") socket while it probes ::1 and closes it in finally (ports.ts:252-273). portConflict for UDP does the same with the held IPv4 socket (ports.ts:181-189).
  • Binding ::1 while the IPv4 socket is held cannot falsely conflict, because it uses udp6 with ipv6Only: true (ports.ts:96-98).
  • The fallback loop is bounded by MAX_PORT_ATTEMPTS and skips reserved ports and ports it already tried (ports.ts:276-278). The final error carries the last conflict and its port (ports.ts:287).
  • The TCP path keeps its previous behavior. Only the error type changed, and it now also reports the port (ports.ts:303-318).
  • Only PortAllocationError is swallowed around prepareLiveKitSettings; any other error still fails preflight (source-checkout.ts:322-327). The test "non-port LiveKit configuration errors still fail preflight" covers this.
  • In the desktop (external-computer) path, a failed allocation leaves LIVEKIT_URL unset. voice becomes undefined, TEAMHUB_VOICE_MEDIA=off is set, and bindLiveKit stops any LiveKit that is running (native-source.ts:816-834, 1085-1098).
  • AGENTS.md describes the new degrade and the UDP allocation behavior.
  • No CJK in the added lines or the commit message. No merge or fixup commits.

Process notes

  • mergeStateStatus is BLOCKED. Check whether the branch is behind main (a rebase would be needed) or only waiting for approval.

CI

  • check (packages), check (installer), check (desktop), check (web), repository-gate and CodeQL pass. The other jobs were skipped because their paths were not touched.
  • I did not run any tests: this worktree has no node_modules. The new UDP tests use a fake dgram, so the Windows excluded-range behavior that motivated the fix is not exercised by CI. I treated it as unverified and judged it from the code.

Automated review by Claude Code


Automated review of eba2335 by Claude Code.

@linger-review linger-review 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.

Approve with nits (1 P3)

Scope: base main, merge base afa1c47d, 2 commits, 7 files reviewed (full merge-base diff plus the delta since the previous review). Helper skills loaded: coderbook, ponytail-review, karpathy-guidelines. Ran in a separate session.

Follow-up on eba2335.

Previous findings

  • P2 – voice degradation inferred from LIVEKIT_URL presence: Resolved. prepareSourceConfiguration now returns liveKit?: LiveKitSettings (packages/local-installation/src/source-checkout.ts:157, :389), and native-source.ts:815 gates voice on configured.liveKit. New test voice still stays off on LiveKit port exhaustion even when checkout .env defines LIVEKIT_URL (native-source-flow.test.ts:2292) writes LIVEKIT_URL into the checkout .env, which source-checkout.ts:289-297 merges into supplied, so the old Boolean(configured.env.LIVEKIT_URL) check would have failed it.
  • P3 – error type and guard larger than needed: Resolved. PortAllocationError (ports.ts:24) now only builds the message; NO_FREE_PORT, the duck-typed isPortAllocationError, and the protocol/port/conflict fields are gone, and the caller uses instanceof (source-checkout.ts:323).
  • P3 – unused UDP allInterfaces branch: Resolved. udpSecondaryConflict is deleted. UDP probing checks only ::1 (ports.ts:150, :224), and freeUdpPort takes just preferred (ports.ts:192-195).

Findings

P3

  • packages/local-installation/src/ports.ts:24: the PR description no longer matches the code. It still says the PR introduces "structured PortAllocationError (code: "NO_FREE_PORT") carrying protocol, conflict detail, and attempted port number" and probes "optional non-loopback interfaces" for UDP. The second commit removed both. The attempted port now appears only in the message. Please update the description so the merged history and the PR match.

Verified as fine

  • The UDP allocation holds the ephemeral 127.0.0.1 socket while it probes ::1, then closes it in finally (ports.ts:216-232). Fallback candidates in 10240-49151 are deduplicated through triedNonEphemeral (ports.ts:238-240). The final error carries the last conflict and port (ports.ts:249).
  • Only PortAllocationError is caught around prepareLiveKitSettings; other errors still fail preflight (source-checkout.ts:315-327, test non-port LiveKit configuration errors still fail preflight). prepareLiveKitSettings writes livekit.env only after both ports are allocated (native-livekit.ts:73-107), so a throw leaves no partial settings file.
  • When liveKit is undefined, LIVEKIT_* is left out of the generated env (source-checkout.ts:358), voice is undefined, TEAMHUB_VOICE_MEDIA=off is set (native-source.ts:815-831), and bindLiveKit does not start LiveKit because it requires voice (native-source.ts:1084-1085). This holds even when the checkout .env supplies LIVEKIT_URL.
  • A TCP PortAllocationError for the LiveKit HTTP port takes the same voice-off path. That matches the updated AGENTS.md wording ("LiveKit cannot reserve a local port").
  • The TCP path is unchanged apart from the port number in the error message (ports.ts:264-281).
  • packages/local-installation/AGENTS.md documents both the voice-off degradation and the UDP probing strategy.
  • No CJK in added lines or commit messages. No merge commits.

Process notes

  • Commit 2254adbf only patches the first commit in response to review; consider squashing it into eba2335f before the rebase merge.

CI

At the time of review, check (installer), check (packages), check (desktop) and repository-gate were still pending. CodeQL, check (web) and changes passed, and the other jobs were skipped. I did not run any tests locally.

Automated review by Claude Code


Automated review of 2254adb by Claude Code.

@linger-review linger-review 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.

Approve (0 findings)

Scope: follow-up on 2254adb (same head SHA as the previous review; the code is unchanged, the PR description was updated). Base main, merge base afa1c47d, 2 commits, 7 files reviewed over the full merge-base diff. Loaded skills: coderbook (sections 2, 3, 5), ponytail-review (sections 1-3), karpathy-guidelines (sections 2-3). Ran in a separate session.

Previous findings

  • PR description still listed removed structured error fields (P3): Resolved. The body now describes PortAllocationError as carrying the protocol, conflict detail and (port <n>) in its message only, which matches packages/local-installation/src/ports.ts:24-36. It no longer mentions NO_FREE_PORT or non-loopback UDP probing.

Findings

None.

Verified as fine

  • UDP close-rebind race removed: the ephemeral path holds the OS-assigned 127.0.0.1 socket while it probes ::1 with ipv6Only: true (ports.ts:214-236, bindUdp at ports.ts:67-91). portConflict for UDP also holds IPv4 while it checks ::1 (ports.ts:146-154). Every socket is closed in a finally, and closeUdpSocket tolerates sockets that never bound (ports.ts:55-64).
  • Fallback is bounded: freeUdpPort makes at most 100 attempts: up to 50 ephemeral binds, then random ports in 10240-49151 without repeats (ports.ts:216-249). It throws PortAllocationError with the last conflict and port. The TCP path keeps its old behavior apart from the error type (ports.ts:265-281).
  • Voice degrades instead of failing: the only UDP allocator is prepareLiveKitSettings (native-livekit.ts:74,88). Its only caller, prepareSourceConfiguration, catches only PortAllocationError and rethrows everything else (source-checkout.ts:315-327). livekit.env is written only after both ports succeed (native-livekit.ts:99), so a failed run leaves no partial settings on disk.
  • No LiveKit start without settings: native-source.ts:815 gates voice on configured.liveKit, which sets TEAMHUB_VOICE_MEDIA=off (native-source.ts:832). The service start path at native-source.ts:1084-1091 starts LiveKit only when voice is set. This holds even if a checkout .env supplies LIVEKIT_URL.
  • git grep finds no other callers of UDP freePort and no other consumers of PortAllocationError. No CJK in added lines or commit messages. AGENTS.md is updated for the new degrade path and the UDP probing rule.

Process notes

  • Commit 2254adbf ("use structured LiveKit readiness flag and trim unused UDP port helpers") reworks code introduced by eba2335f in the same PR. Since main takes rebase merges, consider squashing it into eba2335f.
  • The branch is up to date with main, and there are no merge commits. BLOCKED merge state reflects required review.

CI

All relevant checks pass: installer, desktop, packages, web, repository-gate, CodeQL. The other jobs were skipped by path filters. I ran no tests locally.

Automated review by Claude Code


Automated review of 2254adb by Claude Code.

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