run epilogues register with `at_exit` upon receiving SIGINT as well

Summary:
Epilogue tasks such as closing logs or putting files back were we found them
run automatically at the end of our executables by registering them with
`at_exit`. They do not run if the program is interrupted by a signal. This diff
makes sure they are run when the user stops infer with Ctrl-C (SIGINT).

Reviewed By: cristianoc

Differential Revision: D4435575

fbshipit-source-id: c3ab702
master
Jules Villard 8 years ago committed by Facebook Github Bot
parent 6ace3838ba
commit f9ab3aa1ac

@ -141,7 +141,7 @@ let stats () =
} }
let register_report_at_exit file = let register_report_at_exit file =
Pervasives.at_exit (fun () -> Utils.register_epilogue (fun () ->
try try
let json_stats = to_json (stats ()) in let json_stats = to_json (stats ()) in
try try

@ -77,7 +77,7 @@ let create_log_file exe name_prefix outerr =
dup_formatter file_fmt outerr_fmt dup_formatter file_fmt outerr_fmt
); );
(* flush files on exit *) (* flush files on exit *)
Pervasives.at_exit (fun () -> close_log_file (lazy file_fmt) (lazy chan) (lazy file)); Utils.register_epilogue (fun () -> close_log_file (lazy file_fmt) (lazy chan) (lazy file));
(file_fmt, chan, file) (file_fmt, chan, file)
let should_setup_log_files (exe : CLOpt.exe) = match exe with let should_setup_log_files (exe : CLOpt.exe) = match exe with

@ -289,3 +289,20 @@ let compare_versions v1 v2 =
let lv1 = int_list_of_version v1 in let lv1 = int_list_of_version v1 in
let lv2 = int_list_of_version v2 in let lv2 = int_list_of_version v2 in
[%compare : int list] lv1 lv2 [%compare : int list] lv1 lv2
(* Run the epilogues when we get SIGINT (Control-C). We do not want to mask SIGINT unless at least
one epilogue has been registered, so make this value lazy. *)
let activate_run_epilogues_on_signal = lazy (
let run_epilogues_on_signal s =
F.eprintf "*** %s: Caught %s, time to die@." (Filename.basename Sys.executable_name)
(Signal.to_string s);
(* Epilogues are registered with [at_exit] so exiting will make them run. *)
exit 0 in
Signal.Expert.handle Signal.int run_epilogues_on_signal
)
let register_epilogue f =
(* We call `exit` in a bunch of places, so register the epilogues with [at_exit]. *)
Pervasives.at_exit f;
(* Register signal masking. *)
Lazy.force activate_run_epilogues_on_signal

@ -85,3 +85,7 @@ val suppress_stderr2 : ('a -> 'b -> 'c) -> 'a -> 'b -> 'c
-1 if v1 is older than v2 and 0 if they are the same version. -1 if v1 is older than v2 and 0 if they are the same version.
The versions are strings of the shape "n.m.t", the order is lexicographic. *) The versions are strings of the shape "n.m.t", the order is lexicographic. *)
val compare_versions : string -> string -> int val compare_versions : string -> string -> int
(** Register a function to run when the program exits or is interrupted. Registered functions are
run in the reverse order in which they were registered. *)
val register_epilogue : (unit -> unit) -> unit

@ -132,10 +132,10 @@ let add_profile_to_pom_in_directory dir =
let infer_pom_path = dir ^/ "pom.xml.infer" in let infer_pom_path = dir ^/ "pom.xml.infer" in
add_infer_profile maven_pom_path infer_pom_path; add_infer_profile maven_pom_path infer_pom_path;
Unix.rename ~src:maven_pom_path ~dst:saved_pom_path; Unix.rename ~src:maven_pom_path ~dst:saved_pom_path;
Pervasives.at_exit (fun () -> Unix.rename ~src:saved_pom_path ~dst:maven_pom_path); Utils.register_epilogue (fun () -> Unix.rename ~src:saved_pom_path ~dst:maven_pom_path);
Unix.rename ~src:infer_pom_path ~dst:maven_pom_path; Unix.rename ~src:infer_pom_path ~dst:maven_pom_path;
if Config.debug_mode || Config.stats_mode then if Config.debug_mode || Config.stats_mode then
Pervasives.at_exit (fun () -> Unix.rename ~src:maven_pom_path ~dst:infer_pom_path) Utils.register_epilogue (fun () -> Unix.rename ~src:maven_pom_path ~dst:infer_pom_path)
let capture ~prog ~args = let capture ~prog ~args =
while not (List.is_empty !pom_worklist); do while not (List.is_empty !pom_worklist); do

Loading…
Cancel
Save