From cf3a38e31f6b1ea189ce8696860762ccd3bbc591 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Proust?= Date: Wed, 9 Jul 2025 15:44:40 +0200 Subject: [PATCH] Daemon: actually delay signaling exit specifically: replace `wakeup_later` which is unpredictable with pause+wakeup which is more predicatble --- daemon.ml | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/daemon.ml b/daemon.ml index c0e5343..c70c601 100644 --- a/daemon.ml +++ b/daemon.ml @@ -27,8 +27,18 @@ let should_run () = not !should_exit_ exception ShouldExit let signal_exit = - let do_lwt = lazy (Lwt.wakeup_later signal_exit_lwt ()) in - (* invariant: should_exit_ = (Lwt.state should_exit_lwt = Lwt.Return) *) + let do_lwt = lazy ( + (* we can't use Lwt's wakeup_later because it doesn't always "later", it + soemtimes behaves the same as plain wakeup *) + Lwt.dont_wait + (fun () -> + Lwt.bind + (Lwt.pause ()) + (fun () -> Lwt.wakeup signal_exit_lwt (); Lwt.return_unit)) + (fun exc -> log#error "signal exit: error at wakeup: %s" (Printexc.to_string exc)) + ) + in + (* nearly-invariant: should_exit_ = (Lwt.state should_exit_lwt = Lwt.Return) *) fun () -> should_exit_ := true; Lazy.force do_lwt (** @raise ShouldExit if [should_exit] condition is set, otherwise do nothing *)