fix: reuse the local API endpoint so restarts keep the same port - #31
fix: reuse the local API endpoint so restarts keep the same port#31serrade wants to merge 1 commit into
Conversation
NWListener was created without endpoint reuse, so sockets lingering from a previous run made the rebind fail. start(preferredPort:fallbackLimit:) then walked to the next port, and AppModel persisted that fallback as the new preference. Every restart therefore moved the local API, breaking gateways, editors and scripts configured against the old port. Setting allowLocalEndpointReuse lets the listener reclaim its configured port immediately. A genuinely occupied port still fails to bind, so the existing fallback keeps protecting startup.
|
Follow-up on my own PR, because the description here promises more than the change delivers. Endpoint reuse fixes the cross-process case, and I can confirm that part: a graceful quit and relaunch now holds the port. What it does not cover is the app competing with itself, and that turns out to be the more common path.
Measured by calling I have that written as a small follow-up: a semaphore signalled from the Worth noting either way: whichever way this goes, the one-line change here is still correct and still needed. The wait alone would not help if the previous process had left the socket behind. |
Problem
Restarting the app usually moved the local API to a different port, which breaks anything configured against it: editors, gateways, scripts.
The listener is created without endpoint reuse:
Sockets left over from the previous run keep the endpoint claimed for a while after the process exits, so the rebind fails.
start(preferredPort:fallbackLimit:)then walks to the next port, andAppModelpersists the fallback as the new preference:So the drift is sticky: each restart that hits a lingering socket moves the port permanently. Locally this walked 8789 to 8793 to 8794 over a few restarts.
Fix
Set
allowLocalEndpointReuseon the listener parameters. The port is reclaimed immediately after a restart. A port genuinely held by another live listener still fails to bind, so the existing fallback continues to protect startup.Verification
swift buildclean; Package macOS smoke green on my fork.Note on overlap
This is the public API listener in
LocalAPIServer, which is a different port from the SDK bridge port that #27 works on (8792-8892,CursorSDKBridgeServer). The two changes do not touch the same file. #29 editsstart(port:)nearby for LAN mode; whichever of us lands second has a trivial rebase.