# "RuntimeError: Lost reference to actor" when using get\_actor directly (i.e. not storing in a temporary variable)

**URL:** <https://discuss.ray.io/t/runtimeerror-lost-reference-to-actor-when-using-get-actor-directly-i-e-not-storing-in-a-temporary-variable/1261>\
**Category:** Ray Core\
**Created:** [March 14, 2021, 7:30pm UTC](https://discuss.ray.io/t/runtimeerror-lost-reference-to-actor-when-using-get-actor-directly-i-e-not-storing-in-a-temporary-variable/1261 "2021-03-14T19:30:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mikhail\_Sychev](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mikhail_sychev/32/627_2.png) [@Mikhail\_Sychev](https://discuss.ray.io/u/Mikhail_Sychev)\
**Post date:** [March 14, 2021, 7:30pm UTC](https://discuss.ray.io/t/runtimeerror-lost-reference-to-actor-when-using-get-actor-directly-i-e-not-storing-in-a-temporary-variable/1261/1 "2021-03-14T19:30:27Z")

</div>

I’ve encountered a super puzzling lifecycle/scope(?) issue that took a long time to debug so wanted to share and gather some ideas form the community.

In my project I have a very basic setup ([like this](https://discuss.ray.io/t/long-lived-ray-actors/23/2)) with an actor that I converted into a named one so I don’t need to pass it though multiple layers of my code.

Doing so triggered this bizarre error:

```
>>> ray.get_actor("CounterActor").read.remote()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "###\envs\ray\lib\site-packages\ray\actor.py", line 111, in remote
    return self._remote(args, kwargs)
  File "###\envs\ray\lib\site-packages\ray\actor.py", line 152, in _remote
    return invocation(args, kwargs)
  File "###\envs\ray\lib\site-packages\ray\actor.py", line 140, in invocation
    raise RuntimeError("Lost reference to actor")
RuntimeError: Lost reference to actor

```

I’ve confirmed that my agent is still around because the same one passed via a variable was still happily accepting calls, and even tried to use “detached” lifecycle, just in case, with no effect.

Turned out that as long as I do something like this:

```
f = ray.get_actor('CounterActor')
f.read.remote()

```

everything works, but converting this to a oneliner doesn’t

`ray.get_actor('CounterActor').read.remote()`

so if anyone has ideas on what is going on, I would be happy for any clues 🙂

---

<div class="post-metadata">

**Author:** ![rkn](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/rkn/32/8_2.png) [@rkn](https://discuss.ray.io/u/rkn)\
**Post date:** [March 14, 2021, 9:12pm UTC](https://discuss.ray.io/t/runtimeerror-lost-reference-to-actor-when-using-get-actor-directly-i-e-not-storing-in-a-temporary-variable/1261/2 "2021-03-14T21:12:59Z")

</div>

You can reproduce this issue without named actors as follows.

```auto
import ray

ray.init()

@ray.remote
class Foo:
    def method(self):
        pass

ray.get(Foo.remote().method.remote())

```

The issue is tracked here [Lost reference to actor exception for seemingly valid code. · Issue #6265 · ray-project/ray · GitHub](https://github.com/ray-project/ray/issues/6265).

Right now it’s a limitation that we have because the natural fix would introduce performance issues. There’s probably a good way to get around this though. We can probably also improve the error message in the short term.

---

<div class="post-metadata">

**Author:** ![Mikhail\_Sychev](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mikhail_sychev/32/627_2.png) [@Mikhail\_Sychev](https://discuss.ray.io/u/Mikhail_Sychev)\
**Post date:** [March 15, 2021, 5:58am UTC](https://discuss.ray.io/t/runtimeerror-lost-reference-to-actor-when-using-get-actor-directly-i-e-not-storing-in-a-temporary-variable/1261/3 "2021-03-15T05:58:02Z")

</div>

Thank you, Robert!

I’ve missed the bug, which is embarrassing given that it’s the first Google result 🙂

A better error message (or perhaps a warning in the docs) would be super helpful.
