From 8cb09c01c4cb3624f06bd2b715dedc0465dce4e9 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 8 Oct 2024 15:28:04 -0400 Subject: [PATCH 1/2] fix domain pool: block signals in background threads close #35 --- src/dpool/moonpool_dpool.ml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/dpool/moonpool_dpool.ml b/src/dpool/moonpool_dpool.ml index e396d716..6119e252 100644 --- a/src/dpool/moonpool_dpool.ml +++ b/src/dpool/moonpool_dpool.ml @@ -96,6 +96,11 @@ let domains_ : (worker_state option * Domain_.t option) Lock.t array = in a tight loop), and if nothing happens it tries to stop to free resources. *) let work_ idx (st : worker_state) : unit = + Thread.sigmask SIG_BLOCK + [ + Sys.sigpipe; Sys.sigbus; Sys.sigterm; Sys.sigint; Sys.sigusr1; Sys.sigusr2; + ] + |> ignore; let main_loop () = let continue = ref true in while !continue do From 01cdb66f1f4ea9ab7d9e037a4c1bb83c965a0753 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 9 Oct 2024 00:26:30 -0400 Subject: [PATCH 2/2] avoid recursion in dpool --- src/dpool/moonpool_dpool.ml | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/dpool/moonpool_dpool.ml b/src/dpool/moonpool_dpool.ml index 6119e252..31411991 100644 --- a/src/dpool/moonpool_dpool.ml +++ b/src/dpool/moonpool_dpool.ml @@ -15,19 +15,23 @@ module Bb_queue = struct if was_empty then Condition.broadcast self.cond; Mutex.unlock self.mutex - let pop (self : 'a t) : 'a = - Mutex.lock self.mutex; - let rec loop () = - if Queue.is_empty self.q then ( - Condition.wait self.cond self.mutex; - (loop [@tailcall]) () - ) else ( - let x = Queue.pop self.q in - Mutex.unlock self.mutex; - x - ) - in - loop () + let pop (type a) (self : a t) : a = + let module M = struct + exception Found of a + end in + try + Mutex.lock self.mutex; + while true do + if Queue.is_empty self.q then + Condition.wait self.cond self.mutex + else ( + let x = Queue.pop self.q in + Mutex.unlock self.mutex; + raise (M.Found x) + ) + done; + assert false + with M.Found x -> x end module Lock = struct