Skip to content

Sporadic invalid Http response (missing headers) #3395

@memo33

Description

@memo33

When serving a simple static file server, occasionally the zio-http server sends invalid HTTP responses that lack all headers.

I've used the command tcpflow -i any -C -g port 8090 to monitor the TCP traffic. Downloading multiple files (regardless of whether sequentially or in parallel), eventually I see the following for a random file:

Request:

GET /files/example.txt HTTP/1.1
User-Agent: my-user-agent
Accept: */*
Host: localhost:8090
Connection: keep-alive

Expected response:

HTTP/1.1 200 Ok
content-type: text/plain; charset=utf-8
date: Wed, 12 Mar 2025 19:19:38 GMT
content-length: 30


text-file-content-lorem-ipsum

Actual response:

text-file-content-lorem-ipsum

which is just the served text file as body. The response headers are missing.

Server code:

import zio.ZIO
import zio.http.*

final class FileServer private (val port: Int)
object FileServer {

  def routes(staticDir: java.io.File): Routes[Any, Nothing] = Routes(
    Method.GET / trailing -> Handler.fromFunctionHandler[(zio.http.Path, Request)] { case (path: zio.http.Path, _: Request) =>
      Handler.fromFileZIO(ZIO.attempt { new java.io.File(staticDir, path.encode) })
        .orElse(Handler.notFound).contramap(_._2)
    },
  ).sandbox

  def serve(staticDir: java.io.File, port: Int): zio.ZLayer[Any, Nothing, FileServer] =
    zio.ZLayer.scoped {
      for {
        fiber  <- Server.install(routes(staticDir))
                    .zipRight(ZIO.never)
                    .provideSome(Server.defaultWithPort(port))
                    .forkScoped
      } yield FileServer(port = port)
    }
}

(I could not find a minimal implementation of a HTTP client that consistently exposes the problem, but the problem seems to be with the server anyway.)

Tested with Scala 3.6.3 & 3.4.2, zio 2.1.16, zio-http 3.0.1, izumi-reflect 3.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions