question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

kotlinx.coroutines.JobCancellationException: FlowSubscription was cancelled

See original GitHub issue

I recently migrated a class that uses https://github.com/f2prateek/rx-preferences to now use FlowPreferences. In our app, when the user logs out, we clear all SharedPreferences. During the logout, the app is crashing with the following exception:

kotlinx.coroutines.JobCancellationException: FlowSubscription was cancelled; job=FlowSubscription{Cancelled}@bed4c69

There isn’t really any more details regarding the stack trace. I suspect I am running into https://github.com/Kotlin/kotlinx.coroutines/issues/974 which may make sense given FlowSharedPreferences.keyFlow is implemented as such:

  val keyFlow = callbackFlow {
    val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, key -> offer(key) }
    sharedPreferences.registerOnSharedPreferenceChangeListener(listener)
    awaitClose { sharedPreferences.unregisterOnSharedPreferenceChangeListener(listener) }
  }

Something tells me that the offer(key) is being performed after the Channel has been closed.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
tfcporciunculacommented, May 18, 2021

Now that the underlying coroutines issue is fixed, I’m reverting the fix we have for this in favor of the new trySend() method from 1.5.0. Everything should continue to work just fine, but I thought I’d mention this here just in case.

1reaction
tfcporciunculacommented, May 18, 2020

Thanks for letting me know @mhernand40. I’ll keep this issue open since it might be relevant for other people using the library and will close it once there’s an actual fix in place.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix Kotlin JobCancellationException? - Stack Overflow
The offer method will throw JobCancellationException if the Job was canceled because offer is not a cancellable suspended function.
Read more >
JobCancellationException - kotlinx-coroutines-core
Thrown by cancellable suspending functions if the Job of the coroutine is cancelled or completed without cause, or with a cause or exception...
Read more >
Diff - platform/external/kotlinx.coroutines - Google Git
+ * `JobCancellationException` is introduced as a specific subclass of `CancellationException` which is + used for coroutines that are cancelled without ...
Read more >
Deadlock when cancelling job which started @Transaction ...
Issue emerges, while cancelling Job (Coroutines) which called the function. ... Then, if we would cancel this Job immediately after starting it, deadlock...
Read more >
StandaloneCoroutine was cancelled in go to files : FL-12960
StandaloneCoroutine was cancelled kotlinx.coroutines.JobCancellationException: StandaloneCoroutine was cancelled at ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found