Skip to content

Commit 2e4ed98

Browse files
yawaraminWillenbrink
authored andcommitted
Handle SIGTERM only inside Dream server
Don't install the signal handler at the module scope, to avoid polluting the user's application with potentially unused handlers.
1 parent e2d19b1 commit 2e4ed98

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

src/http/http.ml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,14 @@ let serve_with_maybe_https
673673

674674
let default_interface = "localhost"
675675
let default_port = 8080
676-
let on_sigterm =
676+
677+
(* Lazy option default to avoid side effects. *)
678+
let option_or opt default = match opt with
679+
| Some v -> v
680+
| None -> default ()
681+
682+
(* Lazy SIGTERM handler to avoid side effects. *)
683+
let on_sigterm () =
677684
let promise, resolve = Lwt.wait () in
678685
ignore (Lwt_unix.on_signal Sys.sigterm (fun _ -> Lwt.wakeup_later resolve ()));
679686
promise
@@ -687,7 +694,7 @@ let serve
687694
?(interface = default_interface)
688695
?(port = default_port)
689696
?socket_path
690-
?(stop = on_sigterm)
697+
?stop
691698
?(error_handler = Error_handler.default)
692699
?(tls = false)
693700
?certificate_file
@@ -699,7 +706,7 @@ let serve
699706
"serve"
700707
~interface
701708
~network:(network ~port ~socket_path)
702-
~stop
709+
~stop:(option_or stop on_sigterm)
703710
~error_handler
704711
~tls:(if tls then `OpenSSL else `No)
705712
?certificate_file
@@ -715,7 +722,7 @@ let run
715722
?(interface = default_interface)
716723
?(port = default_port)
717724
?socket_path
718-
?(stop = on_sigterm)
725+
?stop
719726
?(error_handler = Error_handler.default)
720727
?(tls = false)
721728
?certificate_file
@@ -762,7 +769,7 @@ let run
762769
"run"
763770
~interface
764771
~network:(network ~port ~socket_path)
765-
~stop
772+
~stop:(option_or stop on_sigterm)
766773
~error_handler
767774
~tls:(if tls then `OpenSSL else `No)
768775
?certificate_file ?key_file

0 commit comments

Comments
 (0)