-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
ProduceChannel never complete cancelling
state in MainScope after upgrade coroutines to 1.4.3
#2717
Comments
A fix is rather simple. Just close scope after cleaning the channel: fun close() {
channel.cancel()
while (!channel.isClosedForReceive) {
//cleanup
}
cancel()
} And I would agree the problem was in our code. However, I am expecting scope will completely cancel all ReceiveChannels |
The problem is indeed |
That helps to pro-actively catch cases like #2717 and to report such exception in even more robust manner
The actual problem is not the test. It is a cancellation of the scope in the bad time leave stuck producer in And the problem appeared only after upgrading 1.4.2 -> 1.4.3. So the bug could be sneaky. |
The attached test does not reproduce this problem or I've misunderstood the general idea, the test passes correctly and nothing is stuck. Could you please re-visit the reproducer and post it in a self-contained manner (so, if copypasted in IDEA and run, it hangs)? |
I believe I could reproduce it in isolated app. But before spend effort I just want clarify one thing. If a producer is suspended, should fun close() {
scope.cancel()
while (!channel.isClosedForReceive) {
}
} Also, is any chance that commit 089e163 will fix product problem as well? |
Yes, as long as the producer ain't doing anything suspicious such as catching cancellation and busy-waiting/blocking
No.
Replace
Nope, it's irrelevant to the problem you're describing and rather prevents using |
🤦 Big shame on me. The main thread was busy waiting for completion in an infinite loop, while the main thread is also was responsible to handle the completion of There are more potential solutions: use different threads, coroutine's |
That helps to pro-actively catch cases like #2717 and to report such exception in even more robust manner
That helps to pro-actively catch cases like Kotlin#2717 and to report such exception in even more robust manner
Our app stops working after upgrade coroutines to 1.4.3. Any older version works just fine. After spending countless hours of debugging, I still do not understand the problem, and I can't reproduce the same conditions in the tests, but at least I got something similar.
We have an advertising service which has the responsibility of releasing any loaded ads after closing. It is a mandatory requirement to avoid memory leaks.
Simplified version of the code looks like. Please read a comments which represents real world:
You could try use the following test:
However, inside unit tests, the ProduceChannel does not print any lines at all, but cancellation stuck forever. After changing MainScope to
CoroutineScope(Dispatchers.IO)
cancellation works just fine in the tests. I believe the problem is related to the Main dispatcher.My best guess it could be related to 7061cc2, but I am not sure how it could cause such behavior.
BTW, the code is very old and was written before Flow. Maybe should we rewrite it to Flow? What is the best approach to preload ads in advance and make sure everything is cleaned?
The text was updated successfully, but these errors were encountered: