Skip to content

Commit 24d301e

Browse files
committed
Fix #81: Wait for error message to be logged on shutdown.
1 parent 43e236f commit 24d301e

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

src/server/ocsigen_server.ml

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,28 +1010,6 @@ let rec wait_connection use_ssl port socket =
10101010
| Some e -> handle_exn e
10111011
| None -> Lwt.return ())
10121012

1013-
let stop n fmt =
1014-
Printf.ksprintf (fun s -> Lwt_log.ign_error ~section s; exit n) fmt
1015-
1016-
(** Thread waiting for events on a the listening port *)
1017-
let listen use_ssl (addr, port) wait_end_init =
1018-
let listening_sockets =
1019-
try
1020-
let sockets = make_sockets addr port in
1021-
List.iter (fun x -> Lwt_unix.listen x 1024) sockets;
1022-
sockets
1023-
with
1024-
| Unix.Unix_error (Unix.EACCES, _, _) ->
1025-
stop 7 "Fatal - You are not allowed to use port %d." port
1026-
| Unix.Unix_error (Unix.EADDRINUSE, _, _) ->
1027-
stop 8 "Fatal - The port %d is already in use." port
1028-
| exn ->
1029-
stop 100 "Fatal - Uncaught exception: %s" (Printexc.to_string exn)
1030-
in
1031-
List.iter (fun x ->
1032-
ignore (wait_end_init >>= fun () ->
1033-
wait_connection use_ssl port x)) listening_sockets;
1034-
listening_sockets
10351013

10361014
(* fatal errors messages *)
10371015
let errmsg = function
@@ -1147,7 +1125,31 @@ let _ =
11471125
in
11481126
Ocsigen_command.register_command_function f
11491127

1150-
let start_server () = try
1128+
exception Stop of int * string
1129+
1130+
let start_server () =
1131+
let stop n fmt = Printf.ksprintf (fun s -> raise (Stop (n, s))) fmt in
1132+
(** Thread waiting for events on a the listening port *)
1133+
let listen use_ssl (addr, port) wait_end_init =
1134+
let listening_sockets =
1135+
try
1136+
let sockets = make_sockets addr port in
1137+
List.iter (fun x -> Lwt_unix.listen x 1024) sockets;
1138+
sockets
1139+
with
1140+
| Unix.Unix_error (Unix.EACCES, _, _) ->
1141+
stop 7 "Fatal - You are not allowed to use port %d." port
1142+
| Unix.Unix_error (Unix.EADDRINUSE, _, _) ->
1143+
stop 8 "Fatal - The port %d is already in use." port
1144+
| exn ->
1145+
stop 100 "Fatal - Uncaught exception: %s" (Printexc.to_string exn)
1146+
in
1147+
List.iter (fun x ->
1148+
ignore (wait_end_init >>= fun () ->
1149+
wait_connection use_ssl port x)) listening_sockets;
1150+
listening_sockets
1151+
in
1152+
try
11511153

11521154
(* initialization functions for modules (Ocsigen extensions or application
11531155
code) loaded from now on will be executed directly. *)
@@ -1181,7 +1183,6 @@ let start_server () = try
11811183
Unix.tcsetattr Unix.stdin Unix.TCSAFLUSH old_term;
11821184
raise exn
11831185
in
1184-
11851186
let run (user, group) (_, ports, sslports) (minthreads, maxthreads) s =
11861187

11871188
Ocsigen_messages.open_files ~user ~group () >>= fun () ->
@@ -1399,6 +1400,11 @@ let start_server () = try
13991400
in
14001401
launch config_servers
14011402

1402-
with e ->
1403+
with
1404+
| Stop (n, s) ->
1405+
Lwt_main.run (Lwt_log.error ~section s);
1406+
exit n
1407+
| e ->
14031408
let msg, errno = errmsg e in
1404-
stop errno "%s" msg
1409+
Lwt_main.run (Lwt_log.error ~section msg);
1410+
exit errno

0 commit comments

Comments
 (0)