Repository navigation
Conversation
NetworkTransport.attemptConnection() was writing connection.stateUpdateHandler from two contexts: the actor's executor (setting the handler) and AsyncStream's onTermination callback (clearing it to nil), which can fire on any thread. Fix: remove onTermination and use defer to clear the handler on the actor's executor after the for-await loop exits, regardless of normal return or throw. MockNetworkConnection was @unchecked Sendable with entirely unprotected mutable state. The Task { @mainactor in } wrappers were inconsistent: simulateFailure wrote mockError directly while receive read it on @mainactor, and stateUpdateHandler was set from NetworkTransport's actor executor while being read/written on @mainactor. Fix: add NSLock protecting all mutable state; snapshot handler before calling it to avoid holding the lock during the callback.
varun06
force-pushed
the
network-transport-races
branch
from
October 21, 2026 19:17
03776c3 to
bef2f07
Compare
Comment on lines
+74
to
+88
| let (state, error, data): (NWConnection.State, Swift.Error?, Data?) = lock.withLock { | ||
| let d = dataToReceive.isEmpty ? nil : dataToReceive.removeFirst() | ||
| return (mockState, mockError, d) | ||
| } | ||
|
|
||
| let data = self.dataToReceive.removeFirst() | ||
| completion(data, nil, self.dataToReceive.isEmpty, nil) | ||
| if state == .cancelled { | ||
| completion(nil, nil, true, NWError.posix(POSIXErrorCode.ECANCELED)) | ||
| return | ||
| } | ||
| if let error { | ||
| completion(nil, nil, false, error as? NWError) | ||
| return | ||
| } | ||
| let isLast = lock.withLock { dataToReceive.isEmpty } | ||
| completion(data ?? Data(), nil, data == nil ? false : isLast, nil) |
Member
There was a problem hiding this comment.
@varun06 This removes a queued item before the cancelled/error checks, so it gets dropped on those paths. isLast is also read under a separate lock. Do both under one lock:
Suggested change
| let (state, error, data): (NWConnection.State, Swift.Error?, Data?) = lock.withLock { | |
| let d = dataToReceive.isEmpty ? nil : dataToReceive.removeFirst() | |
| return (mockState, mockError, d) | |
| } | |
| let data = self.dataToReceive.removeFirst() | |
| completion(data, nil, self.dataToReceive.isEmpty, nil) | |
| if state == .cancelled { | |
| completion(nil, nil, true, NWError.posix(POSIXErrorCode.ECANCELED)) | |
| return | |
| } | |
| if let error { | |
| completion(nil, nil, false, error as? NWError) | |
| return | |
| } | |
| let isLast = lock.withLock { dataToReceive.isEmpty } | |
| completion(data ?? Data(), nil, data == nil ? false : isLast, nil) | |
| let (state, error, data, isLast): (NWConnection.State, Swift.Error?, Data?, Bool) = | |
| lock.withLock { | |
| guard mockState != .cancelled, mockError == nil, !dataToReceive.isEmpty else { | |
| return (mockState, mockError, nil, false) | |
| } | |
| let data = dataToReceive.removeFirst() | |
| return (mockState, mockError, data, dataToReceive.isEmpty) | |
| } | |
| if state == .cancelled { | |
| completion(nil, nil, true, NWError.posix(POSIXErrorCode.ECANCELED)) | |
| return | |
| } | |
| if let error { | |
| completion(nil, nil, false, error as? NWError) | |
| return | |
| } | |
| completion(data ?? Data(), nil, isLast, nil) |
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.attemptConnection()setconnection.stateUpdateHandleron theactor's executor but cleared it from
AsyncStream'sonTermination, which can run onany thread. The handler is now cleared with
deferon the actor after thefor awaitloop exits.
MockNetworkConnectionwas@unchecked Sendablewith unprotectedmutable state accessed from both the transport actor and
@MainActortasks. Its stateis now lock-protected, and handlers are snapshotted before being invoked.
Related upstream work in the same area: #283, #216, #213.
How Has This Been Tested?
swift testpasses. (A TSan run ofNetworkTransportTestscurrently aborts onmaininHeartbeatparsing — a misalignedload(as: UInt64.self)— which is unrelated to this change.)Breaking Changes
None.
Types of changes