From b0c38148350eeeb4247a82c7c22c47a07128cb58 Mon Sep 17 00:00:00 2001 From: serrade <89281263+serrade@users.noreply.github.com> Date: Wed, 29 Jul 2026 19:46:04 -0700 Subject: [PATCH] fix: reuse the local API endpoint so restarts keep the same port 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. --- macos/CursorAPI/Sources/CursorAPICore/LocalAPIServer.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/macos/CursorAPI/Sources/CursorAPICore/LocalAPIServer.swift b/macos/CursorAPI/Sources/CursorAPICore/LocalAPIServer.swift index 45da03a..1c88044 100644 --- a/macos/CursorAPI/Sources/CursorAPICore/LocalAPIServer.swift +++ b/macos/CursorAPI/Sources/CursorAPICore/LocalAPIServer.swift @@ -118,6 +118,11 @@ public final class LocalAPIServer: @unchecked Sendable { let endpointPort = NWEndpoint.Port(rawValue: port)! let parameters = NWParameters.tcp parameters.requiredLocalEndpoint = NWEndpoint.hostPort(host: "127.0.0.1", port: endpointPort) + // Sockets from the previous run can linger after a restart. Without + // endpoint reuse the rebind fails, the fallback picks the next port, and + // that new port gets persisted, so every restart moves the API and + // breaks anything configured against it. + parameters.allowLocalEndpointReuse = true let listener = try NWListener(using: parameters) let startResult = ListenerStartResult(port: port) listener.newConnectionHandler = { [weak self] connection in