Lwt and Cohttp: `Fatal error: exception Unix.Unix_error(Unix.ECONNRESET, "read", "")`
<p>I have simple HTTP server in Ocaml with Cohttp and Lwt. When I run <code>wrk</code> the application crashes around 50% of the time as soon as <code>wrk</code> finishes. I imagine the crash is triggered by the unexpected tear-down of the connection.</p>

<p>I see the following error on the console:</p>

<pre><code>Fatal error: exception Unix.Unix_error(Unix.ECONNRESET, "read", "")
Raised by primitive operation at file "src/unix/lwt_bytes.ml", line 130, characters 42-84
Called from file "src/unix/lwt_unix.ml", line 489, characters 13-24
</code></pre>

<p>Is there anyway to prevent this?</p>

<p>My full source-code is:</p>

<pre><code>(* server_test.ml *)
open Unix
open Lwt
open Cohttp
open Cohttp_lwt_unix
open Yojson
open Yojson.Basic.Util
open Core.Std

type create = {
username: string;
email: string;
password: string;
} [@@deriving yojson]

let insert coll doc =
    let _id = Core.Std.Uuid.to_string (Uuid.create ()) in
    let uri = Uri.make ~scheme:"http" ~host:"127.0.0.1" ~port:5984 ~path:(coll ^ "/" ^ _id) () in
    Cohttp_lwt_unix.Client.put ~body:(Cohttp_lwt_body.of_string (Yojson.Safe.to_string doc)) uri
    &gt;|= fun (r, _) -&gt; Code.code_of_status @@ Response.status r

let callback _conn req body =
    body |&gt; Cohttp_lwt_body.to_string 
    &gt;&gt;= (fun body -&gt; 
        let mc = Yojson.Safe.from_string body |&gt; create_of_yojson in
        match mc with
        | Ok c -&gt; 
            insert "users" (create_to_yojson c)
            &gt;&gt;= fun status -&gt; print_endline @@ string_of_int status; 
                Server.respond_string ~status:(`Code status) ~body:(string_of_int status) ()
        | _ -&gt; Server.respond_string ~status:`OK ~body: "Not OK" ())

let timeit _conn req body =
    let start = Unix.gettimeofday () in
    callback _conn req body 
    &gt;&gt;= 
    fun result -&gt;
        let finish = Unix.gettimeofday () in
        Lwt_io.printlf "Execution time took %fms" ((finish -. start) *. 1000.0)
        &gt;|= fun _ -&gt; result

let server =
    Server.create ~mode:(`TCP (`Port 8000)) (Server.make timeit ())

let () = ignore (Lwt_main.run server)
</code></pre>

<p>Thanks!</p>