I'm trying to track down the source of a `JobCance...
# coroutines
t
I'm trying to track down the source of a
JobCancellationException
. My crash reporting tool isn't offering any useful information, and I don't quite understand this exception..
Copy code
kotlinx.coroutines.JobCancellationException · Job was cancelled
unknown fileunknown method
Looking at the breadcrumbs, it looks like this occurs after
MainActivity.onDestroy()
is called in my Android app. So presumably, someone is swiping the app away from recents (potentially killing the app process)
There's a coroutine that runs when the app is launched, which I suspect is the source. The scope used to launch this coroutine is:
Copy code
CoroutineScope(Dispatchers.Main + job + coroutineExceptionHandler)
Where:
job
is a
SupervisorJob
, and
coroutineExceptionHandler
is an instance of
CoroutineExceptionHandler
that logs errors
I'm not sure if I'm looking in the wrong place, or the app is throwing an exception that is swallowed by coroutines, or the 'crash' is simply that the job is being cancelled. Any insight would be great, I'm still wrapping my head around JobCancellationExceptions and CoroutineExceptionHandlers
e
Turning on debug mode should help, because it enables stacktrace in JCE
t
Thanks for the quick reply. The issue here is, I'm not able to reproduce this crash myself
Oh, I wonder if I can ship the app (it's only in alpha) with debug mode on.. Would that be crazy? Just had a read of https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/debugging.md Thanks
l
@Tim Malseed Are you using
Channel
and the
offer
function?
t
I am in some places, yes
l
@Tim Malseed Then it's quite likely you have these exceptions thrown here either: • because you're using
consume
,
consumeEach
or
consumeAsFlow()
when, respectively, you should not, when you should use a plain
for
loop or
receive()
, or when you should use
receiveAsFlow()
• because you have a race condition, for example in `callbackFlow`/`channelFlow` or elsewhere, which is this issue: https://github.com/Kotlin/kotlinx.coroutines/issues/974 • because you
close
or
cancel
the channel in a way or another, where a
JobCancellationException
is passed as a cause.
t
Thanks for the info