From b7385e492748fa78a2a37f278323816733d59b98 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Sun, 8 Jan 2012 14:52:03 -0500 Subject: [PATCH] Use Util.with_mutex/Util.with_mutex0 to avoid unbalanced locking --- TODO | 5 ----- log.ml | 10 ++++++---- relay.ml | 10 ++-------- util.ml | 12 ++++++++++++ 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/TODO b/TODO index 781d940..e69de29 100644 --- a/TODO +++ b/TODO @@ -1,5 +0,0 @@ -Running the server and test3_latency, and then starting and -interrupting test1_latency repeatedly, the server eventually corrupts -itself and will no longer let test1_latency bind and receive -messages. Sometimes test3_latency stops too. - [TENTATIVE FIX: see try/with in write_sexp in relay.ml] diff --git a/log.ml b/log.ml index 064cd2a..443f228 100644 --- a/log.ml +++ b/log.ml @@ -3,10 +3,12 @@ open Sexp let mtx = Mutex.create () let write_to_log label body = Mutex.lock mtx; - print_string label; - print_string ": "; - output_sexp_human stdout body; - print_newline (); + (try + print_string label; + print_string ": "; + output_sexp_human stdout body; + print_newline () + with _ -> ()); Mutex.unlock mtx let hook = ref write_to_log diff --git a/relay.ml b/relay.ml index 91111f6..1883aea 100644 --- a/relay.ml +++ b/relay.ml @@ -42,9 +42,7 @@ let flush_output mtx flush_control cout = match Event.poll (Event.receive flush_control) with | Some () -> () | None -> - Mutex.lock mtx; - let ok = try flush cout; true with _ -> false in - Mutex.unlock mtx; + let ok = Util.with_mutex0 mtx (fun () -> try flush cout; true with _ -> false) in if ok then (Thread.delay 0.1; loop ()) else () in loop () @@ -59,11 +57,7 @@ let relay_main peername cin cout = Str "", Str "", Str "", Str "")); let mtx = Mutex.create () in - let write_sexp s = - Mutex.lock mtx; - (try output_sexp cout s with Sys_error _ -> ()); (* TODO: try removing this *) - Mutex.unlock mtx - in + let write_sexp = Util.with_mutex mtx (output_sexp cout) in let flush_control = Event.new_channel () in ignore (Util.create_thread (endpoint_name peername ^ " flush") None (flush_output mtx flush_control) cout); diff --git a/util.ml b/util.ml index 04feb74..62ec67b 100644 --- a/util.ml +++ b/util.ml @@ -15,3 +15,15 @@ let create_thread name cleanup main initarg = | None -> ()) in Thread.create guarded_main initarg + +let with_mutex m f arg = + Mutex.lock m; + try + let result = f arg in + Mutex.unlock m; + result + with e -> + Mutex.unlock m; + raise e + +let with_mutex0 m thunk = with_mutex m thunk ()