https://kotlinlang.org logo
Title
r

Rohan Maity

03/03/2023, 10:06 AM
Hi, my coroutine job immediately gets cancelled even before staring. Now I am not sure what could be reasons for this. What could be reasons for coroutine to get cancelled even before it is cancelled
private fun CoroutineScope?.launchWithNullCheck(
        block: suspend () -> Unit
    ) {
        this?.let {
            Log.d("FUI", "Scope not null")
            val job = launch {
                Log.d("FUI", "executing block not null")
                block()
            }
            Log.d("FUI", "job isActive ${job.isActive}, isCompleted: ${job.isCompleted}, isCancelled: ${job.isCancelled}")
        }
}
s

Sam

03/03/2023, 10:10 AM
One possible reason is if the
CoroutineScope
that you’re launching it in contains an already-cancelled
Job
. Launching a job as a child of a cancelled job will immediately return a cancelled job and won’t start the coroutine. Hard to say if that’s what’s happening here, though. Can you give any more information about how it’s behaving? Do you see any output, for example?
r

Rohan Maity

03/03/2023, 10:11 AM
Launching a job as a child of a cancelled job will immediately return a cancelled job
Let me verify this and give more information
Is there any way to see any cancelled job for the particular scope?
s

Sam

03/03/2023, 10:19 AM
this?.coroutineContext?.job
should work (where
this
is the coroutine scope receiver from your example)
r

Rohan Maity

03/03/2023, 10:24 AM
I printed out whether scope is active or not and existing job is Cancelled or not
private fun CoroutineScope?.launchWithNullCheck(
        block: suspend () -> Unit
    ) {
        this?.let {
            Log.d("FUI", "scope isActive: ${this.isActive}")
            Log.d("FUI", "existing job cancelled: ${coroutineContext.job.isCancelled}")
            Log.d("FUI", "Scope not null")
            val job = launch {
                Log.d("FUI", "executing block not null")
                block()
            }
            Log.d("FUI", "job isActive ${job.isActive}, isCompleted: ${job.isCompleted}, isCancelled: ${job.isCancelled}")
        }
}
For me it says job was not cancelled but scope became inactive
Screenshot 2023-03-03 at 15.54.33.png
s

Sam

03/03/2023, 10:26 AM
Okay, that explains why the job does not start. The scope’s job is already finished, so it can’t launch new child jobs. If it’s not active, but not cancelled, that means it just completed normally. How did you create this scope?
r

Rohan Maity

03/03/2023, 10:28 AM
I passed this scope from another class android's viewmodel This is actually
viewmodelscope
and I am holding reference to that in this class
s

Sam

03/03/2023, 10:29 AM
In that case I guess it just means that the associated
ViewModel
has already been cleared?
r

Rohan Maity

03/03/2023, 10:29 AM
Thats the issue, Its not cleared. This happens while the viewmodel is active
s

Sam

03/03/2023, 10:32 AM
Okay, I haven’t used
ViewModelScope
myself so I’m not sure know what could cause it to be inactive. Hopefully someone with more Android knowledge can help 🤞
r

Rohan Maity

03/08/2023, 10:32 AM
I figured it out. Instead of passing
viewModelScope
I was passing its childScope which caused the issue ChildScope was getting completed before my further operations Passing viewModelScope solved the issue
Although while figuring this issue, I found out its best to manage my the coroutineScope for the class (which was receiving coroutine) on its own instead of receiving from another class