@@ -235,55 +235,65 @@ end = struct
235235
236236 (* The monitor is exiting. Let's try and shut down the server gracefully *)
237237 let cleanup_on_exit ~exit_status ~exit_msg ~connection ~pid =
238- let msg = MonitorProt. (PleaseDie (MonitorExiting (exit_status, exit_msg))) in
239- ServerConnection. write ~msg connection;
238+ let () =
239+ try
240+ let msg = MonitorProt. (PleaseDie (MonitorExiting (exit_status, exit_msg))) in
241+ ServerConnection. write ~msg connection
242+ with Lwt_stream. Closed ->
243+ (* Connection to the server has already closed. The server is likely already dead *)
244+ ()
245+ in
240246
241247 (* The monitor waits 1 second before exiting. So let's give the server .75 seconds to shutdown
242248 * gracefully. *)
243- let % lwt server_status = Lwt. pick [
244- (let % lwt (_, status) = LwtSysUtils. blocking_waitpid pid in Lwt. return (Some status));
245- (let % lwt () = Lwt_unix. sleep 0.75 in Lwt. return None )
246- ] in
247-
248- let % lwt () = ServerConnection. close_immediately connection in
249- let still_alive = begin match server_status with
250- | Some (Unix. WEXITED exit_status ) ->
251- let exit_type =
252- try Some (FlowExitStatus. error_type exit_status)
253- with Not_found -> None
254- in
255- begin if exit_type = Some FlowExitStatus. Killed_by_monitor
256- then Logger. info " Successfully killed the server process"
257- else
258- let exit_status_string =
259- Option. value_map ~default: " Invalid_exit_code" ~f: FlowExitStatus. to_string exit_type
249+ try % lwt
250+ let % lwt server_status = Lwt. pick [
251+ (let % lwt (_, status) = LwtSysUtils. blocking_waitpid pid in Lwt. return (Some status));
252+ (let % lwt () = Lwt_unix. sleep 0.75 in Lwt. return None )
253+ ] in
254+
255+ let % lwt () = ServerConnection. close_immediately connection in
256+ let still_alive = begin match server_status with
257+ | Some (Unix. WEXITED exit_status ) ->
258+ let exit_type =
259+ try Some (FlowExitStatus. error_type exit_status)
260+ with Not_found -> None
260261 in
262+ begin if exit_type = Some FlowExitStatus. Killed_by_monitor
263+ then Logger. info " Successfully killed the server process"
264+ else
265+ let exit_status_string =
266+ Option. value_map ~default: " Invalid_exit_code" ~f: FlowExitStatus. to_string exit_type
267+ in
268+ Logger. error
269+ " Tried to kill the server process (%d), which exited with the wrong exit code: %s"
270+ pid
271+ exit_status_string
272+ end ;
273+ false
274+ | Some (Unix. WSIGNALED signal ) ->
261275 Logger. error
262- " Tried to kill the server process (%d), which exited with the wrong exit code: %s "
276+ " Tried to kill the server process (%d), but for some reason it was killed with %s signal "
263277 pid
264- exit_status_string
265- end ;
266- false
267- | Some (Unix. WSIGNALED signal ) ->
268- Logger. error
269- " Tried to kill the server process (%d), but for some reason it was killed with %s signal"
270- pid
271- (PrintSignal. string_of_signal signal);
272- false
273- | Some (Unix. WSTOPPED signal ) ->
274- Logger. error
275- " Tried to kill the server process (%d), but for some reason it was stopped with %s signal"
276- pid
277- (PrintSignal. string_of_signal signal);
278- true
279- | None ->
280- Logger. error " Tried to kill the server process (%d), but it didn't die" pid;
281- true
282- end in
278+ (PrintSignal. string_of_signal signal);
279+ false
280+ | Some (Unix. WSTOPPED signal ) ->
281+ Logger. error
282+ " Tried to kill the server process (%d), but for some reason it was stopped with %s signal"
283+ pid
284+ (PrintSignal. string_of_signal signal);
285+ true
286+ | None ->
287+ Logger. error " Tried to kill the server process (%d), but it didn't die" pid;
288+ true
289+ end in
283290
284- if still_alive then Unix. kill pid Sys. sigkill;
291+ if still_alive then Unix. kill pid Sys. sigkill;
285292
286- Lwt. return_unit
293+ Lwt. return_unit
294+ with Unix. Unix_error (Unix. ECHILD, _ , _ ) ->
295+ Logger. info " Server process has already exited. No need to kill it" ;
296+ Lwt. return_unit
287297
288298 let cleanup t =
289299 Lwt. cancel t.command_loop;
0 commit comments