Skip to content

kotlinx.coroutines.JobCancellationException: Job was cancelled; #2005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
msya opened this issue Feb 19, 2020 · 5 comments · Fixed by #2007
Closed

kotlinx.coroutines.JobCancellationException: Job was cancelled; #2005

msya opened this issue Feb 19, 2020 · 5 comments · Fixed by #2007

Comments

@msya
Copy link

msya commented Feb 19, 2020

Summary

I am getting a kotlinx.coroutines.JobCancellationException: Job was cancelled; job=StandaloneCoroutine{Cancelled} exception when I use the toFlow method on the Apollo client.

Description

This repo https://github.com/msya/ApolloAndroidPlayground has an example app that shows the problem. I have a repo that creates an Apollo client and uses the toFlow extension to get the Flow.

class Repo {

    fun getData(): Flow<Response<TestQuery.Data>> {
        val client = ApolloClient.builder().serverUrl("https://api.kivaws.org/graphql").build()
        return client.query(TestQuery.builder().build()).toFlow()
    }
}

I also have a ViewModel that launch a coroutines in the view model scope. In this coroutine, I am collecting from the Flow.

class MyViewModel: ViewModel() {

    private val repo = Repo()

    val scope = CoroutineScope(Dispatchers.Main)

    fun getData() {
        scope.launch {
            repo.getData()
                .collect {
                    println(it)
                }
        }
    }

    fun cancel() {
        scope.cancel()
    }

}

However, if I call cancel on the Job returned by the coroutine. I am getting the cancellation exception - kotlinx.coroutines.JobCancellationException: Job was cancelled; job=StandaloneCoroutine{Cancelled}. In the activity below, I am canceling the call explicitly on a button click.

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val model: MyViewModel by viewModels()

        make_request?.setOnClickListener {
            model.getData()
        }

        cancel?.setOnClickListener {
            model.cancel()
        }

    }
}

I suspect this problem is due to the clone method in the callbackFlow in the toFlow extension. If you remove the clone method, the exception does not occur.

Version
1.3.2

@martinbonnin
Copy link
Contributor

For some context, the clone was added to address this bug: #1986

@diegocunhawarren
Copy link

Hey @martinbonnin, looking the changes regarding this issue, the class ApolloSubscriptionCall has the same issue about emit when the channel is closed, I created a custom extension like yours and just fixed to me!

@CureleaAndrei
Copy link
Contributor

Hi, found the same issue in ApolloCall<>.await(). I think there should be a scope.isActive check. What do you think ?

@martinbonnin martinbonnin changed the title Job Cancellation Exception kotlinx.coroutines.JobCancellationException: Job was cancelled; Mar 29, 2021
@martinbonnin
Copy link
Contributor

@CureleaAndrei can you share your apollo version and the stacktrace?

@basithp
Copy link

basithp commented Aug 12, 2021

@CureleaAndrei Facing the same issue on ApolloCall<>.await(). was it fixed. I am using the version "2.5.9"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants