Stuck on #EJ5 Received: "" (no content received) but code works locally

I’m using Scala and I’m stuck on Stage #EJ5. I keep getting this error: Received: “” (no content received), but locally I cannot reproduce this issue. At this point I’m not sure if this is an issue on my side or if it’s a bug

I’ve tested my code with oha:
oha -n 3 -c 3 ``http://localhost:4221

Here are my logs:

accepted new connection from null:49204
accepted new connection from null:49203
accepted new connection from null:49205
Response HTTP/1.1 200 OK\r\n\r\n was sent to client on port 49203
Response HTTP/1.1 200 OK\r\n\r\n was sent to client on port 49205
Response HTTP/1.1 200 OK\r\n\r\n was sent to client on port 49204
OK
OK
OK

And here’s a snippet of my code:

@main def main(): Unit = {
  val CRLF = "\r\n"
  val HTTP_VERSION = "HTTP/1.1"
  val serverSocket = new ServerSocket(4221)
  serverSocket.setReuseAddress(true)

  while (true) {
    val clientSocket = serverSocket.accept()
    println(
      s"accepted new connection from ${clientSocket.getChannel()}:${clientSocket.getPort()}"
    )

    Future {
      handleClient(HTTP_VERSION, clientSocket)
    }.onComplete {
      case Success(_) => println("OK")
      case Failure(e) => println(s"Error handling client: ${e.getMessage}")
    }
  }

}

def handleClient(httpVersion: String, clientSocket: Socket): Unit = {
  try {
    val inputStream = clientSocket.getInputStream()
    val reader = new BufferedReader(new InputStreamReader(inputStream))

    val rawRequest = Iterator
      .continually(reader.readLine())
      .takeWhile(it => it != null && it.nonEmpty)
      .toList

    val request = HttpRequest.parse(rawRequest)

    val outputStream = clientSocket.getOutputStream()

    val response = request.line.path match {
      case "/"                               => HttpResponse(StatusCode.OK)
      case echo if echo.startsWith("/echo/") =>
        HttpResponse(StatusCode.OK, body = echo.stripPrefix("/echo/"))
      case userAgent if userAgent.startsWith("/user-agent") =>
        HttpResponse(
          StatusCode.OK,
          body = request.headers.getOrElse(USER_AGENT, "")
        )
      case _ => HttpResponse(StatusCode.NOT_FOUND)
    }
    val responseBytes = response.toBytes(httpVersion)
    outputStream.write(responseBytes)
    outputStream.flush()
    val a = String(responseBytes).replace("\r\n", "\\r\\n")
    println(s"Response $a was sent to client on port ${clientSocket.getPort}")
  } catch {
    case e: IOException =>
      println(s"IOException: ${e.getMessage}")
  } finally {
      clientSocket.close()
  }
}

Hey @flaviapodariu, ExecutionContext.Implicits.global implicitly uses a thread pool whose size is based on the number of available processors.

Since our testers run on single-CPU machines, that effectively means only one worker thread is available, so your code won’t be able to handle connections concurrently by default.

You can create your own execution context with a larger thread pool instead of using the global one. For example:

Let me know if you’d like any further clarification!

Ooh it makes a lot of sense, thanks a lot!
Also, for anyone interested, a good way to reproduce the issue locally would be to add this JVM flag (under VM options if using IntelliJ): -XX:ActiveProcessorCount=1 .

Then connect to the server from a terminal and do a curl from another terminal. This will hang until the first connection will be closed. This is also way easier than using oha or other similar tools :smiley:

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.