From c68ca3376bc706d65a78d36e2d67aa616fb56ffb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20B=C3=B6ttger?= Date: Fri, 30 May 2025 21:11:04 +0200 Subject: [PATCH] Try to fix issue if connection state update handler called early -> aborts connection --- NearbyShare/NearbyConnection.swift | 33 ++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/NearbyShare/NearbyConnection.swift b/NearbyShare/NearbyConnection.swift index 2cf2463..86463f0 100644 --- a/NearbyShare/NearbyConnection.swift +++ b/NearbyShare/NearbyConnection.swift @@ -57,16 +57,20 @@ class NearbyConnection { self.connectionReady() self.receiveFrameAsync() } else if case let .failed(err) = state { - self.lastError = err - log("Error opening socket: \(err)") - - if err == .posix(.ENOTCONN) { - ConnectionFailureTracker.shared.recordFailure { - NearbyConnectionManager.shared.mainAppDelegate?.showFirewallAlert() + + // If connection reset by peer, it could still be a valid file transfer that has not been processed yet. => Wait + if err == .posix(.ECONNRESET) { + NearbyConnection.dispatchQueue.asyncAfter(deadline: .now() + 0.5) { + + // If already closed, ignore the error, as file transfer has been processed + if !self.connectionClosed { + recordErrorAndDisconnect(err: err) + } } } - - self.handleConnectionClosure() + else { + recordErrorAndDisconnect(err: err) + } } } else { @@ -75,6 +79,19 @@ class NearbyConnection { } // connection.start(queue: .global(qos: .utility)) connection.start(queue: NearbyConnection.dispatchQueue) + + func recordErrorAndDisconnect(err: NWError) { + self.lastError = err + log("Connection Error: \(err)") + + if err == .posix(.ENOTCONN) { + ConnectionFailureTracker.shared.recordFailure { + NearbyConnectionManager.shared.mainAppDelegate?.showFirewallAlert() + } + } + + self.handleConnectionClosure() + } } func connectionReady() {}