pusher / NWWebSocket

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

Retain cycle when trying to disconnect #33

Closed slice closed 1 year ago

slice commented 2 years ago

I'm trying to call NWWebSocket.disconnect() in my app, but the WebSocket never appears to actually get closed.

If I pull up a memory graph, there's a retain cycle:

image

Xcode says that a strong capture in the closure in NWWebSocket.scheduleDisconnectionReporting is retaining the object:

    private func scheduleDisconnectionReporting(closeCode: NWProtocolWebSocket.CloseCode,
                                                reason: Data?) {
        // Cancel any existing `disconnectionWorkItem` that was set first
        disconnectionWorkItem?.cancel()

        disconnectionWorkItem = DispatchWorkItem {
            self.delegate?.webSocketDidDisconnect(connection: self,
                                                  closeCode: closeCode,
                                                  reason: reason)
        }
    }

The webSocketDidDisconnect delegate method does get called (with a close code of 1001), but the WebSocket stays connected.

benjamin-tang-pusher commented 2 years ago

Hi, I couldn't spot a retain cycle in my app after calling disconnect:

Screenshot 2021-11-18 at 17 46 14

I don't see a the purple exclamation on my NWWebSocket, although please correct me if I'm missing something.

If I'm not, I'd like to see more of your code if possible.

slice commented 2 years ago

Sure thing. I was using NWWebSocket in my code here.

Since you can't seem to reproduce the retain cycle, I'm going to assume that I was doing something incorrectly, and Xcode was unable to display the root problem in my code via the memory graph. Feel free to close this issue in that case.

slice commented 2 years ago

[A bit later...]

image

https://developer.apple.com/documentation/macos-release-notes/macos-13-release-notes

Looks like this could be relevant. Not sure if the issue is resolved in the betas; I don't install them on my Macs and I'm no longer using NWWebSocket. But I just thought I'd bring this up.

benjamin-tang-pusher commented 1 year ago

Thanks for that.

I will close this, since this can't be reproduced.