Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Kotlin JobCancellationException?

Tags:

android

kotlin

I got a crash because of Kotlin JobCancellationException.

The following is the detail about the crash :

kotlinx.coroutines.JobCancellationException: Job was cancelled; job=SupervisorJobImpl{Cancelling}@131dbe3     

All I know is the SupervisorJobImpl is for ViewModelScope, and it will be called method cancel when ViewModel lifecycle is over.

I was so confused about the Exception because Kotlin coroutines will just ignore the Exception, but it was thrown and cause the App crash. If it has stack I can just figure out, but it doesn't, just tell me that the job was cancelled.

I spent about more than 3 days on the exception but just have no idea.

I saw the video : KotlinConf 2019: Coroutines! Gotta catch 'em all! by Florina Muntenescu & Manuel Vivo, I found if the scope is canceled, and if you call await on a Deferred, it will throw the Exception, but I found no await on the canceled scope.

So can someone just show me some code which perhaps causes the same exception and make the App crash? Thx, there.

like image 603
ParadiseHell Avatar asked May 04 '20 12:05

ParadiseHell


3 Answers

Finally, I found what causes the Exception and the issue address is flowing:

kotlin.coroutines.channels.awaitClose: JobCancellationException

Actually, awaitClose will not throw the JobCancellationException, because awaitClose is a cancellable suspended function. The offer method will throw JobCancellationException if the Job was canceled because offer is not a cancellable suspended function.

By the way, callbackFlow is an experimental API, so it may cause some bug, so when we use it, we need to be careful. Because it will not always ignore JobCancellationException when Job was canceled, and I don't think it's friendly to developers.

Now I have found 2 situations that will cause JobCancellationException so we need to try catch the exception.

  1. async await, when we call the await method we need to try catch. And you can find and example in the Video.

  2. callbackFlow offer, when we call the offer method we need to try catch. And you can find an example in the issue above.

like image 132
ParadiseHell Avatar answered Nov 04 '22 11:11

ParadiseHell


I know I am late but you can just check Job Status before offering objects. Like this

if(isActive) offer(Resource.success(response))

isActive is Coroutine Scope

like image 7
gauravmehra Avatar answered Nov 04 '22 11:11

gauravmehra


I just saw the same problem. The issue was caused by the activity being finished manually before the job managed to complete.

like image 3
K.Cvetanovska Avatar answered Nov 04 '22 12:11

K.Cvetanovska