From b258a937501e5853d31574b4edafd594da4ccf04 Mon Sep 17 00:00:00 2001 From: Erasov Ivan Date: Thu, 7 Jun 2018 13:13:44 +0300 Subject: [PATCH 1/2] Fixed loosing POST packets on web socket connection failure and engine memory leak on server errors. --- Source/SocketIO/Engine/SocketEngine.swift | 7 +++---- Source/SocketIO/Manager/SocketManager.swift | 1 + 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Source/SocketIO/Engine/SocketEngine.swift b/Source/SocketIO/Engine/SocketEngine.swift index 4904ee5..0000de2 100644 --- a/Source/SocketIO/Engine/SocketEngine.swift +++ b/Source/SocketIO/Engine/SocketEngine.swift @@ -371,6 +371,9 @@ open class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePollable, So fastUpgrade = false probing = false flushProbeWait() + if postWait.count != 0 { + flushWaitingForPostToWebSocket() + } } private func flushProbeWait() { @@ -381,10 +384,6 @@ open class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePollable, So } probeWait.removeAll(keepingCapacity: false) - - if postWait.count != 0 { - flushWaitingForPostToWebSocket() - } } /// Causes any packets that were waiting for POSTing to be sent through the WebSocket. This happens because when diff --git a/Source/SocketIO/Manager/SocketManager.swift b/Source/SocketIO/Manager/SocketManager.swift index 7434ec7..571bf8d 100644 --- a/Source/SocketIO/Manager/SocketManager.swift +++ b/Source/SocketIO/Manager/SocketManager.swift @@ -169,6 +169,7 @@ open class SocketManager : NSObject, SocketManagerSpec, SocketParsable, SocketDa engine?.engineQueue.sync { self.engine?.client = nil + self.engine?.disconnect(reason: "Adding new engine") } engine = SocketEngine(client: self, url: socketURL, config: config) From feb476c76bd9dcf67dde754bf27c0073d7259366 Mon Sep 17 00:00:00 2001 From: Erasov Ivan Date: Thu, 7 Jun 2018 13:42:13 +0300 Subject: [PATCH 2/2] Added comments to document changes reasons --- Source/SocketIO/Engine/SocketEngine.swift | 6 +++++- Source/SocketIO/Manager/SocketManager.swift | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Source/SocketIO/Engine/SocketEngine.swift b/Source/SocketIO/Engine/SocketEngine.swift index 0000de2..bca0944 100644 --- a/Source/SocketIO/Engine/SocketEngine.swift +++ b/Source/SocketIO/Engine/SocketEngine.swift @@ -359,6 +359,7 @@ open class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePollable, So /// /// **You shouldn't call this directly** open func doFastUpgrade() { + if waitingForPoll { DefaultSocketLogger.Logger.error("Outstanding poll when switched to WebSockets," + "we'll probably disconnect soon. You should report this.", type: SocketEngine.logType) @@ -371,7 +372,10 @@ open class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePollable, So fastUpgrade = false probing = false flushProbeWait() - if postWait.count != 0 { + + // Need to flush postWait to socket since it connected successfully + // (moved from flushProbeWait() since it is also called on connected failure) + if !postWait.isEmpty { flushWaitingForPostToWebSocket() } } diff --git a/Source/SocketIO/Manager/SocketManager.swift b/Source/SocketIO/Manager/SocketManager.swift index 571bf8d..830bf93 100644 --- a/Source/SocketIO/Manager/SocketManager.swift +++ b/Source/SocketIO/Manager/SocketManager.swift @@ -168,7 +168,10 @@ open class SocketManager : NSObject, SocketManagerSpec, SocketParsable, SocketDa DefaultSocketLogger.Logger.log("Adding engine", type: SocketManager.logType) engine?.engineQueue.sync { + self.engine?.client = nil + + // Close old engine so it will not leak because of URLSession if in polling mode self.engine?.disconnect(reason: "Adding new engine") }