sender inside a future

You are making a very common mistake of “closing over mutable state”. The closure you pass to onComplete does not make a copy of this.sender, so when your onComplete gets called, you are sending the message to whatever this.sender happens to point to at that time, not what it pointed to when you created the closure.

You can avoid this problem by creating your own local, immutable copy of the current contents of this.sender, and reference that value in the closure:

val origSender = sender
f.onComplete {
    case Successs(x) => origSender ! x
    ...
}

Leave a Comment