https://kotlinlang.org logo
Title
r

rocketraman

01/05/2019, 3:19 PM
I have some suspending code running inside
withContext
that never completes. If I remove the
withContext
it does run to completion. The problem seems to be my use inside the
withContext
block of
async(SupervisorJob(coroutineContext[Job])) { ... }
as described in https://github.com/Kotlin/kotlinx.coroutines/issues/763 -- if I change the call to be a straight
async
call, then it works fine. Either this is a bug, or I'm not understanding something...
Using
withContext(IO + SupervisorJob(coroutineContext[Job])) { ... }
with regular
async
calls inside the block also hangs.
Using
DebugProbes.printScope(this)
shows a bunch of active
SupervisorJobImpl
. I'm not sure why these would be still be active... the underlying async calls have been awaited and values retrieved with no issues.
l

louiscad

01/06/2019, 12:17 AM
@rocketraman Replace the supervisor with a local
coroutineScope { ... }
wrapping the
async
calls that may fail recoverably, and handle the throwables outside of the local
coroutineScope { ... }
r

rocketraman

01/06/2019, 12:22 AM
That's exactly what I ended up doing, but is there an explanation for the observed behavior?
l

louiscad

01/06/2019, 12:30 AM
I think because of its context + its content withContext stays suspended as the job is likely staying active (since the parent job comes from outer context and supervisor is not cancelled by children)
r

rocketraman

01/07/2019, 2:05 PM
Would you consider this a bug? Certainly seems so to me.
l

louiscad

01/07/2019, 6:01 PM
I consider it a misusage, and you corrected it yourself. However you could request an IDE inspection on kotl.in/issue that detects this and suggests to use a local
coroutineScope
instead.