Thanks @DTS Engineer .
I now understand that the dispatch queue holds a strong reference to the closures, and those closures are only released once they run. If the queue is suspended and the closures never execute, those references are never released and the memory remains retained indefinitely.
In my case, I suspended the dispatch queue only to simulate certain scenarios - I don’t intend to suspend the queue. My main question was whether, after calling .cancel() on an NWConnection, I need to wait for confirmation from the OS that the pending I/O has actually been cancelled. Based on our discussion on the thread, my understanding is that I do not need to wait. Is that correct?
To restate the scenario:
If I call .cancel() on the NWConnection and then release my own reference to the dispatch queue (e.g., set it to nil), the NWConnection still holds a strong reference to that queue. So the queue won’t be released until the NWConnection itself completes its teardown.
My understanding is that .cancel() begins tearing down the NWConnection, and even if cancellation of the pending I/O is delayed, the NWConnection will eventually drop its reference to the queue as part of its cleanup, without waiting for any pending send/receive closures to execute. Since the closures themselves do not hold a strong reference back to the NWConnection, the teardown does not wait for those closures to run before releasing its reference to the queue. Right?
Topic:
App & System Services
SubTopic:
Networking
Tags: