diff --git a/CHANGES.md b/CHANGES.md index 66a0de7ce27..cce17b4c7fd 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,8 +1,10 @@ -3.3.0 (unreleased) ------------------- +Unreleased +---------- -- Support new locations of unix, str, dynlink in OCaml >= 5.0 - (#5582, @dra27) +- Set the exit code to 130 when dune is terminated with a signal (#5769, fixes + #5757) + +- Support new locations of unix, str, dynlink in OCaml >= 5.0 (#5582, @dra27) 3.2.0 (17-05-2022) ------------------ diff --git a/bin/main.ml b/bin/main.ml index 0bbdec878d5..f16cd067410 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -92,7 +92,8 @@ let () = | `Error _ -> exit 1 | _ -> exit 0 with - | Scheduler.Run.Shutdown_requested -> exit 0 + | Scheduler.Run.Shutdown.E Requested -> exit 0 + | Scheduler.Run.Shutdown.E (Signal _) -> exit 130 | exn -> let exn = Exn_with_backtrace.capture exn in Dune_util.Report_error.report exn; diff --git a/src/dune_engine/scheduler.ml b/src/dune_engine/scheduler.ml index 7656ac707d5..5b636beae07 100644 --- a/src/dune_engine/scheduler.ml +++ b/src/dune_engine/scheduler.ml @@ -113,6 +113,18 @@ module Signal = struct let name t = Signal.name (to_int t) end +module Shutdown = struct + module Signal = Signal + + module Reason = struct + type t = + | Requested + | Signal of Signal.t + end + + exception E of Reason.t +end + module Thread : sig val spawn : (unit -> 'a) -> unit @@ -993,7 +1005,7 @@ let prepare (config : Config.t) ~(handler : Handler.t) = module Run_once : sig type run_error = | Already_reported - | Shutdown_requested + | Shutdown_requested of Shutdown.Reason.t | Exn of Exn_with_backtrace.t (** Run the build and clean up after it (kill any stray processes etc). *) @@ -1001,7 +1013,7 @@ module Run_once : sig end = struct type run_error = | Already_reported - | Shutdown_requested + | Shutdown_requested of Shutdown.Reason.t | Exn of Exn_with_backtrace.t exception Abort of run_error @@ -1081,11 +1093,14 @@ end = struct filesystem_watcher_terminated (); raise (Abort Already_reported) | Yield ivar -> [ Fill (ivar, ()) ] - | Shutdown signal -> ( + | Shutdown signal -> got_signal signal; - match signal with - | Shutdown -> raise (Abort Shutdown_requested) - | _ -> raise (Abort Already_reported)) + raise + @@ Abort + (Shutdown_requested + (match signal with + | Shutdown -> Requested + | Signal s -> Signal s)) let run t f : _ result = let fiber = @@ -1177,6 +1192,8 @@ let wait_for_build_input_change t = module Run = struct exception Build_cancelled = Build_cancelled + module Shutdown = Shutdown + type file_watcher = | Automatic | No_watcher @@ -1271,8 +1288,6 @@ module Run = struct in loop () - exception Shutdown_requested - let go config ?timeout ?(file_watcher = No_watcher) ~(on_event : Config.t -> Handler.Event.t -> unit) run = let events, prepare = prepare config ~handler:on_event in @@ -1312,7 +1327,7 @@ module Run = struct in match Run_once.run_and_cleanup t run with | Ok a -> Result.Ok a - | Error Shutdown_requested -> Error (Shutdown_requested, None) + | Error (Shutdown_requested reason) -> Error (Shutdown.E reason, None) | Error Already_reported -> Error (Dune_util.Report_error.Already_reported, None) | Error (Exn exn_with_bt) -> diff --git a/src/dune_engine/scheduler.mli b/src/dune_engine/scheduler.mli index f320d3b7cf8..24cdb4bc9fa 100644 --- a/src/dune_engine/scheduler.mli +++ b/src/dune_engine/scheduler.mli @@ -53,10 +53,26 @@ module Run : sig | Automatic | No_watcher - (** Raised when [go] terminates due to the user requesting a shutdown via rpc. - The caller needs to know about this to set the exit code to 0 for such - cases *) - exception Shutdown_requested + module Shutdown : sig + module Signal : sig + (* TODO move this stuff into stdune? *) + type t = + | Int + | Quit + | Term + end + + module Reason : sig + type t = + | Requested + | Signal of Signal.t + end + + (** Raised when [go] terminates due to the user requesting a shutdown via + rpc or raising a signal. The caller needs to know about this to set the + exit code correctly *) + exception E of Reason.t + end exception Build_cancelled diff --git a/test/blackbox-tests/test-cases/signal-exit-code.t b/test/blackbox-tests/test-cases/signal-exit-code.t index b401ca89f20..a81ee4bdc1b 100644 --- a/test/blackbox-tests/test-cases/signal-exit-code.t +++ b/test/blackbox-tests/test-cases/signal-exit-code.t @@ -11,4 +11,4 @@ Demonstrate the exit code once dune is interrupted by a signal > EOF $ dune build @all - [1] + [130] diff --git a/test/expect-tests/dune_rpc_e2e/dune_rpc_registry_test.ml b/test/expect-tests/dune_rpc_e2e/dune_rpc_registry_test.ml index ba6fb06f63f..9f02b233769 100644 --- a/test/expect-tests/dune_rpc_e2e/dune_rpc_registry_test.ml +++ b/test/expect-tests/dune_rpc_e2e/dune_rpc_registry_test.ml @@ -124,5 +124,5 @@ let%expect_test "turn on dune watch and wait until the connection is listed" = run case; [%expect {| - $PATH/dune build --passive-watch-mode --root . returned 1 + $PATH/dune build --passive-watch-mode --root . returned 130 [PASS] found . at unix:path=%24CWD/_build/.rpc/dune |}] diff --git a/test/expect-tests/scheduler_tests.ml b/test/expect-tests/scheduler_tests.ml index 2926d63dfc1..374fb3ea51a 100644 --- a/test/expect-tests/scheduler_tests.ml +++ b/test/expect-tests/scheduler_tests.ml @@ -13,7 +13,7 @@ let go f = in try Scheduler.Run.go config ~file_watcher:No_watcher ~on_event:(fun _ _ -> ()) f - with Scheduler.Run.Shutdown_requested -> () + with Scheduler.Run.Shutdown.E Requested -> () let true_ = Bin.which "true" ~path:(Env.path Env.initial) |> Option.value_exn