Skip to content

fix(ios): rebuild the listener after iOS takes a suspended app's sockets - #46

Merged
heoblitz merged 1 commit into
mainfrom
fix/ios-listener-after-suspension
Nov 16, 2026
Merged

heoblitz merged 1 commit into
mainfrom
fix/ios-listener-after-suspension

Conversation

@heoblitz

Copy link
Copy Markdown
Collaborator

Summary

After an iPhone is locked for a minute or two and the app is brought back to the foreground without relaunching, Necto no longer sees the app. Every port in the SDK's range refuses the connection while the app is alive and in the foreground, and only a relaunch brings it back.

iOS reclaims the sockets of an app it has suspended, the listening one included. The app is told nothing: the descriptor stays open, connections to its port are refused, the socket never becomes readable, and nonblocking accept keeps reporting EAGAIN. NectoSocketAcceptor only watched for readability, so its sessions stream never ended, and NectoSDKRuntime.serve never returned to the retry loop that binds a new listener.

A listening socket is never writable either, until the kernel tears it down and reports EOF there. The acceptor now holds a write source on the same descriptor and ends sessions with an error when it fires. The existing retry loop does the rest: a new listener a second later, found by the Mac's next probe.

  • Nothing observes the app's lifecycle, so the SDK does not depend on how the host app is built, and a listener that survived is left alone.
  • A session that was open ends the way a dropped connection does, and the runtime goes back to listening rather than staying connected.
  • There is no polling.
  • The status passes through .failed for the second it takes to rebuild.
  • NectoControlServer on the Mac uses the same acceptor and gains the same behaviour. A healthy listener, TCP or Unix, does not fire the write source.
  • The Android SDK is not changed. Its listener blocks in accept, which throws when the socket breaks, and its runtime already rebuilds after that.

Changelog

iOS: Necto reconnects to an app that returns from suspension, without relaunching it.

Test Plan

script/test swift

==> swift test
    ok
==> Mac swift test
✔ Test run with 248 tests in 16 suites passed after 10.198 seconds.
✔ Test run with 36 tests in 3 suites passed after 0.420 seconds.
    ok
==> SDK consumer test
✔ Test run with 2 tests in 0 suites passed after 0.010 seconds.
    ok
all tests passed

Two tests are new. Both use disconnectx to tear a listening socket down while leaving its descriptor open, which leaves it in the state described above: connections refused, never readable, accept reporting EAGAIN.

  • a listening socket the system tore down ends the acceptor's sessions: with the fix reverted it runs into its time limit; with the fix it passes at once.
  • listensAgainAfterTheSystemTakesItsSockets: with a session connected, tears down the listener and the accepted connection, then expects the runtime to report listening again and to accept a new connection.

A one-off run on macOS that reclaimed the test process's sockets through pid_shutdown_sockets, which is not part of the suite, gave the same sequence: connected, listening, failed, then listening on a new port.

On a physical iPhone over USB, with ExampleApp built from this branch: necto device list showed the app, the phone was locked, then unlocked and the app brought forward without relaunching. The app showed the failed status briefly and Necto connected to it again.

Not run: the Android Gradle build, since nothing under SDK/Android changed.

iOS reclaims the sockets of an app it has suspended. A reclaimed listening
socket keeps its descriptor, refuses every connection and never becomes
readable, and nonblocking accept keeps reporting EAGAIN. The acceptor only
watched for readability, so its sessions stream never ended, the SDK never
reached the retry loop that binds a new listener, and Necto could not see the
app again until it was relaunched.

A listening socket is never writable until the kernel tears it down and
reports EOF there. The acceptor now watches for that and ends its sessions
stream with an error, which hands control to the existing retry loop. Nothing
observes the app's lifecycle, so a listener that survived is left alone.
@heoblitz
heoblitz merged commit 2f14995 into main Nov 16, 2026
7 checks passed
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