question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Timeout exception in the ember parser when using fs2 >= `2.4.0`

See original GitHub issue

I was not sure if I should report this here or in the fs2 repo, but I guess it made more sense to report it here first and move it to fs2 if we can take ember out of the equation.

The error is very simple, if using an ember server (with tls) and the fs2 jar in the classpath is the version 2.4.0 (or superior) then calling the endpoints will fail with the underlying error:

java.util.concurrent.TimeoutException: 5 seconds at timeout @ org.http4s.ember.core.Parser$Request$.$anonfun$parser$8(Parser.scala:394) at flatMap @ org.http4s.ember.core.Parser$Request$.parser(Parser.scala:363) at flatMap @ org.http4s.ember.server.internal.ServerHelpers$.runApp(ServerHelpers.scala:132) at map @ org.http4s.ember.server.internal.ServerHelpers$.$anonfun$runConnection$2(ServerHelpers.scala:198)

MCVE

// scala 2.13.4

import $ivy.`org.typelevel::cats-effect:2.3.1`
import $ivy.`co.fs2::fs2-io:2.5.0` // Replace with 2.3.0 to fix the problem.
import $ivy.`org.http4s::http4s-ember-server:0.21.19`
import $ivy.`org.http4s::http4s-dsl:0.21.19`

import cats.effect.{Blocker, IO}
import fs2.Stream
import fs2.io.tls.TLSContext
import org.http4s.ember.server.EmberServerBuilder

import java.nio.file.Paths

object Main {
  implicit val cs = IO.contextShift(scala.concurrent.ExecutionContext.global)
  implicit val timer = IO.timer(scala.concurrent.ExecutionContext.global)

  val app = {
    import org.http4s.dsl.io._
    import org.http4s.HttpRoutes
    import org.http4s.server.Router
    import org.http4s.syntax.kleisli._ // Provides the orNotFound method.

    val test = HttpRoutes.of[IO] {
      case GET -> Root / "test" => Ok("OK")
    }

    Router(
      "/" -> test
    ).orNotFound
  }

  val keystorePassword = "password".toCharArray

  def tlsContextR(blocker: Blocker) =
    TLSContext.fromKeyStoreFile[IO](
      file = Paths.get("./keystore.jks"),
      keyPassword = keystorePassword,
      storePassword = keystorePassword,
      blocker = blocker
    )

  def serverR(blocker: Blocker, tlsContext: TLSContext) =
    EmberServerBuilder
      .default[IO]
      .withHost("0.0.0.0")
      .withPort(8080)
      .withHttpApp(app)
      .withTLS(tlsContext)
      .withBlocker(blocker)
      .withErrorHandler {
        case e =>
          IO(e.printStackTrace()).as(org.http4s.Response.timeout[IO])
      }.build

  val program =
    for {
      blocker <- Stream.resource(Blocker[IO])
      tlsContext <- Stream.eval(tlsContextR(blocker))
      server <- Stream.resource(serverR(blocker, tlsContext))
      _ <- Stream.eval(IO(println(s"Server has started at ${server.address}")))
      _ <- Stream.never[IO] >> Stream.emit(())
    } yield ()

  def run(): Unit = {
    program.compile.drain.unsafeRunSync()
  }
}

@main
def main(): Unit = {
  Main.run()
}

To generate the keystore.jks file run the following command:

keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 360 -keysize 2048

There is something really weird about this and it is that when using the original code that produced the error it was only reproducible by querying the endpoint using Postman or Axios (a NodeJS http client, which was actually the one that identified the problem) but it worked if using curl, chrome or vertx-web-client. But, using this minimal snippet even using curl I can reproduce the problem.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:21 (21 by maintainers)

github_iconTop GitHub Comments

1reaction
BalmungSancommented, Mar 2, 2021

@rossabaker No worries, as I said it was very easy to fix this in userland by just manually upgrading the fs2 version. But I was not sure if I should have closed this when it was fixed in fs2 or wait until it was “fixed” by http4s directly. Thanks for all the hard work!

1reaction
BalmungSancommented, Feb 20, 2021

@mpilquist I just tested it, it works!

Using the reproducible snippet now I can query it using postman and it answers without problems, (although the exception is still logged after a couple of seconds). Then I upgraded the version in the original / real code and I can confirm the issue is fixed!

Now, should I close this since it is technically fixed, or should we wait until a new version of http4s that depends on at least that version is released?

Read more comments on GitHub >

github_iconTop Results From Across the Web

http4s/http4s - Gitter
I'm building an app using Play with ScalaJS where my main method is an IOApp and the vast majority of my client/server side...
Read more >
Changelog - http4s
SocketException with information on which host failed. http4s-ember-client. Enhancements. #5271: Eliminate exception allocation on the parser hot path ...
Read more >
Read timeout error when parsing - java - Stack Overflow
I understand that java knows that this exception is pointless because it will never happen.
Read more >
Debian -- Source Packages in "sid", Subsection misc
F-3-5); catatonit (0.1.7-1); catch (1.12.2-0.1); catch2 (2.13.9-1) ... django-graphiql-debug-toolbar (0.2.0-3); django-guardian (2.4.0-1); django-haystack ...
Read more >
of /pub/archive/fedora-secondary/releases/30/Everything ...
nodejs-basic-auth-parser-0.0.2-7.fc30.noarch.rpm 2019-02-02 02:12 9.9K [ ] ... nodejs-error-symbol-0.1.0-4.fc30.noarch.rpm 2019-02-02 03:29 9.6K [ ] ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found