Hmm, this code hangs forever for me using 1.3.30: ...
# coroutines
a
Hmm, this code hangs forever for me using 1.3.30:
Copy code
withTimeoutOrNull(100) {
    suspendCoroutine<Unit> { cont ->
        (context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)
            .hideSoftInputFromWindow(windowToken, 0, object : ResultReceiver(handler) {
                override fun onReceiveResult(resultCode: Int, resultData: Bundle?) {
                    cont.resume(Unit)
                }
            })
    }
}
l
Yes, that's expected as cancellation is cooperative. Use
suspenCancellableCoroutine
and unregister the receiver in a finally block, or in cont.invokeOnClose.
a
That’s not the issue. First of all you cannot unregister the receiver and secondly it shouldn’t matter as withTimeout should still continue and just let the other coroutine be, ignoring it when it (if ever) resumes
Actually, my bad. It was the issue. I’m pretty surprised that’s how it worked
Why does even
suspendCoroutine
exist, why not only have
suspendCancellableCoroutine
l
Because cancellation is part of kotlinx.coroutines, and because in some cases, your code is not cancellable (e.g. blocking code).
a
I thought the only difference was that you could perform more efficient cleanup using the latter
l
Try finally works for both, but for the non cancellable, it happens only if your code throws or is done as it will not receive the cancellation signal.