Mark function as blocking

Hi,
Is there a way to mark some function as blocking - so that the IDE will warn about using it inside suspending code?

I already know that if I am writing something like the following:

suspend fun example() {
  Thread.sleep(1000)
}

then the IDE will warn me about “inappropriate blocking method call” because I am calling to the blocking method “sleep” in a suspending function.

what I am looking for is for a way to mark other methods as blocking, something like:

@Blocking
fun sleep1000() { Thread.sleep(1000) }
suspend fun nonBlocking() {
    sleep1000() //here the ide will warn me about inappropriate usage 
}

I did noticed that if I am marking the method as throwing InterruptedException Ill get the required behavior:

@Throws(InterruptedException::class)
fun sleep1000() { Thread.sleep(1000) }
suspend fun nonBlocking() {
    sleep1000() //here the ide does warn me about inappropriate usage 
}

But it seems more like a hack that takes advantage of the current implementation of this feature in Idea…

2 Likes

Yes, the set of blocking annotations may be added to the inspection settings.

Get the yellow on Thread.sleep(1000). Alt+Enter to get the quick fix menu. Arrow right to get sub-menu. Choose “Edit inspection profile settings”. See the box “Blocking Annotations”. Add an annotation there.

NB: The annotation FQNs are added as plain text here, i.e. they are not updated on Refactor / Rename.

1 Like