2016-11-08 3 views
1

Ich habe einfachen HTTP-Server in Ocaml mit Cohttp und Lwt. Wenn ich wrk ausführen, stürzt die Anwendung um 50% der Zeit ab, sobald wrk beendet wird. Ich stelle mir vor, dass der Absturz durch den unerwarteten Abbruch der Verbindung ausgelöst wird.Lwt und Cohttp: `Fataler Fehler: Ausnahme Unix.Unix_error (Unix.ECONNRESET," lesen "," ")`

Ich sehe die folgenden Fehler auf der Konsole:

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 

Gibt es das überhaupt zu verhindern?

Meine vollständige Quellcode ist:

(* 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) 

Dank!

Antwort

3

Der Fehler, den Sie sehen, stammt von einer nicht gehosteten Ausnahme, die ausgelöst wird, wenn der Client unerwartet getrennt wird. Die relevante Ausnahme wird dem asynchronen Ausnahme-Hook von Lwt übergeben (http://ocsigen.org/lwt/2.6.0/api/Lwt#VALasync_exception_hook), der nach Lwts Vorgabe einen Backtrace ausgibt und das Programm mit einem Exit-Code von 2 beendet.

Es ist eine laufende Diskussion darüber auf der cohttp Github issue tracker: https://github.com/mirage/ocaml-cohttp/issues/511

Kurz gesagt, wenn Sie eine benutzerdefinierte Exception-Handler für Lwt des async/„Hintergrund“ definieren Fäden dann können Sie erfassen und ignorieren/log/Behandeln Sie die Clientfehler. Fügen Sie so etwas wie die folgenden, bevor Sie Ihren cohttp Server starten:

Lwt.async_exception_hook := (function 
    | Unix.Unix_error (error, func, arg) -> 
    Logs.warn (fun m -> 
     m "Client connection error %s: %s(%S)" 
     (Unix.error_message error) func arg 
    ) 
    | exn -> Logs.err (fun m -> m "Unhandled exception: %a" Fmt.exn exn) 
); 

von https://github.com/mirage/ocaml-cohttp/issues/511#issuecomment-258510531 genommen und die logs Bibliothek mit dem Ereignis zu protokollieren: http://erratique.ch/software/logs

+1

Dank! Ich habe "Lwt.async_exception_hook: = ignore;" hinzugefügt. und hörte auf, das Problem zu sehen. –