Skip to content
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

Trigger InterruptException inside a job #861

Closed
lamba92 opened this issue Nov 28, 2018 · 3 comments
Closed

Trigger InterruptException inside a job #861

lamba92 opened this issue Nov 28, 2018 · 3 comments
Labels

Comments

@lamba92
Copy link

lamba92 commented Nov 28, 2018

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.

@fvasco
Copy link
Contributor

fvasco commented Nov 28, 2018

but it feels like... wrong

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/

@qwwdfsad
Copy link
Member

qwwdfsad commented Nov 30, 2018

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.

@lamba92
Copy link
Author

lamba92 commented Nov 30, 2018

I agree that interrupting may not be a good practise in most cases. As @fvasco recommended I continued the discussion here.

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

No branches or pull requests

3 participants