Peter Kievits
09/18/2023, 3:41 PMRiccardo Lippolis
09/18/2023, 3:47 PM@Blocking
annotation that can be placed on a method that should not be executed in a non-blocking context. It doesn't give any guarantees of course, but at least IntelliJ would give warnings when calling that method in a non-blocking context, and other static analysis tools could potentially catch it as well.Peter Kievits
09/18/2023, 3:47 PMPeter Kievits
09/18/2023, 3:47 PMPeter Kievits
09/18/2023, 4:07 PMRiccardo Lippolis
09/18/2023, 4:32 PMPossibly blocking call in non-blocking context
) is indeed active? (see screenshot)
also note that it is possible to mark a coroutine dispatcher as a BlockingExecutor, meaning that blocking code is allowed inside a coroutine using that executor (and I think the warning is also not generated for <http://Dispatchers.IO|Dispatchers.IO>
). Maybe you were using the IO dispatcher for your test?Peter Kievits
09/19/2023, 7:08 AMrunBlocking
Riccardo Lippolis
09/19/2023, 8:04 AMPeter Kievits
09/19/2023, 8:43 AMRiccardo Lippolis
09/19/2023, 4:14 PMrunBlocking
without specifying a Dispatcher, the coroutine runs single-threaded on the calling thread. So any blocking call you do inside the coroutine would only block the calling thread (like it would when calling the blocking method without using runBlocking
.
However, when you do specify a Dispatcher, like Dispatchers.Default
in my example above, calling blocking code from that coroutine would lead to blocking a thread that's part of a pool of threads also used for other coroutines, which is a general no-no in coroutine-world... so in that case the warning is justified.Peter Kievits
09/20/2023, 7:28 AMPeter Kievits
09/20/2023, 7:28 AMPeter Kievits
09/20/2023, 7:31 AMsuspend
Peter Kievits
09/20/2023, 7:50 AMDispatchers.Default
Peter Kievits
09/20/2023, 7:50 AMRiccardo Lippolis
09/20/2023, 8:02 AM