Skip to content

Rebuild listening sockets on foreground when not suspending in background - #19

Merged
TimOliver merged 1 commit into
mainfrom
reconnect-on-foreground
Jul 22, 2026
Merged

Rebuild listening sockets on foreground when not suspending in background#19
TimOliver merged 1 commit into
mainfrom
reconnect-on-foreground

Conversation

@TimOliver

Copy link
Copy Markdown
Owner

With GCDWebServerOption_AutomaticallySuspendInBackground = NO the server keeps serving in the background instead of stopping, but iOS can still tear down its listening sockets once the process is actually suspended (e.g. the device is locked for a while). Nothing rebuilt them on resume, so the server silently stopped accepting connections even though it still looked started (swisspol/GCDWebServer#292).

In that mode, observe UIApplicationWillEnterForeground and, if we were running, rebuild the listening sockets via the existing _stop/_start path. That notification only fires on a real background->foreground transition — exactly when a socket may have been reclaimed — so there is no churn on ordinary activation. _start reuses the previously assigned port, so client URLs stay valid, and because _stop only cancels the listening sources, already-accepted connections (in-flight uploads, open SSE streams) are unaffected.

Verified headlessly (macOS, driving the same _stop/_start the handler uses): the port stays stable, new connections are accepted afterwards, and a long- lived SSE stream keeps delivering across the refresh. iOS/tvOS compile the handler. The actual "iOS reclaimed the socket" trigger can only be confirmed on-device.

…ound

With GCDWebServerOption_AutomaticallySuspendInBackground = NO the server keeps
serving in the background instead of stopping, but iOS can still tear down its
listening sockets once the process is actually suspended (e.g. the device is
locked for a while). Nothing rebuilt them on resume, so the server silently
stopped accepting connections even though it still looked started
(swisspol/GCDWebServer#292).

In that mode, observe UIApplicationWillEnterForeground and, if we were running,
rebuild the listening sockets via the existing _stop/_start path. That
notification only fires on a real background->foreground transition — exactly
when a socket may have been reclaimed — so there is no churn on ordinary
activation. _start reuses the previously assigned port, so client URLs stay
valid, and because _stop only cancels the listening sources, already-accepted
connections (in-flight uploads, open SSE streams) are unaffected.

Verified headlessly (macOS, driving the same _stop/_start the handler uses):
the port stays stable, new connections are accepted afterwards, and a long-
lived SSE stream keeps delivering across the refresh. iOS/tvOS compile the
handler. The actual "iOS reclaimed the socket" trigger can only be confirmed
on-device.
@TimOliver
TimOliver merged commit cdadf19 into main Jul 22, 2026
@TimOliver
TimOliver deleted the reconnect-on-foreground branch July 24, 2026 17:36
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.

1 participant