# Memory leak of RepointableActorRef due to race condition

**URL:** <https://discuss.akka.io/t/memory-leak-of-repointableactorref-due-to-race-condition/3073>\
**Category:** Akka Libraries\
**Created:** [January 4, 2019, 9:24am UTC](https://discuss.akka.io/t/memory-leak-of-repointableactorref-due-to-race-condition/3073 "2019-01-04T09:24:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sasiharan](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/sasiharan/32/1113_2.png) [@sasiharan](https://discuss.akka.io/u/sasiharan)\
**Post date:** [January 4, 2019, 9:24am UTC](https://discuss.akka.io/t/memory-leak-of-repointableactorref-due-to-race-condition/3073/1 "2019-01-04T09:24:37Z")

</div>

Hi,

we are using play-framework 2.6.17 version with scala version 2.11 for our service.

We notice the heap usage increases gradually over a period of time and from the analysis of heap dump,  
the number of RepointableActorRef instances were around 105K with retained heap size of over 200MB.  
This in turn cause the fullGC cycle to be triggered frequently.

GC root of these instances point to supervisor(StreamSupervisor-0) of akka.stream.impl.PhasedFusingActorMaterializer instance for AkkaHttpServer.

We looked at the associated ActorGraphInterpreter instances which has the ‘interpreterCompleted’ value as ‘true’ with no subcribersPending. From our code understanding, the RepointableActorRef should be removed from the childrenContainer once the interpreter execution is completed. But in this case,

We notice following sequence of action happening

- as part of preStart() method in ActorGraphInterpreter, it checks for ‘activeInterpreters’ which is empty and stops the actor.
- But during this time, actor still has ‘UnstartedActorCell’ which receives the stop message and swapping of active ActorCell has not happened.
- Now supervisor which added this actorRef as child never gets DeathWatchNotification since the stop message is handled by UnstartedActorCell.

This seems to be a kind of race condition due to which the RepointableActorRef references are retained in the childrenContainer of supervisor forever.

Could someone give pointers on if this issue is already resolved in later version.

Thanks.

---

<div class="post-metadata">

**Author:** ![johanandren](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/johanandren/32/85_2.png) [@johanandren](https://discuss.akka.io/u/johanandren)\
**Post date:** [January 4, 2019, 2:35pm UTC](https://discuss.akka.io/t/memory-leak-of-repointableactorref-due-to-race-condition/3073/2 "2019-01-04T14:35:20Z")

</div>

That is interesting, would it be possible to share a minimal reproducer?

---

<div class="post-metadata">

**Author:** ![sasiharan](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/sasiharan/32/1113_2.png) [@sasiharan](https://discuss.akka.io/u/sasiharan)\
**Post date:** [January 8, 2019, 9:52am UTC](https://discuss.akka.io/t/memory-leak-of-repointableactorref-due-to-race-condition/3073/3 "2019-01-08T09:52:56Z")

</div>

I will try to come up with sample project which can reproduce this issue.

---

<div class="post-metadata">

**Author:** ![sasiharan](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/sasiharan/32/1113_2.png) [@sasiharan](https://discuss.akka.io/u/sasiharan)\
**Post date:** [January 14, 2019, 3:28pm UTC](https://discuss.akka.io/t/memory-leak-of-repointableactorref-due-to-race-condition/3073/4 "2019-01-14T15:28:59Z")

</div>

I modified the sample play-scala starter project with using Accumulator.flatten api which creates a graph ‘Source. _fromPublisher_ (publisher).toMat(accumulator.toSink)(Keep. _right_ ).run()’ as part of futureToSink method call in FlattenedAccumulator initialization.

When hitting the endpoint ‘[http://localhost:9000/count](http://localhost:9000/count)’ at 1000 req in a minute, I can see the following logs,

./play-scala-starter-example -Dplay.http.secret.key=abcdefghijk | grep UnstartedCell  
UnstartedCell stop method akka://application/system/StreamSupervisor-0/flow-463-0  
UnstartedCell stop method akka://application/system/StreamSupervisor-0/flow-1423-0  
UnstartedCell stop method akka://application/system/StreamSupervisor-0/flow-1961-0  
UnstartedCell stop method akka://application/system/StreamSupervisor-0/flow-2327-0  
UnstartedCell stop method akka://application/system/StreamSupervisor-0/flow-2328-0-lazySource  
UnstartedCell stop method akka://application/system/StreamSupervisor-0/flow-2330-0  
UnstartedCell stop method akka://application/system/StreamSupervisor-0/flow-2780-0  
UnstartedCell stop method akka://application/system/StreamSupervisor-0/flow-2807-0  
UnstartedCell stop method akka://application/system/StreamSupervisor-0/flow-2876-0  
UnstartedCell stop method akka://application/system/StreamSupervisor-0/flow-2984-0

In my local setup, I had custom akka-actor.jar in the lib folder containing above debug message in stop method of UnstartedCell class.

Kindly find the below project which has the modified code,  
[https://github.com/sasiharan/play-scala-starter-example/tree/testing](https://github.com/sasiharan/play-scala-starter-example/tree/testing).

Pls let me know if you need any additional information.

---

<div class="post-metadata">

**Author:** ![eugenemiretsky](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/eugenemiretsky/32/1508_2.png) [@eugenemiretsky](https://discuss.akka.io/u/eugenemiretsky)\
**Post date:** [October 8, 2019, 11:13pm UTC](https://discuss.akka.io/t/memory-leak-of-repointableactorref-due-to-race-condition/3073/5 "2019-10-08T23:13:19Z")

</div>

Hi @ [sasiharan](https://discuss.akka.io/u/sasiharan)  
Did you find a solution to this? I am getting pretty much the exact same problem

---

<div class="post-metadata">

**Author:** ![johanandren](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/johanandren/32/85_2.png) [@johanandren](https://discuss.akka.io/u/johanandren)\
**Post date:** [November 18, 2019, 1:07pm UTC](https://discuss.akka.io/t/memory-leak-of-repointableactorref-due-to-race-condition/3073/6 "2019-11-18T13:07:48Z")

</div>

I can’t reproduce a memory leak with the sample project, putting load on the sample project in prod mode, whenever a gc is triggered I see heap usage drop down to 10-20mb. So there must be some more circumstance causing this for you.

In Akka 2.6.0+ we have eliminated the use of `RepointableActorRef` for the system level materializers, so it could be worth checking out the play 2.8 release candidate and see if you can still repeat the problem.

---

<div class="post-metadata">

**Author:** ![johanandren](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/johanandren/32/85_2.png) [@johanandren](https://discuss.akka.io/u/johanandren)\
**Post date:** [November 18, 2019, 1:08pm UTC](https://discuss.akka.io/t/memory-leak-of-repointableactorref-due-to-race-condition/3073/7 "2019-11-18T13:08:27Z")

</div>

Akka issue tracker issue for this: [https://github.com/akka/akka/issues/27948](https://github.com/akka/akka/issues/27948)
