NullPointerException when concatenating routes (akka-http)

351 views
Skip to first unread message

Pouria Mellati

unread,
Mar 24, 2016, 3:28:11 PM3/24/16
to Akka User List
I think I've ran into a akka-http bug.

I have a big-ass route:

val route = pathPrefix("users") {
 
...
}


And wanted to add this new route to it:

val blockingEndpoint = (pathPrefix("users" / "blocked") & authentication) {issuerId =>
  path
(JavaUUID) {blockeeId =>
    put
{
      complete
{HttpResponse(NotImplemented)}
   
} ~
   
delete {
      complete
{HttpResponse(NotImplemented)}
   
}
 
} ~
 
get {
    complete
{HttpResponse(NotImplemented)}
 
}
}



Now, if I simply do:


val route = pathPrefix("users") {
 
...
} ~
blockingEndpoint


I will get the following NPE if I send a request towards the routes in blockingEndpoint:

java.lang.NullPointerException: null


        at akka.http.scaladsl.server.RouteConcatenation$RouteWithConcatenation$$anonfun$$tilde$1$$anonfun$apply$1.apply(RouteConcatenation.scala:28) ~[akka-http-experimental_2.11-2.0.2.jar:na]


        at akka.http.scaladsl.server.RouteConcatenation$RouteWithConcatenation$$anonfun$$tilde$1$$anonfun$apply$1.apply(RouteConcatenation.scala:25) ~[akka-http-experimental_2.11-2.0.2.jar:na]


        at akka.http.scaladsl.util.FastFuture$.akka$http$scaladsl$util$FastFuture$$strictTransform$1(FastFuture.scala:41) ~[akka-http-core-experimental_2.11-2.0.2.jar:na]


        at akka.http.scaladsl.util.FastFuture$.transformWith$extension1(FastFuture.scala:45) ~[akka-http-core-experimental_2.11-2.0.2.jar:na]


        at akka.http.scaladsl.util.FastFuture$.flatMap$extension(FastFuture.scala:26) ~[akka-http-core-experimental_2.11-2.0.2.jar:na]


        at akka.http.scaladsl.server.RouteConcatenation$RouteWithConcatenation$$anonfun$$tilde$1.apply(RouteConcatenation.scala:25) ~[akka-http-experimental_2.11-2.0.2.jar:na]


        at akka.http.scaladsl.server.RouteConcatenation$RouteWithConcatenation$$anonfun$$tilde$1.apply(RouteConcatenation.scala:23) ~[akka-http-experimental_2.11-2.0.2.jar:na]


        at akka.http.scaladsl.server.directives.BasicDirectives$$anonfun$mapRouteResultWith$1$$anonfun$apply$4.apply(BasicDirectives.scala:35) ~[akka-http-experimental_2.11-2.0.2.jar:na]


        at akka.http.scaladsl.server.directives.BasicDirectives$$anonfun$mapRouteResultWith$1$$anonfun$apply$4.apply(BasicDirectives.scala:35) ~[akka-http-experimental_2.11-2.0.2.jar:na]


        at akka.http.scaladsl.server.directives.BasicDirectives$$anonfun$textract$1$$anonfun$apply$5.apply(BasicDirectives.scala:88) ~[akka-http-experimental_2.11-2.0.2.jar:na]


        at akka.http.scaladsl.server.directives.BasicDirectives$$anonfun$textract$1$$anonfun$apply$5.apply(BasicDirectives.scala:88) ~[akka-http-experimental_2.11-2.0.2.jar:na]


        at akka.http.scaladsl.server.directives.ExecutionDirectives$$anonfun$handleExceptions$1$$anonfun$apply$1.apply(ExecutionDirectives.scala:27) ~[akka-http-experimental_2.11-2.0.2.jar:na]


        at akka.http.scaladsl.server.directives.ExecutionDirectives$$anonfun$handleExceptions$1$$anonfun$apply$1.apply(ExecutionDirectives.scala:23) ~[akka-http-experimental_2.11-2.0.2.jar:na]


        at akka.http.scaladsl.server.Route$$anonfun$asyncHandler$1.apply(Route.scala:64) ~[akka-http-experimental_2.11-2.0.2.jar:na]


        at akka.http.scaladsl.server.Route$$anonfun$asyncHandler$1.apply(Route.scala:63) ~[akka-http-experimental_2.11-2.0.2.jar:na]


        at akka.stream.impl.fusing.MapAsync$$anon$9$$anon$10.onPush(Ops.scala:590) ~[akka-stream-experimental_2.11-2.0.2.jar:na]


        at akka.stream.impl.fusing.GraphInterpreter.processElement$1(GraphInterpreter.scala:575) ~[akka-stream-experimental_2.11-2.0.2.jar:na]


        at akka.stream.impl.fusing.GraphInterpreter.processEvent(GraphInterpreter.scala:586) ~[akka-stream-experimental_2.11-2.0.2.jar:na]


        at akka.stream.impl.fusing.GraphInterpreter.execute(GraphInterpreter.scala:533) ~[akka-stream-experimental_2.11-2.0.2.jar:na]


        at akka.stream.impl.fusing.GraphInterpreterShell.runBatch(ActorGraphInterpreter.scala:445) ~[akka-stream-experimental_2.11-2.0.2.jar:na]


        at akka.stream.impl.fusing.GraphInterpreterShell.receive(ActorGraphInterpreter.scala:400) ~[akka-stream-experimental_2.11-2.0.2.jar:na]


        at akka.stream.impl.fusing.ActorGraphInterpreter$$anonfun$receive$1.applyOrElse(ActorGraphInterpreter.scala:549) ~[akka-stream-experimental_2.11-2.0.2.jar:na]


        at akka.actor.Actor$class.aroundReceive(Actor.scala:480) ~[akka-actor_2.11-2.4.1.jar:na]


        at akka.stream.impl.fusing.ActorGraphInterpreter.aroundReceive(ActorGraphInterpreter.scala:495) ~[akka-stream-experimental_2.11-2.0.2.jar:na]


        at akka.actor.ActorCell.receiveMessage(ActorCell.scala:526) [akka-actor_2.11-2.4.1.jar:na]


        at akka.actor.ActorCell.invoke(ActorCell.scala:495) [akka-actor_2.11-2.4.1.jar:na]


        at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:257) [akka-actor_2.11-2.4.1.jar:na]


        at akka.dispatch.Mailbox.run(Mailbox.scala:224) [akka-actor_2.11-2.4.1.jar:na]


        at akka.dispatch.Mailbox.exec(Mailbox.scala:234) [akka-actor_2.11-2.4.1.jar:na]


        at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library-2.11.7.jar:na]


        at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library-2.11.7.jar:na]


        at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library-2.11.7.jar:na]


        at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library-2.11.7.jar:na]


