Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ocaml Lwt和Cohttp:`致命错误:异常Unix.Unix''u错误(Unix.ECONNRESET,'read'quot;,'quot;)`_Ocaml_Ocaml Lwt - Fatal编程技术网

Ocaml Lwt和Cohttp:`致命错误:异常Unix.Unix''u错误(Unix.ECONNRESET,'read'quot;,'quot;)`

Ocaml Lwt和Cohttp:`致命错误:异常Unix.Unix''u错误(Unix.ECONNRESET,'read'quot;,'quot;)`,ocaml,ocaml-lwt,Ocaml,Ocaml Lwt,我在Ocaml中有一个简单的HTTP服务器,带有Cohttp和Lwt。当我运行wrk时,只要wrk完成,应用程序就会有大约50%的时间崩溃。我想崩溃是由连接意外中断引发的 我在控制台上看到以下错误: Fatal error: exception Unix.Unix_error(Unix.ECONNRESET, "read", "") Raised by primitive operation at file "src/unix/lwt_bytes.ml", line 130, character

我在Ocaml中有一个简单的HTTP服务器,带有Cohttp和Lwt。当我运行
wrk
时,只要
wrk
完成,应用程序就会有大约50%的时间崩溃。我想崩溃是由连接意外中断引发的

我在控制台上看到以下错误:

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
有没有办法防止这种情况发生

我的完整源代码是:

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

谢谢

您看到的错误来自客户端意外断开连接时引发的未经处理的异常。相关异常被传递给Lwt的异步异常钩子(),在Lwt的默认情况下,该钩子打印回溯并以退出代码
2
退出程序

cohttp github问题追踪器上正在对此进行讨论:

简而言之,如果为Lwt的异步/后台线程定义自定义异常处理程序,则可以捕获并忽略/记录/处理客户端错误。在启动cohttp服务器之前,请添加以下内容:

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)
);

取自并使用
日志
库记录事件:

您看到的错误来自客户端意外断开连接时引发的未经处理的异常。相关异常被传递给Lwt的异步异常钩子(),在Lwt的默认情况下,该钩子打印回溯并以退出代码
2
退出程序

cohttp github问题追踪器上正在对此进行讨论:

简而言之,如果为Lwt的异步/后台线程定义自定义异常处理程序,则可以捕获并忽略/记录/处理客户端错误。在启动cohttp服务器之前,请添加以下内容:

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)
);
取自并使用
日志
库记录事件:

谢谢!我添加了“Lwt.async\u exception\u hook:=ignore;”并停止查看该问题。谢谢!我添加了“Lwt.async\u exception\u hook:=ignore;”并停止查看该问题。