Repository navigation
fix(local-installation): avoid UDP close-rebind races and degrade LiveKit voice on port exhaustion - #683
fix(local-installation): avoid UDP close-rebind races and degrade LiveKit voice on port exhaustion#683oldwinter53 wants to merge 2 commits into
Conversation
…eKit voice on port exhaustion
There was a problem hiding this comment.
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". Butgeneratedinsource-checkout.ts:327-358spreadssupplied(the repo.env, read wheneverexternalComputeris false,source-checkout.ts:287) before...liveKitEnv..env.example:130setsLIVEKIT_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 = falseby default) on a host where UDP allocation fails.prepareLiveKitSettingsthrowsPortAllocationError, which is caught, andliveKitEnvstays{}.LIVEKIT_URLstill comes from.env, sovoiceConfiguredis true. As a resultTEAMHUB_VOICE_MEDIA=offis not set, the services'voicesetting stays on, andbindLiveKit(native-source.ts:1085) starts LiveKit from a stalelivekit.envif one exists. That LiveKit runs on the port that was just found to be taken. If nolivekit.envexists, voice is advertised and no LiveKit runs. - The new flow test uses only the
externalComputer: truefixture, so it does not cover this case. - Fix: return a structured flag from
prepareSourceConfiguration(for exampleliveKit: LiveKitSettings | undefined) and use it innative-source.tsinstead 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 onlyinstanceof PortAllocationError. Only tests read the duck-typedcode === NO_FREE_PORTbranch inisPortAllocationError, the exportedNO_FREE_PORTconstant, and the publicprotocol/port/conflictfields. The error message already includes the port. - Simpler version: keep the class with its message and use
instanceofat the catch site. DropisPortAllocationErrorandNO_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
allInterfacesarelocalmodels-tool.ts:744/748, and both use TCP. Before this PR, UDP ignoredallInterfaces, so this is new speculative behavior. - Fix: drop the non-loopback loop from
udpSecondaryConflictand theallInterfacesoption fromfreeUdpPort.
Verified as fine
- The ephemeral UDP path no longer closes and rebinds 127.0.0.1.
freeUdpPortholds thebindUdp(0, "127.0.0.1")socket while it probes::1and closes it infinally(ports.ts:252-273).portConflictfor UDP does the same with the held IPv4 socket (ports.ts:181-189). - Binding
::1while the IPv4 socket is held cannot falsely conflict, because it usesudp6withipv6Only: true(ports.ts:96-98). - The fallback loop is bounded by
MAX_PORT_ATTEMPTSand 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
PortAllocationErroris swallowed aroundprepareLiveKitSettings; 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_URLunset.voicebecomesundefined,TEAMHUB_VOICE_MEDIA=offis set, andbindLiveKitstops any LiveKit that is running (native-source.ts:816-834, 1085-1098). AGENTS.mddescribes 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
mergeStateStatusis BLOCKED. Check whether the branch is behindmain(a rebase would be needed) or only waiting for approval.
CI
check (packages),check (installer),check (desktop),check (web),repository-gateand 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 fakedgram, 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.
…im unused UDP port helpers
There was a problem hiding this comment.
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_URLpresence: Resolved.prepareSourceConfigurationnow returnsliveKit?: LiveKitSettings(packages/local-installation/src/source-checkout.ts:157,:389), andnative-source.ts:815gates voice onconfigured.liveKit. New testvoice still stays off on LiveKit port exhaustion even when checkout .env defines LIVEKIT_URL(native-source-flow.test.ts:2292) writesLIVEKIT_URLinto the checkout.env, whichsource-checkout.ts:289-297merges intosupplied, so the oldBoolean(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-typedisPortAllocationError, and theprotocol/port/conflictfields are gone, and the caller usesinstanceof(source-checkout.ts:323). - P3 – unused UDP
allInterfacesbranch: Resolved.udpSecondaryConflictis deleted. UDP probing checks only::1(ports.ts:150,:224), andfreeUdpPorttakes justpreferred(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 "structuredPortAllocationError(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.1socket while it probes::1, then closes it infinally(ports.ts:216-232). Fallback candidates in10240-49151are deduplicated throughtriedNonEphemeral(ports.ts:238-240). The final error carries the last conflict and port (ports.ts:249). - Only
PortAllocationErroris caught aroundprepareLiveKitSettings; other errors still fail preflight (source-checkout.ts:315-327, testnon-port LiveKit configuration errors still fail preflight).prepareLiveKitSettingswriteslivekit.envonly after both ports are allocated (native-livekit.ts:73-107), so a throw leaves no partial settings file. - When
liveKitis undefined,LIVEKIT_*is left out of the generated env (source-checkout.ts:358),voiceis undefined,TEAMHUB_VOICE_MEDIA=offis set (native-source.ts:815-831), andbindLiveKitdoes not start LiveKit because it requiresvoice(native-source.ts:1084-1085). This holds even when the checkout.envsuppliesLIVEKIT_URL. - A TCP
PortAllocationErrorfor the LiveKit HTTP port takes the same voice-off path. That matches the updatedAGENTS.mdwording ("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.mddocuments both the voice-off degradation and the UDP probing strategy.- No CJK in added lines or commit messages. No merge commits.
Process notes
- Commit
2254adbfonly patches the first commit in response to review; consider squashing it intoeba2335fbefore 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.
There was a problem hiding this comment.
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
PortAllocationErroras carrying the protocol, conflict detail and(port <n>)in its message only, which matchespackages/local-installation/src/ports.ts:24-36. It no longer mentionsNO_FREE_PORTor non-loopback UDP probing.
Findings
None.
Verified as fine
- UDP close-rebind race removed: the ephemeral path holds the OS-assigned
127.0.0.1socket while it probes::1withipv6Only: true(ports.ts:214-236,bindUdpatports.ts:67-91).portConflictfor UDP also holds IPv4 while it checks::1(ports.ts:146-154). Every socket is closed in afinally, andcloseUdpSockettolerates sockets that never bound (ports.ts:55-64). - Fallback is bounded:
freeUdpPortmakes at most 100 attempts: up to 50 ephemeral binds, then random ports in 10240-49151 without repeats (ports.ts:216-249). It throwsPortAllocationErrorwith 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 onlyPortAllocationErrorand rethrows everything else (source-checkout.ts:315-327).livekit.envis 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:815gatesvoiceonconfigured.liveKit, which setsTEAMHUB_VOICE_MEDIA=off(native-source.ts:832). The service start path atnative-source.ts:1084-1091starts LiveKit only whenvoiceis set. This holds even if a checkout.envsuppliesLIVEKIT_URL. git grepfinds no other callers of UDPfreePortand no other consumers ofPortAllocationError. No CJK in added lines or commit messages.AGENTS.mdis 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 byeba2335fin the same PR. Sincemaintakes rebase merges, consider squashing it intoeba2335f. - The branch is up to date with
main, and there are no merge commits.BLOCKEDmerge 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.
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
packages/local-installation/src/ports.ts,freePort(..., { protocol: "udp" })bound an ephemeraludp4socket to127.0.0.1:0, immediately closed it, and then calledportConflict(port, "udp")to bind127.0.0.1:porta 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 withEADDRINUSEon every attempt.49152-65535),freePorthad no fallback to unprivileged non-ephemeral ports and omitted the attempted port number from its error message.prepareLiveKitSettingsran 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
udp4socket open while probing::1(usingipv6Only: true) instead of closing and immediately rebinding127.0.0.1:port.10240-49151) when OS ephemeral UDP binds fail or repeatedly collide.PortAllocationErrorincluding the protocol, conflict detail, and attempted port number ((port <n>)) in the error message for diagnostics.PortAllocationErroraroundprepareLiveKitSettingsinsource-checkout.ts, log a warning, omitLIVEKIT_*env vars, return a structuredliveKit?: LiveKitSettingsfield fromprepareSourceConfiguration, and gate voice enablement onconfigured.liveKitinnative-source.ts(so voice stays off withTEAMHUB_VOICE_MEDIA=offand LiveKit is not started even if a developer checkout.envdefinesLIVEKIT_URL); non-port configuration errors still fail preflight.packages/local-installation/AGENTS.mdand add unit tests inports.test.ts,ports-udp.test.ts, andnative-source-flow.test.ts.Verification
pnpm --filter @linger/local-installation typecheckpnpm --filter @linger/local-installation lintpnpm --filter @linger/local-installation buildpnpm --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.tspnpm --filter @linger/linger-installer typecheck && pnpm --filter @linger/linger-installer lintscripts/ci/check-changed-files.sh,pnpm env:inventory:check, andnode scripts/check-doc-links.mjs