You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Many already existing JVM APIs polls in a try {} catch {} block using InterruptException to prematurely wake themselves up. What is the correct way to handle those APIs when using coroutines (eventually using JVM IO dispatcher)?
A workaround could be use a timeout version of the same API, if available, and wait again in a cycle.
In my case the blocking call is NIO WatchService.take() which waits until it got something to return or the thread is interrupted and throws the exception. I could use WatchService.poll(5, TimeUnit.SECONDS) and cycle if nothing is returned or the job is cancelled using isActive, but it feels like... wrong.
The text was updated successfully, but these errors were encountered:
You are right, coroutine reduces the thread cost, interrupt it resumes this cost.
In short, for your use case, I strongly suggest you to consider non blocking NIO API.
However this does not look as a issue, so I propose you consider another platform, like https://discuss.kotlinlang.org/
We do not have a solution for this, the closest issue to your request is #57.
It is too dangerous to interrupt a thread by default: interrupts are delivered asynchronously (~racy for multiple interruptable calls) and a lot of code in the wild does not handle/propagate them properly.
and cycle if nothing is returned
Polling with a fixed interval is fine as a workaround, I can't say it is completely wrong.
If you have any thoughts which API for interruption would be useful for you, let's discuss it.
Many already existing JVM APIs polls in a
try {} catch {}
block usingInterruptException
to prematurely wake themselves up. What is the correct way to handle those APIs when using coroutines (eventually using JVMIO
dispatcher)?A workaround could be use a timeout version of the same API, if available, and wait again in a cycle.
In my case the blocking call is NIO
WatchService.take()
which waits until it got something to return or the thread is interrupted and throws the exception. I could useWatchService.poll(5, TimeUnit.SECONDS)
and cycle if nothing is returned or the job is cancelled usingisActive
, but it feels like... wrong.The text was updated successfully, but these errors were encountered: