Cancel removed Network connections - #40
Conversation
There was a problem hiding this comment.
Pull request overview
This PR tightens Network.framework connection teardown semantics so NWConnection instances are explicitly cancelled when a receive loop terminates (EOF/error) and when the coordinator removes a pending or registered connection, while avoiding coordinator-queue deadlocks from cancellation-driven callbacks.
Changes:
- Cancel
NWConnectionwhen the receive loop hits EOF/error before synthesizing a.cancelledstate. - Update
NetworkPeerCoordinator.removeConnection(_:)to cancel pending/registered connections after removing them from coordinator state (and outside the coordinator serial queue). - Extend coordinator unit tests to assert removed connections are cancelled and that pending removals do not emit session events.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| Sources/NetworkPeerTransport.swift | Cancels the underlying NWConnection when the receive loop ends due to EOF/error before emitting the synthesized .cancelled state. |
| Sources/NetworkPeerCoordinator.swift | Ensures removing a connection also cancels it, with cancellation occurring outside the coordinator queue to avoid deadlock risk. |
| PeerConnectivityTests/NetworkPeerCoordinatorTests.swift | Adds/extends coverage verifying cancellations for both registered and pending connection removals (and no session event for pending removal). |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Superseded by consolidated PR #42. No commits were discarded; this branch is preserved as a recovery and reference point. Addressed review findings were replied to and resolved before consolidation. |
Summary
Makes Network connection teardown explicit when receive loops end or coordinator state removes a connection.
Changes
NWConnectionwhen the receive loop sees an error or EOF before emitting the synthesized.cancelledstate.removeConnection(_:)removes them from coordinator state.Verification
swift test --filter NetworkPeerCoordinatorTests— 18 tests passedswift test— 109 tests passedxcodebuild test -project PeerConnectivity.xcodeproj -scheme PeerConnectivity -destination 'platform=iOS Simulator,name=iPhone 16,OS=18.3.1' -configuration Debug— 109 tests passedpull_requestCI — Swift Package Tests and Xcode Project Tests passedpushCI — Swift Package Tests and Xcode Project Tests passedNotes / limitations
.cancelledcallbacks from Network.framework remain safe no-ops after the first removal.Stack context
Stacked on PR #39 (
feature/network-migration-docs-guide). This is the explicit Network connection cleanup slice of the migration stack.