From d37df62da99b831fa53d0197dbdea75d4b1991bd Mon Sep 17 00:00:00 2001 From: Aidan Dysart Date: Wed, 6 Aug 2025 12:47:55 -0700 Subject: [PATCH] Fix memory leak or URLSession in SocketEngine `SocketEngine` retains an instance of `URLSession`, and this value is replaced in `resetEngine()`. But this instance of `URLSession` is leaked unless you call `invalidateAndCancel()`, which breaks the references between the session and its delegate and callbacks. Note in contrast to other types in Foundation and UIKit, `URLSession` _strongly_ retains its delegate. So the default configuration of `SocketEngine` still has a retain cycle since it sets itself as the delegate of the `URLSession` that it owns. Breaking this retain cycle requires user of the library to provide a custom `URLSessionDelegate` during configuration. --- Source/SocketIO/Engine/SocketEngine.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Source/SocketIO/Engine/SocketEngine.swift b/Source/SocketIO/Engine/SocketEngine.swift index 1d1c071d..2fabd95c 100644 --- a/Source/SocketIO/Engine/SocketEngine.swift +++ b/Source/SocketIO/Engine/SocketEngine.swift @@ -563,6 +563,7 @@ open class SocketEngine: NSObject, WebSocketDelegate, URLSessionDelegate, polling = true probing = false invalidated = false + session?.invalidateAndCancel() session = Foundation.URLSession(configuration: .default, delegate: sessionDelegate, delegateQueue: queue) sid = "" waitingForPoll = false