On the other hand, adding the new routes directly to routes (i.e. without defining blockingEndpoint) works fine:

val route = pathPrefix("users") {
 
...
} ~
(pathPrefix("users" / "blocked") & authentication) {issuerId =>
  path(JavaUUID) {blockeeId => 
    put 
{ 
      complete
{HttpResponse(NotImplemented)} 
    
} ~ 
    
delete { 
      complete
{HttpResponse(NotImplemented)} 
    
} 
  
} ~ 
  
get { 
    complete
{HttpResponse(NotImplemented)} 
  
} 
}


My akka version: "2.4.1"
My akka streams and http version = "2.0.2"

Thanks.


Konrad Malawski

unread,
Mar 24, 2016, 3:32:17 PM3/24/16
to akka...@googlegroups.com, Pouria Mellati
Hi there,
you're running into "val referring to not yet initialized val" problems.

in such order that one trait refers with a `val` to another before the first one has a chance to initialize etc.

In general if you have a lot of routes refering to one another I'd recommend making them `lazy val` instead.

// FYI: This is a general Scala thing, it could happen with "val x = a + b" if b was not yet initialized.

Hope this helps, happy hakking!


-- 
Cheers,
Konrad 'ktoso’ Malawski
Akka @ Lightbend
--
>>>>>>>>>> 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.

Pouria Mellati

unread,
Mar 28, 2016, 10:28:35 PM3/28/16
to Akka User List, pou...@takor.com.au
Thanks Konrad, this should fix the issue!

I think this sort of issue should be pointed out by the compiler, rather than at runtime. Not sure if it is possible though.

...

Konrad Malawski

unread,
Mar 29, 2016, 6:29:33 AM3/29/16
to akka...@googlegroups.com, Pouria Mellati, pou...@takor.com.au


-- 
Cheers,
Konrad 'ktoso’ Malawski
Akka @ Lightbend

On 29 March 2016 at 04:28:40, Pouria Mellati (pou...@takor.com.au) wrote:

Thanks Konrad, this should fix the issue!

Yup, happy hakking!


I think this sort of issue should be pointed out by the compiler, rather than at runtime. Not sure if it is possible though.

In simple cases Scala actually does notify about that, simple forward references etc.

In more complex cases it's not trivial sadly so it doesn't (nowadays, perhaps it can be improved in future scalac versions).


-- Konrad

Reply all
Reply to author
Forward
0 new messages