Skip to content

cohttp server crash when client dies mid-transfer #511

@hcarty

Description

@hcarty

The following server will crash (usually) if the wrk process is canceled with a ctrl-c. I've seen at least EPIPE, ECONNRESET, EPROTOTYPE errors while testing this.

This may be related to #503

Using OCaml 4.02.3, cohttp 0.21.0. Tested on 64bit OSX and Linux with libev/conf-libev installed.

Server code:

module Server = Cohttp_lwt_unix.Server

let callback _conn _req body =
  let%lwt body = Cohttp_lwt_body.to_string body in
  Server.respond_string ~status:`OK ~body ()

let server port =
  Server.create ~mode:(`TCP (`Port port)) (Server.make ~callback ())

let () =
  Lwt_engine.set (new Lwt_engine.libev);
  Lwt_main.run (server (int_of_string Sys.argv.(1)))

Client using wrk:

wrk -s put.lua 'http://127.0.0.1:8080/'

The put.lua script creates a blob of bytes to send as a PUT request to the server.

put.lua:

local Chars = {}
for Loop = 0, 255 do
   Chars[Loop+1] = string.char(Loop)
end
local String = table.concat(Chars)

local Built = {['.'] = Chars}

local AddLookup = function(CharSet)
   local Substitute = string.gsub(String, '[^'..CharSet..']', '')
   local Lookup = {}
   for Loop = 1, string.len(Substitute) do
       Lookup[Loop] = string.sub(Substitute, Loop, Loop)
   end
   Built[CharSet] = Lookup

   return Lookup
end

function string.random(Length, CharSet)
   -- Length (number)
   -- CharSet (string, optional); e.g. %l%d for lower case letters and digits

   local CharSet = CharSet or '.'

   if CharSet == '' then
      return ''
   else
      local Result = {}
      local Lookup = Built[CharSet] or AddLookup(CharSet)
      local Range = table.getn(Lookup)

      for Loop = 1,Length do
         Result[Loop] = Lookup[math.random(1, Range)]
      end

      return table.concat(Result)
   end
end

wrk.method = "PUT"
wrk.body   = string.random(1000000, "%l%d")
wrk.headers["Content-Type"] = "text/plain"

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions