Skip to content

DefaultDispatcher-worker-xx thread state :TIMED_WAITING (parking) #1697

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

Closed
yunipingan opened this issue Dec 11, 2019 · 5 comments
Closed
Assignees

Comments

@yunipingan
Copy link

mutillevel coroutines and a lot of requests cause tomcat threads blocking, donnot know the reason, please give a solvment.

this is my code

    @GetMapping("/test")
    fun test() {
        runBlocking {
            println(Thread.currentThread().name)
            async(Dispatchers.Default) {
                println(Thread.currentThread().name)
                runBlocking() {
                    println(Thread.currentThread().name)
                    async(Dispatchers.Default) {
                        println(Thread.currentThread().name)
                    }
                }
            }
        }
    }

deploy and run tomcat , run a single request is normal, but run 10 concurrent requests, the tomcat is always waiting
this is output

http-nio-8866-exec-7
http-nio-8866-exec-2
http-nio-8866-exec-6
http-nio-8866-exec-1
http-nio-8866-exec-5
http-nio-8866-exec-10
http-nio-8866-exec-8
http-nio-8866-exec-9
http-nio-8866-exec-4
http-nio-8866-exec-3
DefaultDispatcher-worker-4
DefaultDispatcher-worker-2
DefaultDispatcher-worker-1
DefaultDispatcher-worker-3
DefaultDispatcher-worker-2
DefaultDispatcher-worker-4
DefaultDispatcher-worker-3
DefaultDispatcher-worker-1

jstack message

"DefaultDispatcher-worker-4" #39 daemon prio=5 os_prio=31 tid=0x00007fd4ca455000 nid=0x9803 waiting on condition [0x0000700009bff000]
   java.lang.Thread.State: TIMED_WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x0000000798a06340> (a kotlinx.coroutines.BlockingCoroutine)
	at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
	at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:82)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:54)
	at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:36)
	at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
	at com.example.web.DemoController$test3$1$1.invokeSuspend(DemoController.kt:98)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:241)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:740)

"DefaultDispatcher-worker-3" #38 daemon prio=5 os_prio=31 tid=0x00007fd4cb399800 nid=0x9a03 waiting on condition [0x0000700009afc000]
   java.lang.Thread.State: TIMED_WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x0000000798b45b60> (a kotlinx.coroutines.BlockingCoroutine)
	at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
	at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:82)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:54)
	at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:36)
	at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
	at com.example.web.DemoController$test3$1$1.invokeSuspend(DemoController.kt:98)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:241)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:740)

"DefaultDispatcher-worker-2" #37 daemon prio=5 os_prio=31 tid=0x00007fd4ca7e3800 nid=0x6703 waiting on condition [0x00007000099f9000]
   java.lang.Thread.State: TIMED_WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000007988b82e0> (a kotlinx.coroutines.BlockingCoroutine)
	at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
	at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:82)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:54)
	at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:36)
	at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
	at com.example.web.DemoController$test3$1$1.invokeSuspend(DemoController.kt:98)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:241)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:740)

"DefaultDispatcher-worker-1" #36 daemon prio=5 os_prio=31 tid=0x00007fd4cb389800 nid=0x6503 waiting on condition [0x00007000098f6000]
   java.lang.Thread.State: TIMED_WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x000000079877eb50> (a kotlinx.coroutines.BlockingCoroutine)
	at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
	at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:82)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:54)
	at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:36)
	at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
	at com.example.web.DemoController$test3$1$1.invokeSuspend(DemoController.kt:98)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:241)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:740)
@yunipingan
Copy link
Author

yunipingan commented Dec 11, 2019

kotlinx-coroutines-core version

<dependency>
    <groupId>org.jetbrains.kotlinx</groupId>
    <artifactId>kotlinx-coroutines-core</artifactId>
    <version>1.3.2-1.3.60</version>
</dependency>

@fvasco
Copy link
Contributor

fvasco commented Dec 11, 2019

Do not use runBlocking inside a launch or async.
In this case the Dispatchers.Default is fully blocked.

Please read carefully the documentation
https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/run-blocking.html

@qwwdfsad
Copy link
Collaborator

@fvasco recommendation is correct, closing the issue

@fvasco
Copy link
Contributor

fvasco commented Mar 16, 2020

As suggested by @qwwdfsad,
I copy-paste #1861

I found a trouble in the follow code:

import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

object A {
    var ok = false

    init {
        println(1)
        runBlocking { }
        println(2)
        ok = true
        println(3)
    }

}

fun main() {
    runBlocking {
        launch { println(A.ok) }
        launch { println(A.ok) }
    }
}

I expect:

1
2
3
true
true

but it is:

1
false
2
3
true

openjdk version "11.0.6" 2020-01-14
kotlin 1.3.70
coroutine 1.3.4

Using GlobalScope.futute(Dispatchers.Unconfined) { }.get() is a workaround.

My use case does not result to a TIMED_WAITING or a deadlock, it works always but expose an object in an uninitialized state: ok cannot be false (second row in the response).

@iseki0
Copy link

iseki0 commented Aug 8, 2023

Why it's not a bug? The runBlocking will be returned in future. @qwwdfsad

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

No branches or pull requests

4 participants