Things that happen are for instance `Thread.sleep(...
# announcements
l
Things that happen are for instance
Thread.sleep(1000000)
. I wanted to interrupt this Co-routine, but it's impossible
m
Could you explain me your use case?
m
Use
delay
instead.
Thread.sleep
blocks the thread.
👍 1
delay
suspends the coroutine, so it will "react" on a cancellation.
l
It's a library, I don't know what the user will be using, and I need to interrupt it
It's a testing library, that will run arbitrary code but must interrupt it if it's over a certain timeout
I can't know if user's code will execute
Thread.sleep(forever)
, but I still need to interrupt it
g
You cannot interrupt blocking code. The only way is shutdown thread, but it will work only if blocking operation handless interruption exception (Thread.sleep does), but it's pretty rare thing, for things like while(true) it just doesn't work. Also you cannot reuse thread after shutdown
And this is not something coroutines related, exatly the same you have in Java and with raw threads Java doesn't have thread stopping starting from version 1.1 or 1.2, just because it wouldn't be possible to write reliable resources or exception management
What is your real use case?
l
This is my real use case
With #C18FWPKKL we allow users to test pieces of code, and configure a test to have a maximum time to execute
However, if the user's code have a infinite loop by mistake, it will never finish, and the tests won't end.
For this, I wanted to interrupt the code that it's executing
But I don't think it's very possible with CoRoutines. I also don't know if I should be thinking about this. JUnit does, for example
In JUnit you can interrupt a while(true)
g
Coroutines can do nothing with blocked thread, same way as pure Java
l
What I tried to do was something like this: Initialize a CoroutineScope -> Run user code inside it. Initializa a CoroutineScope -> Interrupt the first job if it elapses X time
g
I believe you should shutdown vm instance
Like Junit
l
Interesting
This leads to another discussion, if I should actually care about users' code leading to infinite loops
g
You can try interrupt thread, but for most real use cases it will not work
It will help with Thread sleep, but not with infinite loop, so for your use case it is almost useless
So for such cases the only case is kill vm process
One more (not so good) solution, you can just abandon blocked thread and notify user about time out. But it may be not really good to have those infinite threads in background that also accumulated on each test suite run
l
That may be an alternative, I could also just fail the test if I'm not able to cancel it. But how to do this with coroutines tho? The
routine.await()
will never complete, right?
I mean, fail the test and leave the thread hanging
This specific scenario is "Test that this block of code runs in less than Xms"
l
@LeoColman Maybe you're looking for
System.exit(someNonZeroIntegerValue)
? If your coroutine runs on a non blocked thread, it can still be around to call this and shutdown the VM with this call.
l
I'm not. I need the JVM to keep running as other tests might run
l
@LeoColman Then you can fail the test while letting the faulty code under test run, then when all tests are done, you shutdown the VM, which will stop the faulty code altogether.
1