ray.util.Queue Deadlock

I just looked at an example for queues at Ray Docs. If you would use Queues like this with multiprocessing in Python it could deadlock, since another Process could empty the Queue after this process has checked if it is empty or not.

Can somebody please explain how it is different in Ray.

1 Like

Just to make it clear

while not queue.empty():
    # Here the queue could be emptied by another process
    next_item = queue.get(block=True)

cc @simon-mo (I think this part was written by you). I think we should rewrite the example to add a timeout to the get API?

Ray’s semantics will follow exactly the multiprocessing.Queue’s semantics. Namely, if you only have one item in the queue and two callers calling queue.get(block=True), one of them will wait forever. Under the hood, Ray hosts a asyncio.Queue in a Ray actor.

Okay, it seems @Nemo is right and the example will reliably produce deadlock (especially since we say the example is for many consumers). It seems that we should rewrite the example to use queue.get(block=True, timeout=5) so that there isn’t this race condition.

Thoughts @simon-mo @sangcho ? I’m happy to create a fix for this (or @Nemo you can if you want, good find :slight_smile: )

PR to fix is here: [docs] [core] Fixing deadlock in Queue example by cadedaniel · Pull Request #31303 · ray-project/ray · GitHub

1 Like