import context.dispatcher nullpointerexception

80 views
Skip to first unread message

Jeff

unread,
Mar 6, 2018, 2:37:50 PM3/6/18
to Akka User List
I have noticed an issue where if a future maps/flatmaps after actor shutdown, a NullPointerException is thrown. I've narrowed it down to the import context.dispatcher, which I technically understand since that is closing over actor state. If I change that to implicit val ec = context.dispatcher, everything works fine. 

I'd like to understand what is the best practice in this case, since the documentation for context.dispatcher indicates that it is threadsafe and looking at the actor trait, context is a val. Most documentation seems to indicate that import context.dispatcher is preferred. 

Thanks
Jeff

Viktor Klang

unread,
Mar 6, 2018, 3:09:22 PM3/6/18
to Akka User List
Context is bound to the lifecycle of the actor.

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+unsubscribe@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Cheers,

Jeff

unread,
Mar 6, 2018, 4:44:52 PM3/6/18
to Akka User List
I suspected as much. So what would you suggest as for handling use cases where you could have chained flatmaps on futures that at the end will pipeTo a message back to the actor. Should we set the ExecutionContext to a val? 


On Tuesday, March 6, 2018 at 12:09:22 PM UTC-8, √ wrote:
Context is bound to the lifecycle of the actor.
On Tue, Mar 6, 2018 at 8:37 PM, Jeff <jknigh...@gmail.com> wrote:
I have noticed an issue where if a future maps/flatmaps after actor shutdown, a NullPointerException is thrown. I've narrowed it down to the import context.dispatcher, which I technically understand since that is closing over actor state. If I change that to implicit val ec = context.dispatcher, everything works fine. 

I'd like to understand what is the best practice in this case, since the documentation for context.dispatcher indicates that it is threadsafe and looking at the actor trait, context is a val. Most documentation seems to indicate that import context.dispatcher is preferred. 

Thanks
Jeff

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.

To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Cheers,

Patrik Nordwall

unread,
Mar 7, 2018, 9:09:24 AM3/7/18
to akka...@googlegroups.com
It's because when the actor is stopped some of the fields are cleared (yeah, even though they are vals) to "help" the GC in case something (e.g. an local ActorRef) is still referencing the actor instance.

implicit val ec = context.dispatcher

would solve it here.

Where in the documentation is the import recommended? We should probably update that. Would you be able creating a PR fixing it? Thanks.

/Patrik

To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+unsubscribe@googlegroups.com.

To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--

Patrik Nordwall
Akka Tech Lead
Lightbend -  Reactive apps on the JVM
Twitter: @patriknw

Jeff

unread,
Mar 7, 2018, 12:16:58 PM3/7/18
to Akka User List
An example of documentation for using import context.dispatcher is here https://doc.akka.io/docs/akka/2.5/futures.html#within-actors

I can create some PR to update the documentation

Patrik Nordwall
Akka Tech Lead

Patrik Nordwall

unread,
Mar 7, 2018, 1:23:46 PM3/7/18
to akka...@googlegroups.com
Thanks!

To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+unsubscribe@googlegroups.com.

To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Heiko Seeberger

unread,
Mar 7, 2018, 5:32:59 PM3/7/18
to akka...@googlegroups.com
`import context.dispatcher` is what „everybody“ is doing, not? I’m using it all over the place, because I have learned that one can import from stable identifiers (e.g. vals) in Scala. Hence I don’t think changing the docs has the necessary effect.

How can a final val be „nulled"? Unsafe? Reflection?

Cheers
Heiko

--

Heiko Seeberger
Twitter: @hseeberger



Konrad “ktoso” Malawski

unread,
Mar 7, 2018, 8:35:05 PM3/7/18
to akka...@googlegroups.com, Heiko Seeberger
Yes it is nulled using unsafe.

-- 
Cheers,
Konrad 'ktoso' Malawski

Jeff

unread,
Mar 8, 2018, 3:15:53 PM3/8/18
to Akka User List
I agree with Heiko on this one - import context.dispatcher is used everywhere. It is mentioned in the docs not to close over 'context' and access it outside of message handlers, however I think more education is needed here. 
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Patrik Nordwall

unread,
May 2, 2018, 8:56:53 AM5/2/18
to akka...@googlegroups.com
For the record, I created issue https://github.com/akka/akka/issues/25040

To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+unsubscribe@googlegroups.com.

To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages