question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

withContext(IO) blocks thread

See original GitHub issue

Currently we are using Vert.x web for HTTP server, and uses JedisPool for Redis client. Since that Redis client has a blocking API, we used withContext(IO) to not block the Vert.x thread.

suspend fun <R> JedisPool.useAsync(block: (Jedis) -> R) = withContext(IO) {
    this.resource.use { block(it) }
}

But the Vert.x thread is blocked by withContext. Coroutines version is “0.25.0”, Vertx version is “3.5.3”.

Aug 31, 2018 3:24:53 AM io.vertx.core.impl.BlockedThreadChecker
WARNING: Thread Thread[vert.x-eventloop-thread-0,5,main] has been blocked for 39234941 ms, time limit is 2000
io.vertx.core.VertxException: Thread blocked
	at kotlinx.coroutines.experimental.scheduling.CoroutineScheduler.parkedWorkersStackPop(CoroutineScheduler.kt:982)
	at kotlinx.coroutines.experimental.scheduling.CoroutineScheduler.tryUnpark(CoroutineScheduler.kt:395)
	at kotlinx.coroutines.experimental.scheduling.CoroutineScheduler.requestCpuWorker(CoroutineScheduler.kt:366)
	at kotlinx.coroutines.experimental.scheduling.CoroutineScheduler.dispatch(CoroutineScheduler.kt:345)
	at kotlinx.coroutines.experimental.scheduling.ExperimentalCoroutineDispatcher.dispatchWithContext$kotlinx_coroutines_core(ExperimentalCoroutineDispatcher.kt:83)
	at kotlinx.coroutines.experimental.scheduling.LimitingDispatcher.dispatch(ExperimentalCoroutineDispatcher.kt:123)
	at kotlinx.coroutines.experimental.scheduling.LimitingDispatcher.dispatch(ExperimentalCoroutineDispatcher.kt:113)
	at kotlinx.coroutines.experimental.DispatchedKt.resumeCancellable(Dispatched.kt:189)
	at kotlinx.coroutines.experimental.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:16)
	at kotlinx.coroutines.experimental.CoroutineStart.invoke(CoroutineStart.kt:83)
	at kotlinx.coroutines.experimental.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:136)
	at kotlinx.coroutines.experimental.BuildersKt.withContext(Unknown Source)
	at kotlinx.coroutines.experimental.BuildersKt__Builders_commonKt.withContext$default(Builders.common.kt:107)
	at kotlinx.coroutines.experimental.BuildersKt.withContext$default(Unknown Source)
...
	at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:42)
	at kotlinx.coroutines.experimental.DispatchedTask$DefaultImpls.run(Dispatched.kt:149)
	at kotlinx.coroutines.experimental.DispatchedContinuation.run(Dispatched.kt:13)
	at io.vertx.kotlin.coroutines.VertxCoroutineExecutor$execute$1.handle(VertxCoroutine.kt:408)
	at io.vertx.kotlin.coroutines.VertxCoroutineExecutor$execute$1.handle(VertxCoroutine.kt:404)
	at io.vertx.core.impl.ContextImpl.lambda$wrapTask$2(ContextImpl.java:339)
	at io.vertx.core.impl.ContextImpl$$Lambda$33/1483526283.run(Unknown Source)
	at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
	at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:463)
	at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:886)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.lang.Thread.run(Thread.java:745)

Where the ... is 3 lines of our code.

I would love to understand things under the hood but currently we will just use the non-blocking Redis client to avoid the problem first.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
elizarovcommented, Sep 9, 2018

@objcode We are cleaning up and finalizing API for 1.0 release of this library, so I’d suggest to also watch for issues with “for 1.0 release” label: https://github.com/Kotlin/kotlinx.coroutines/issues?q=is%3Aissue+is%3Aopen+label%3A"for+1.0+release"

0reactions
LouisCADcommented, Sep 11, 2018

Shouldn’t this issue have its labels updated? (like having the bug label in place of the current one)

Read more comments on GitHub >

github_iconTop Results From Across the Web

withContext(Dispatchers.IO) infinitely blocking after running
My guess would be that that the app is crashing and you don't realize it. Double check logcat. You cannot set a live...
Read more >
Blocking threads, suspending coroutines | by Roman Elizarov
The other way to block a thread is by using blocking IO (aka IO-bound task) ... Using withContext is not the only way...
Read more >
Improve app performance with Kotlin coroutines
Inside the body of get , call withContext(Dispatchers.IO) to create a block that runs on the IO thread pool. Any code you put...
Read more >
Kotlin Coroutines: A Detailed Introduction - Appcircle Blog
runBlocking : Lets you start a new coroutine. It blocks the main thread until the task is done. println("Run Blocking Start") ...
Read more >
"Inappropriate blocking method call" with coroutines and ...
IO dedicated to such calls, so doesn't block main thread (or calling coroutine). withContext(Dispatchers.IO) behaves similar as calling delay() ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found