https://kotlinlang.org logo
k

kevin.cianfarini

12/29/2022, 2:01 AM
Are there any plans to support
runInterruptable
in Kotlin/Native (posix)? Are there technical limitation to this? I think we could do this with signals.
My use case is that I have a blocking call on native which waits for the kernel to do something. It is not a syscall. Because
runInterruptable
does not support native, I'm having to use a blocking call that times, and then spin needlessly with an arbitrary timeout. For reference, this is all in the context of binding kotlin native to
io_uring
. I would like to be able to do the following:
Copy code
val dispatcher = newSingleThreadedContext(...)
runInterruptable(context = dispatcher) {
  while (true) {
    io_uring_wait_cqe(...) // blocks until kernel gives us a value. 
  }
}
Instead I am having to do this:
Copy code
val dispatcher = newSingleThreadedContext(...)
val timeout = /* 100 milliseconds */
withContext(dispatcher) {
  while (isActive) {
    io_uring_wait_cqe_timeout(..., timeout) // blocks for 100ms or until kernel gives us a value.
  }
}
Originally I was doing the following:
Copy code
withContext(newSingleThreadeContext(...)) {
  while (isActive) {
    io_uring_wait_cqe(...)
  }
}
But there's a subtle bug here. If the parent coroutine scope is cancelled, and there's no new value is ever returned from
io_uring_wait_cqe
, then cancelling the scope has no effect. This loop never checks the
isActive
condition and it hangs forever.
I filed an issue because I didn't see anything like this on the issue tracker. https://github.com/Kotlin/kotlinx.coroutines/issues/3563
d

Dmitry Khalanskiy [JB]

12/29/2022, 12:50 PM
Replied under the issue.
6 Views