akka/akka-http

Early response warning when ignoring the last chunk in a chunked request

Open

#2,455 opened on Mar 8, 2019

View on GitHub
 (5 comments) (0 reactions) (0 assignees)Scala (598 forks)batch import
1 - triagedhelp wantedt:coret:server

Repository metrics

Stars
 (1,311 stars)
PR merge metrics
 (Avg merge 1d 10h) (2 merged PRs in 30d)

Description

Not sure if this should be considered an Akka HTTP bug or not, or if it's an intrinsic race condition that should instead be avoided by client code.

The following server code:

 Http().bindAndHandleAsync({ req =>
    req.entity match {
      case HttpEntity.Chunked(ct, chunks) =>
        chunks.takeWhile(!_.isLastChunk).runWith(Sink.fold(0)((count, _) => count + 1))
          .map(count => HttpResponse(entity = HttpEntity(s"Got chunked request with $count chunks")))
    }
  }, "localhost", 8080)

looks fair enough, it's taking all the chunks up to the last chunk, and then counting them, and telling the client how many chunks there were.

So, now in the client we invoke that with something like this:

  Http().singleRequest(
        HttpRequest(
          uri = s"http://localhost:$port",
          method = HttpMethods.POST,
          entity = HttpEntity.Chunked(ContentTypes.`text/plain(UTF-8)`, Source.single(ChunkStreamPart("a")))
        )
      )

And everything works just fine, except for this warning in the logs:

[WARN] [03/08/2019 16:04:17.480] [default-akka.actor.default-dispatcher-12] [akka.actor.ActorSystemImpl(default)] Sending an 2xx 'early' response before end of request was received... Note that the connection will be closed after this response. Also, many clients will not read early responses! Consider only issuing this response after the request data has been completely read!

It's a bit odd that the last chunk has been received, and so presumably the very next thing that the HTTP request body is going to do is terminate the stream, but somehow, my asynchronously provided result gets there first, and is considered to have been emitted before the request body stream has been consumed.

Contributor guide