-
Notifications
You must be signed in to change notification settings - Fork 160
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
Fixed Coroutine Cancellation Exception for concurrent close and offer #93
Fixed Coroutine Cancellation Exception for concurrent close and offer #93
Conversation
What was the error you were encounting on which platform? |
Its a complicated stack, but short summary: Preface:
PseudoCode:
Launching this crashes the app, because when logged out on init it immediately navigates away, closing the coroutineScope before the observable has its initial value. Offer is then fired despite the coroutine being cancelled, because the listener is always called when assigned. Its an edgecase of a concurrency issue, but it can and will happen. Jut not offering after the scope is cancelled solves the issue |
We use this code in production and its never happened I suspect it's a iOS only thing. Coroutines on ios are being majorly overhauled so maybe this problem will go away at some point. Are you able to write a test to reproduce the issue? Maybe we just implement this workaround for iOS only for now? Another thing I have noticed though is that we should probably be using sendBlocking instead of offer in the callbackFlows - this is unrelated to this issue but this would be a good time to make the switch. |
I managed to reproduce it in Android as well:
Crashes with
|
Do you have a full stack trace for that exception? |
Nope, thats all I'm getting, most likely because it crashes within the coroutine. I found the source by commenting out the code until I found the source at offer. I also found this thread talking about similar issues: Kotlin/kotlinx.coroutines#1762 in addition, I would suggest it makes perfect sense this crashes:
Here, So I understand why you dont run into it on production: unless you hit the edgecase it wont be noticable. But when you do, it breaks. In any case, I dont see the harm in this change, since even if offer would just ignore the value when the coroutine is cancelled, this check just causes the same behaviour. |
Interesting, following the link you posted I got to Kotlin/kotlinx.coroutines#974 Looks like they will address this in the future but that thread mentioned it could still happen even using isClosedForSend. Maybe we should surround them all with try catch? |
used the try catch approach instead. One thing I am slightly unsure about is whether the listeners are properly unsubscribed in these cases, but at least this will prevent a crash |
2bc1165
to
9b237c2
Compare
No description provided.