Skip to content

feat: add pauseListening() and resumeListening() - #81

Merged
gmaclennan merged 1 commit into
mainfrom
feat/pause-resume-listening
Oct 21, 2026
Merged

gmaclennan merged 1 commit into
mainfrom
feat/pause-resume-listening

Conversation

@gmaclennan

@gmaclennan gmaclennan commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

iOS can reclaim the listening sockets of a suspended app. When the app comes back, the process and its open connections are still there, but the server no longer accepts new connections on either port. In CoMapeo on iOS this shows up as "Could not connect to the server" for map tiles, map imports and map sharing after the app has been in the background for more than a few seconds (Sentry COMAPEO-2QZ). Apple's TN2277 recommends closing listening sockets on background and reopening them on foreground.

This adds pauseListening() and resumeListening(). Pausing calls close() on the local HTTP server and the secret-stream server, which releases the ports synchronously. It deliberately does not wait for the 'close' event, since that only fires once open connections end. Map shares and downloads are left alone, and requests and transfers already in progress carry on, so a transfer under way survives a short app switch. Idle keep-alive connections to the local port are closed, as http.Server.close() does, and a download that hasn't yet connected to a paused sender fails. Resuming listens on the same ports again, so style URLs and map share URLs handed out earlier keep working. If a port was taken while paused it falls back to a random port, updates what getRemotePort() and listen() report, and resolves with the new ports so the caller can tell.

listen(), close(), pauseListening() and resumeListening() now all run under one p-mutex lock around a single state value (closed, listening, or paused with the ports in use), replacing start-stop-state-machine, which is dropped as a dependency. The lock serialises overlapping calls and means close() can never land between a listen() call and its 'listening' event, which Node cancels silently and would leave the caller hanging. Binding is sequential and all-or-nothing, so a failed bind can't leave one port listening, and a failed listen() or resume leaves the server closed or paused and can be retried. listen() while paused rejects with a new SERVER_PAUSED error rather than reporting ports that aren't bound, so a stray listen() elsewhere in the app can't quietly reopen the sockets in the background. close() also no longer removes the connection listeners, so connections are still tracked after a close and listen.

Calling close() and then listen({ localPort, remotePort }) already works, but it destroys every open connection, which would cut an in-progress upload or peer transfer on every app switch.

iOS can reclaim the listening sockets of a suspended app, leaving the
server unreachable after the app comes back. pauseListening() closes the
listening sockets without touching open connections, map shares or
downloads; resumeListening() listens on the same ports again, falling
back to a random port if one was taken while paused.

listen(), close(), pauseListening() and resumeListening() are serialised
with one p-mutex lock around a single state value, replacing
start-stop-state-machine. A failed listen() or resume leaves the server
closed or paused and can be retried. listen() rejects with SERVER_PAUSED
while paused, so only resumeListening() undoes a pause.
@gmaclennan
gmaclennan force-pushed the feat/pause-resume-listening branch from c6dda5e to 8bb9fa4 Compare October 20, 2026 21:03
@gmaclennan
gmaclennan requested a review from achou11 October 20, 2026 21:06
@gmaclennan
gmaclennan merged commit 850f5c9 into main Oct 21, 2026
8 checks passed
@gmaclennan
gmaclennan deleted the feat/pause-resume-listening branch October 21, 2026 16:35
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