pusher / NWWebSocket

A WebSocket client written in Swift, using the Network framework from Apple.
MIT License
123 stars 25 forks source link

Fix dispatch work item retain cycle #38

Closed choulepoka closed 1 year ago

choulepoka commented 1 year ago

Problem: The dispatch work item created create a strong reference to self, which in turn have a strong reference on disconnectionWorkItem, creating an infinite retain cycle that makes the NWWebSocket an immortal object

This is a major memory leak for apps that try arbitrarily disconnect from the websocket (i.e.: a logout) and then reconnect later (i.e.: a login)

This cause multiple socket to be alive, and taking up a sizeable amount of system resources

Bug was introduced by commit 45eafe24fb1c9be1ab09f9c7fa97c30ba7aab09c

Solution Add the [weak self] in clause in the dispatch work item declaration, and add a guard let self else { return } for the case where self is truly deallocated.

The solution has been tested, and resolved the memory leak with no adverse side-effect

choulepoka commented 1 year ago

@benjamin-tang-pusher Any news on this pull-request? This a quite serious bug.

benjamin-tang-pusher commented 1 year ago

Hi, thanks for this, I will test it and get it merged.

benjamin-tang-pusher commented 1 year ago

Hi @fbenevides, I've tested this and its good to go. Could you release this as 0.5.3 ? pusher-websocket-swift will pin this to the latest version.