Node: shutdown on an RPC server error.

This commit is contained in:
Benjamin Canou 2017-02-13 14:05:24 +01:00 committed by Grégoire Henry
parent 8f618a0f94
commit f7a70f355c
2 changed files with 19 additions and 5 deletions

View File

@ -178,15 +178,24 @@ let launch ?pre_hook ?post_hook ?(host="::") mode root cors_allowed_origins cors
Cohttp_lwt_body.empty) Cohttp_lwt_body.empty)
| e -> Lwt.fail e) | e -> Lwt.fail e)
and conn_closed (_, con) = and conn_closed (_, con) =
log_info "connection close %s" (Cohttp.Connection.to_string con) ; log_info "connection closed %s" (Cohttp.Connection.to_string con) ;
shutdown_stream con in shutdown_stream con in
Conduit_lwt_unix.init ~src:host () >>= fun ctx -> Conduit_lwt_unix.init ~src:host () >>= fun ctx ->
let ctx = Cohttp_lwt_unix_net.init ~ctx () in let ctx = Cohttp_lwt_unix_net.init ~ctx () in
let stop = cancelation () in let stop = cancelation () in
let _server = Lwt.async
Server.create (fun () ->
~stop ~ctx ~mode Lwt.catch
(Server.make ~callback ~conn_closed ()) in (fun () ->
Server.create
~stop ~ctx ~mode
(Server.make ~callback ~conn_closed ()))
(function
| Unix.Unix_error (Unix.EADDRINUSE, "bind", _) ->
lwt_log_error "RPC server port already taken, \
the node will be shutdown" >>= fun () ->
Lwt_exit.exit 1
| exn -> Lwt.fail exn)) ;
let shutdown () = let shutdown () =
canceler () >>= fun () -> canceler () >>= fun () ->
lwt_log_info "server not really stopped (cohttp bug)" >>= fun () -> lwt_log_info "server not really stopped (cohttp bug)" >>= fun () ->

View File

@ -16,6 +16,11 @@ let () =
Lwt.async_exception_hook := Lwt.async_exception_hook :=
(function (function
| Exit -> () | Exit -> ()
(* BEGIN HACK
ignore error unhandled by cohttp (until PR129 is merged) *)
| Unix.Unix_error(Unix.ECONNRESET, _, _)
| Unix.Unix_error(Unix.EPIPE, _, _) -> ()
(* END HACK *)
| exn -> | exn ->
Format.eprintf Format.eprintf
"@[Uncaught (asynchronous) exception (%d):@ %a@]" "@[Uncaught (asynchronous) exception (%d):@ %a@]"