Skip to content

Commit 1fbb7fd

Browse files
Stop server on SIGTERM, SIGINT by default (#406)
* Stop server on SIGTERM by default Fix #174 * Fix variable name * 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. * Handle SIGTERM and SIGINT * Update docs --------- Co-authored-by: Sebastian Willenbrink <sebastian.willenbrink@tngtech.com>
1 parent 638ac87 commit 1fbb7fd

3 files changed

Lines changed: 19 additions & 13 deletions

File tree

example/z-playground/server/playground.ml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -479,12 +479,6 @@ let rec gc ?(initial = true) () =
479479
let () =
480480
Dream.log "Starting playground";
481481

482-
(* Stop when systemd sends SIGTERM. *)
483-
let stop, signal_stop = Lwt.wait () in
484-
Lwt_unix.on_signal Sys.sigterm (fun _signal ->
485-
Lwt.wakeup_later signal_stop ())
486-
|> ignore;
487-
488482
(* Build the base image. *)
489483
Lwt_main.run begin
490484
Lwt_io.(with_file ~mode:Output "Dockerfile" (fun channel ->
@@ -520,7 +514,7 @@ let () =
520514
Dream.html (Client.html example)
521515
in
522516

523-
Dream.run ~interface:"0.0.0.0" ~port:80 ~stop ~adjust_terminal:false
517+
Dream.run ~interface:"0.0.0.0" ~port:80 ~adjust_terminal:false
524518
@@ Dream.logger
525519
@@ Dream.router [
526520

src/dream.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2130,7 +2130,7 @@ val run :
21302130
- [~stop] is a promise that causes the server to stop accepting new
21312131
requests, and {!Dream.run} to return. Requests that have already entered
21322132
the Web application continue to be processed. The default value is a
2133-
promise that never resolves.
2133+
promise that resolves when [SIGTERM] or [SIGINT] is received.
21342134
- [~error_handler] handles all errors, both from the application, and
21352135
low-level errors. See {!section-errors} and example
21362136
{{:https://github.com/camlworks/dream/tree/master/example/9-error#folders-and-files}

src/http/http.ml

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

674674
let default_interface = "localhost"
675675
let default_port = 8080
676-
let never = fst (Lwt.wait ())
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 signal handler to avoid side effects. *)
683+
let on_termination_signal () =
684+
let term_promise, term_resolve = Lwt.wait () in
685+
let int_promise, int_resolve = Lwt.wait () in
686+
ignore (Lwt_unix.on_signal Sys.sigterm (fun _ -> Lwt.wakeup_later term_resolve ()));
687+
ignore (Lwt_unix.on_signal Sys.sigint (fun _ -> Lwt.wakeup_later int_resolve ()));
688+
Lwt.pick [term_promise; int_promise]
677689

678690
let network ~port ~socket_path =
679691
match socket_path with
@@ -684,7 +696,7 @@ let serve
684696
?(interface = default_interface)
685697
?(port = default_port)
686698
?socket_path
687-
?(stop = never)
699+
?stop
688700
?(error_handler = Error_handler.default)
689701
?(tls = false)
690702
?certificate_file
@@ -696,7 +708,7 @@ let serve
696708
"serve"
697709
~interface
698710
~network:(network ~port ~socket_path)
699-
~stop
711+
~stop:(option_or stop on_termination_signal)
700712
~error_handler
701713
~tls:(if tls then `OpenSSL else `No)
702714
?certificate_file
@@ -712,7 +724,7 @@ let run
712724
?(interface = default_interface)
713725
?(port = default_port)
714726
?socket_path
715-
?(stop = never)
727+
?stop
716728
?(error_handler = Error_handler.default)
717729
?(tls = false)
718730
?certificate_file
@@ -759,7 +771,7 @@ let run
759771
"run"
760772
~interface
761773
~network:(network ~port ~socket_path)
762-
~stop
774+
~stop:(option_or stop on_termination_signal)
763775
~error_handler
764776
~tls:(if tls then `OpenSSL else `No)
765777
?certificate_file ?key_file

0 commit comments

Comments
 (0)