I have some suspending code running inside `withCo...
# coroutines
r
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
@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
That's exactly what I ended up doing, but is there an explanation for the observed behavior?
l
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
Would you consider this a bug? Certainly seems so to me.
l
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.