# ray.util.Queue Deadlock

**URL:** <https://discuss.ray.io/t/ray-util-queue-deadlock/8688>\
**Category:** Ray Core\
**Created:** [December 17, 2022, 1:27pm UTC](https://discuss.ray.io/t/ray-util-queue-deadlock/8688 "2022-12-17T13:27:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nemo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/nemo/32/1876_2.png) [@Nemo](https://discuss.ray.io/u/Nemo)\
**Post date:** [December 17, 2022, 1:27pm UTC](https://discuss.ray.io/t/ray-util-queue-deadlock/8688/1 "2022-12-17T13:27:30Z")

</div>

I just looked at an example for queues at [Ray Docs](https://docs.ray.io/en/latest/ray-core/actors/actor-utils.html). 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.

---

<div class="post-metadata">

**Author:** ![Nemo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/nemo/32/1876_2.png) [@Nemo](https://discuss.ray.io/u/Nemo)\
**Post date:** [December 19, 2022, 3:28am UTC](https://discuss.ray.io/t/ray-util-queue-deadlock/8688/2 "2022-12-19T03:28:50Z")

</div>

Just to make it clear

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

```

---

<div class="post-metadata">

**Author:** ![sangcho](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sangcho/32/425_2.png) [@sangcho](https://discuss.ray.io/u/sangcho)\
**Post date:** [December 21, 2022, 2:10am UTC](https://discuss.ray.io/t/ray-util-queue-deadlock/8688/3 "2022-12-21T02:10:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![simon-mo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/simon-mo/32/9_2.png) [@simon-mo](https://discuss.ray.io/u/simon-mo)\
**Post date:** [December 21, 2022, 2:30am UTC](https://discuss.ray.io/t/ray-util-queue-deadlock/8688/4 "2022-12-21T02:30:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cade](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/cade/32/3837_2.png) [@cade](https://discuss.ray.io/u/cade)\
**Post date:** [December 22, 2022, 9:26pm UTC](https://discuss.ray.io/t/ray-util-queue-deadlock/8688/5 "2022-12-22T21:26:36Z")

</div>

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 🙂 )

---

<div class="post-metadata">

**Author:** ![cade](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/cade/32/3837_2.png) [@cade](https://discuss.ray.io/u/cade)\
**Post date:** [December 22, 2022, 9:55pm UTC](https://discuss.ray.io/t/ray-util-queue-deadlock/8688/6 "2022-12-22T21:55:21Z")

</div>

PR to fix is here: [[docs] [core] Fixing deadlock in Queue example by cadedaniel · Pull Request #31303 · ray-project/ray · GitHub](https://github.com/ray-project/ray/pull/31303)
