Skip to content

Commit

Permalink
fix: set exit code on signal
Browse files Browse the repository at this point in the history
set the exit code to 130 when dune is terminated with a signal

Signed-off-by: Rudi Grinberg <[email protected]>

ps-id: FF217EDC-15F2-4BB2-9A37-5AB923FA61A9
  • Loading branch information
rgrinberg committed May 24, 2022
1 parent 2d1726d commit a73b066
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 21 deletions.
10 changes: 6 additions & 4 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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)
------------------
Expand Down
3 changes: 2 additions & 1 deletion bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 24 additions & 9 deletions src/dune_engine/scheduler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -993,15 +1005,15 @@ 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). *)
val run_and_cleanup : t -> (unit -> 'a Fiber.t) -> ('a, run_error) Result.t
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
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) ->
Expand Down
24 changes: 20 additions & 4 deletions src/dune_engine/scheduler.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/blackbox-tests/test-cases/signal-exit-code.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ Demonstrate the exit code once dune is interrupted by a signal
> EOF

$ dune build @all
[1]
[130]
2 changes: 1 addition & 1 deletion test/expect-tests/dune_rpc_e2e/dune_rpc_registry_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 |}]
2 changes: 1 addition & 1 deletion test/expect-tests/scheduler_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a73b066

Please sign in to comment.