Lwt and Cohttp: `Fatal error: exception Unix.Unix_error(Unix.ECONNRESET, "read", "")`

2016-11-09T06:14:56

I have simple HTTP server in Ocaml with Cohttp and Lwt. When I run wrk the application crashes around 50% of the time as soon as wrk finishes. I imagine the crash is triggered by the unexpected tear-down of the connection.

I see the following error on the console:

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

Is there anyway to prevent this?

My full source-code is:

(* 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
    >|= fun (r, _) -> Code.code_of_status @@ Response.status r

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

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

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

let () = ignore (Lwt_main.run server)

Thanks!

Copyright License:
Author:「user6307701」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/40497364/lwt-and-cohttp-fatal-error-exception-unix-unix-errorunix-econnreset-read

About “Lwt and Cohttp: `Fatal error: exception Unix.Unix_error(Unix.ECONNRESET, "read", "")`” questions

I have simple HTTP server in Ocaml with Cohttp and Lwt. When I run wrk the application crashes around 50% of the time as soon as wrk finishes. I imagine the crash is triggered by the unexpected tea...
Running opam install cohttp does not provide me with cohttp.lwt in findlib. Am I missing a command line option to install with lwt support?
I am using capnproto to send messages between several nodes. Each node can both send and receive messages from all others. The relevant code looks like this: main.ml: let start_node id nodes =
How can I use the Cohttp library to GET a URL over HTTPS? Here is the code I'm trying to use: open Lwt open Cohttp open Cohttp_lwt_unix let url = "https://google.com" let do_request = Client
I'm trying to use the OCaml cohttp package to send a POST request using the Client.post method. I looked at the example in ocaml-cohttp/lib_test/test_net_lwt_client_and_server.ml to use the method,...
The following let new_socket () = Lwt_unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in let socket_address = Network.make_address "127.0.0.1" 7777 in let listening_socket = new_socket () in Lwt...
I wrote the following OCaml code in order to make POST requests to an https server. open Lwt open Cohttp open Cohttp_lwt_unix try let headers = Header.init () |> fun h -> Header.add...
I'm trying to write a small Lwt (and batteries) port scanner to better understand Lwt however I'm getting a strange exception whenever I try to scan too many ports at a time with scan_ports_range. ...
I'm developing a web service in Ocaml on top of MirageOS(Unix) and at the moment I'm having some trouble with Lwt.async(). The Lwt documentation states the following: val async : (unit -> 'a ...
Is there any way to synchronously execute a thread made with Lwt library? To be specific, I am trying to run a series of post requests to a server that compute some value and returns a result. B...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.