From 2994ad6c5089256df284ffd216229911db69f621 Mon Sep 17 00:00:00 2001 From: Vasilis Papavasileiou Date: Mon, 12 Feb 2018 19:07:24 +0100 Subject: [PATCH] Fix buggy & leaky "connection closed" detection connection != request :( --- src/server/ocsigen_cohttp.ml | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/server/ocsigen_cohttp.ml b/src/server/ocsigen_cohttp.ml index 122c42976..52fa2e9d5 100644 --- a/src/server/ocsigen_cohttp.ml +++ b/src/server/ocsigen_cohttp.ml @@ -35,7 +35,7 @@ let print_request fmt request = values)) (Cohttp.Request.headers request) -let waiters = Hashtbl.create 256 +let connections = Hashtbl.create 256 exception Ocsigen_is_dir of (Ocsigen_request.t -> Uri.t) @@ -116,8 +116,15 @@ let handler ~ssl ~address ~port ~connector (flow, conn) request body = in let sockaddr = getsockname edn in - let (connection_closed, wakener) = Lwt.wait () in - Hashtbl.add waiters conn wakener; + + let connection_closed = + try + fst (Hashtbl.find connections conn) + with Not_found -> + let ((connection_closed, _) as p) = Lwt.wait () in + Hashtbl.add connections conn p; + connection_closed + in let handle_error exn = @@ -202,9 +209,8 @@ let conn_closed (flow, conn) = Lwt_log.ign_debug_f ~section "Connection closed:\n%s" (Cohttp.Connection.to_string conn); - let wakener = Hashtbl.find waiters conn in - Lwt.wakeup wakener (); - Hashtbl.remove waiters conn + Lwt.wakeup (snd (Hashtbl.find connections conn)) (); + Hashtbl.remove connections conn with Not_found -> () let stop, stop_wakener = Lwt.wait ()