kotlinx.coroutines.JobCancellationException: FlowSubscription was cancelled
See original GitHub issueI 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:
- Created 4 years ago
- Comments:9 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.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.