In my case, as soon as the handshaking is over, the keep-alive loop must be started. If not, the other side will eventually terminate the connection. If I design my connect function as a suspending one, then it is pretty clear - if it finishes normally (= without an exception), then the connection and handshaking are over, keepalive is running, communication can commence.
But I share your view about Job/Deferred
. I have been doing that because of that convention, but in a case like this, where there’s handshaking involved (which in turn involves suspending function), having to use them feels forced.
I’d also like to ping @nickallendev , since he helped me with a similar question before. In particular, I am referring to this quote:
Your suspend methods should not take a CoroutineScope and should wait for all work to finish. Methods that launch coroutines without waiting should not suspend and should take a CoroutineScope parameter or receiver.
This is the convention I was talking about. Wouldn’t my case be sort of a special one that warrants an exception to that convention?