Under what circumstances would `delay` suspend for...
# coroutines
d
Under what circumstances would
delay
suspend forever, if I pass it a value around 500-1500 MS? I have some code that is looping with an interval which works perfectly in one environment but not in another - where it's used as a plugin in some other software. The dispatcher used is a single thread dispatcher created using
Executors.newSingleThreadDispatcher(threadFactory).asCoroutineDispatcher()
The behavior I'm seeing is that it never resumes the coroutine.
g
It may be deadlock
Especially with single thread dispatcher
If some code blocks coroutine, delay also cannot be dispatched and will never return
Do you have some use case for single threaded dispatcher?
d
avoiding shared mutable state
I think it's probably blocking somewhere as well
thanks for the responses
g
avoiding shared mutable state
I would use actor for this
in general limiting coroutine concurrency using threads is not really good idea (often it cause unexpected behavior, when you think that suspending will not allow other coroutines to run because you are on single threaded dispatcher), better to use actor, which is 1 coroutine that process queue of events
d
I'm conscious that an actor is good for this, but it's not needed. You can actually use LimitingDispatcher atm with a bit of experimental trickery by the way, which is pretty cool.
Turns out the parent Job was getting cancelled immediately due to a big oversight
😅