Skip to content

fix: retry NetworkTransport.connect() on a fresh connection - #409

Merged
jeanmatthieu merged 1 commit into
modelcontextprotocol:rel/1.0.0from
canarik:oss/network-reconnect-fresh-connection
Nov 30, 2026
Merged

jeanmatthieu merged 1 commit into
modelcontextprotocol:rel/1.0.0from
canarik:oss/network-reconnect-fresh-connection

Conversation

@canarik

@canarik canarik commented Nov 30, 2026

Copy link
Copy Markdown

Motivation and Context

NetworkTransport.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 the configured maxAttempts never applied.

  • Each retry now dials a fresh connection to the same endpoint. A later connect() dials fresh
    whenever the stored connection has been started, because NWConnection delivers .cancelled
    later on its queue and a state check would still see .waiting.
  • A .waiting connection counts as a failed attempt, so maxAttempts and backoff apply and
    connect() throws once they are exhausted.
  • 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. This includes a connect() stopped by disconnect(), until it has returned. The
    loss handlers in receiveLoop() and handleSendFailure(_:) no longer cancel the connection
    before 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 connection
accepted from an NWListener cannot be rebuilt this way.

How Has This Been Tested?

Extends NetworkTransportTests with tests for a retry that succeeds on a fresh connection,
connect() to an unreachable endpoint, connect() after a failed connect(), disconnect()
during connect()'s backoff, a connect() cancelled while its connection is preparing, a
.ready delivered after disconnect(), connect() after disconnect() on a transport that
never connected, and a second connect() while one is dialing. Each fails with its fix
reverted. MockNetworkConnection.start() now does nothing after cancel(), and the mock can
deliver .cancelled late the way NWConnection does, so the tests follow the platform's
behavior. swift test passes.

Breaking Changes

None in the API. connect() no longer waits indefinitely for a peer it cannot reach: a
.waiting connection now counts as a failed attempt, so code that connected while offline and
relied on NWConnection waiting for connectivity fails after maxAttempts instead. A second
connect() while one is in progress now throws. Both are in the migration guide and the
changelog.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to change)

AI Assistance

Drafted with an AI coding agent, then reviewed by me.

Co-authored-by: Jean-Matthieu Schaffhauser

`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>
@canarik
canarik requested a review from a team as a code owner November 30, 2026 12:58
@jeanmatthieu
jeanmatthieu merged commit 804718d into modelcontextprotocol:rel/1.0.0 Nov 30, 2026
12 of 14 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.

3 participants