attempt to fix handle_client error and cleanup

This commit is contained in:
tali 2024-01-31 11:48:52 -05:00
parent 4e8381ea35
commit 5c43d23dd5
2 changed files with 20 additions and 17 deletions

View File

@ -685,7 +685,8 @@ let quit t me ~reason =
let close ?(reason = "Client closed") t = let close ?(reason = "Client closed") t =
Option.iter (quit t ~reason) t.user; Option.iter (quit t ~reason) t.user;
Outbox.close t.outbox Outbox.close t.outbox;
t.user <- None
let on_msg_quit t reason = let on_msg_quit t reason =
let reason = match reason with let reason = match reason with

View File

@ -75,17 +75,22 @@ let handle_client
~server_info ~server_info
~addr:conn_addr ~addr:conn_addr
in in
Wheel.add ping_wheel conn;
let reader = Lwt_stream.iter (Connection.on_msg conn) (reader conn_fd) in let reader = Lwt_stream.iter (Connection.on_msg conn) (reader conn_fd) in
let writer = writer conn_fd (Outbox.stream (Connection.outbox conn)) in let writer = writer conn_fd (Outbox.stream (Connection.outbox conn)) in
let close () = Connection.close conn in let both =
Lwt.on_termination reader close; Lwt.finalize
Lwt.on_termination writer close; (fun () -> reader <&> writer)
Wheel.add ping_wheel conn; (fun () -> Lwt_unix.close conn_fd)
Lwt.finalize in
(fun () -> writer) begin
(fun () -> Lwt.on_termination reader (fun () -> Connection.close conn);
Lwt_unix.close conn_fd >|= fun () -> Lwt.on_termination writer (fun () -> Connection.close conn);
info (fun m -> m "connection closed %a" pp_sockaddr conn_addr)) Lwt.on_termination both
(fun () -> info (fun m -> m "connection closed %a" pp_sockaddr conn_addr));
Lwt.on_failure both
(fun e -> error (fun m -> m "%a:@ %a" pp_sockaddr conn_addr Fmt.exn e));
end
type config = { type config = {
port : int; port : int;
@ -130,13 +135,10 @@ let run (cfg : config) : unit Lwt.t =
in in
let on_con (fd, adr) = let on_con (fd, adr) =
Lwt.on_failure handle_client fd adr
(handle_client fd adr ~server_info
~server_info ~router
~router ~ping_wheel
~ping_wheel)
(fun exn ->
error (fun m -> m "%a:@ %a" pp_sockaddr adr Fmt.exn exn))
in in
let listener_promise = let listener_promise =