Skip to content
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

Deadlock within Flow.onCompletion? #2669

Closed
peterfigure opened this issue Apr 22, 2021 · 3 comments
Closed

Deadlock within Flow.onCompletion? #2669

peterfigure opened this issue Apr 22, 2021 · 3 comments
Assignees
Labels

Comments

@peterfigure
Copy link

Coroutine "coroutine#30":StandaloneCoroutine{Active}@11501093, state: SUSPENDED
        at kotlinx.coroutines.channels.AbstractChannel.receiveOrClosed-WVj179g(AbstractChannel.kt:631)
        at kotlinx.coroutines.flow.FlowKt__ChannelsKt.emitAllImpl$FlowKt__ChannelsKt(Channels.kt:50)
        at kotlinx.coroutines.flow.internal.ChannelFlow$collect$2.invokeSuspend(ChannelFlow.kt:135)
        at kotlinx.coroutines.flow.FlowKt__ErrorsKt.catchImpl(Errors.kt:230)
        at kotlinx.coroutines.flow.FlowKt__ErrorsKt$catch$$inlined$unsafeFlow$1.collect(Errors.kt:113)
        at kotlinx.coroutines.flow.FlowKt__EmittersKt$onCompletion$$inlined$unsafeFlow$1.collect(Emitters.kt:114)

The coroutine is suspended there and never makes any progress. This happens in a backend application after a period of sustained load. I'm trying to distill a simplified example to illustrate the situation but no success yet.

@peterfigure
Copy link
Author

Just to add more colour, the 8 threads in the scope in which this flow was launch are in waiting:

stackTrace:
java.lang.Thread.State: WAITING (parking)
at jdk.internal.misc.Unsafe.park(java.base@11.0.11/Native Method)
- parking to wait for <0x0000000749396fa8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(java.base@11.0.11/LockSupport.java:194)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(java.base@11.0.11/AbstractQueuedSynchronizer.java:2081)
at java.util.concurrent.LinkedBlockingQueue.take(java.base@11.0.11/LinkedBlockingQueue.java:433)
at java.util.concurrent.ThreadPoolExecutor.getTask(java.base@11.0.11/ThreadPoolExecutor.java:1054)
at java.util.concurrent.ThreadPoolExecutor.runWorker(java.base@11.0.11/ThreadPoolExecutor.java:1114)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(java.base@11.0.11/ThreadPoolExecutor.java:628)
at java.lang.Thread.run(java.base@11.0.11/Thread.java:829)

@qwwdfsad
Copy link
Contributor

It seems like you have the following code somewhere: flow.onCompletion { emitAll(channel) } and the flow is cancelled, while the channel has no elements and is not closed.

As a temporary workaround, you can use flow.onCompletion { if (it != null) emitAll(channel) }

@qwwdfsad
Copy link
Contributor

Fixed in 1.5.0

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

No branches or pull requests

2 participants