Repository navigation
fix: retry NetworkTransport.connect() on a fresh connection - #409
Merged
jeanmatthieu merged 1 commit intoNov 30, 2026
Conversation
`connect()` retried by restarting the `NWConnection` it had cancelled. A cancelled `NWConnection` cannot be restarted and reports no further state, so `attemptConnection()` waited on its state stream forever and the retry never completed. A `.waiting` connection was not treated as a failed attempt and had no timeout, so `connect()` to an unreachable peer blocked indefinitely and `maxAttempts` never applied. Each retry now dials a fresh connection to the same endpoint, and a later `connect()` dials fresh whenever the stored connection has been started. A `.waiting` connection counts as a failed attempt, so `maxAttempts` and backoff apply. `disconnect()` stops a `connect()` in progress and releases its connection; a `connect()` cancelled while its connection is preparing throws instead of returning success, and a `.ready` delivered after `disconnect()` no longer marks the transport connected. A second `connect()` while one is in progress throws instead of replacing the first one's connection, and the loss handlers no longer cancel the connection themselves before calling `connect()`. Adds tests for each case to `NetworkTransportTests`, and records the behavior change in the migration guide and the changelog. Co-authored-by: Jean-Matthieu Schaffhauser <jeanmatthieu@apple.com>
jeanmatthieu
approved these changes
Nov 30, 2026
varun06
approved these changes
Nov 30, 2026
jeanmatthieu
merged commit Nov 30, 2026
804718d
into
modelcontextprotocol:rel/1.0.0
12 of 14 checks passed
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.
Motivation and Context
NetworkTransport.connect()retried by restarting theNWConnectionit had cancelled. Acancelled
NWConnectioncannot be restarted and reports no further state, soattemptConnection()waited on its state stream forever and the retry never completed. A.waitingconnection was not treated as a failed attempt and had no timeout, soconnect()toan unreachable peer blocked indefinitely and the configured
maxAttemptsnever applied.connect()dials freshwhenever the stored connection has been started, because
NWConnectiondelivers.cancelledlater on its queue and a state check would still see
.waiting..waitingconnection counts as a failed attempt, somaxAttemptsand backoff apply andconnect()throws once they are exhausted.disconnect()stops aconnect()in progress and releases its connection. Aconnect()cancelled while its connection is preparing throws instead of returning success, and a
.readydelivered afterdisconnect()no longer marks the transport connected.connect()while one is in progress throws instead of replacing the first one'sconnection. This includes a
connect()stopped bydisconnect(), until it has returned. Theloss handlers in
receiveLoop()andhandleSendFailure(_:)no longer cancel the connectionbefore calling
connect(), so one cannot cancel the connection the other has just dialed.A retry dials
connection.endpoint, so it is for outbound (client) connections; a connectionaccepted from an
NWListenercannot be rebuilt this way.How Has This Been Tested?
Extends
NetworkTransportTestswith tests for a retry that succeeds on a fresh connection,connect()to an unreachable endpoint,connect()after a failedconnect(),disconnect()during
connect()'s backoff, aconnect()cancelled while its connection is preparing, a.readydelivered afterdisconnect(),connect()afterdisconnect()on a transport thatnever connected, and a second
connect()while one is dialing. Each fails with its fixreverted.
MockNetworkConnection.start()now does nothing aftercancel(), and the mock candeliver
.cancelledlate the wayNWConnectiondoes, so the tests follow the platform'sbehavior.
swift testpasses.Breaking Changes
None in the API.
connect()no longer waits indefinitely for a peer it cannot reach: a.waitingconnection now counts as a failed attempt, so code that connected while offline andrelied on
NWConnectionwaiting for connectivity fails aftermaxAttemptsinstead. A secondconnect()while one is in progress now throws. Both are in the migration guide and thechangelog.
Types of changes
AI Assistance
Drafted with an AI coding agent, then reviewed by me.
Co-authored-by: Jean-Matthieu Schaffhauser