Repository navigation
feat: add pauseListening() and resumeListening() - #81
Merged
Merged
Conversation
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
force-pushed
the
feat/pause-resume-listening
branch
from
October 20, 2026 21:03
c6dda5e to
8bb9fa4
Compare
achou11
approved these changes
Oct 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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()andresumeListening(). Pausing callsclose()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, ashttp.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 whatgetRemotePort()andlisten()report, and resolves with the new ports so the caller can tell.listen(),close(),pauseListening()andresumeListening()now all run under onep-mutexlock around a single state value (closed, listening, or paused with the ports in use), replacingstart-stop-state-machine, which is dropped as a dependency. The lock serialises overlapping calls and meansclose()can never land between alisten()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 failedlisten()or resume leaves the server closed or paused and can be retried.listen()while paused rejects with a newSERVER_PAUSEDerror rather than reporting ports that aren't bound, so a straylisten()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 thenlisten({ localPort, remotePort })already works, but it destroys every open connection, which would cut an in-progress upload or peer transfer on every app switch.