Skip to content

Commit

Permalink
Fix signals tests
Browse files Browse the repository at this point in the history
  • Loading branch information
talex5 committed Jan 24, 2023
1 parent 218ef10 commit a7083e5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib_eio_luv/eio_luv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1332,5 +1332,5 @@ let stop_signal_thread (tid, omask, outp) =

let run main =
let sigctx = start_signal_thread () in
Fun.protect (fun () -> run2 stdenv)
Fun.protect (fun () -> run2 main)
~finally:(fun () -> stop_signal_thread sigctx)
40 changes: 23 additions & 17 deletions tests/signal.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,38 @@

```ocaml
# #require "eio_main";;
# open Eio.Std;;
```

# Test cases

Prove we can catch sigint:
```ocaml
# Eio_main.run @@ fun _stdenv ->
let got_sigint = ref false in
let interrupted = Eio.Condition.create () in
let old = Sys.signal Sys.sigint
(Signal_handle (fun _num -> got_sigint := true))
(Signal_handle (fun num -> if num = Sys.sigint then Eio.Condition.broadcast interrupted))
in
let ppid = Unix.getpid () in
let () = match Unix.fork () with
| 0 ->
Unix.kill ppid Sys.sigint;
Unix._exit 0
| child_pid ->
let wait () =
let pid, status = Unix.waitpid [] child_pid in
assert (pid = child_pid);
assert (status = (Unix.WEXITED 0))
in
try wait () with Unix.Unix_error (Unix.EINTR, _, _) -> wait ()
in
Eio.Std.traceln "got_sigint = %b" !got_sigint;
Fiber.both
(fun () ->
Eio.Condition.await_no_mutex interrupted;
traceln "interrupted!";
)
(fun () ->
let ppid = Unix.getpid () in
match Unix.fork () with
| 0 ->
Unix.kill ppid Sys.sigint;
Unix._exit 0
| child_pid ->
let wait () =
let pid, status = Unix.waitpid [] child_pid in
assert (pid = child_pid);
assert (status = (Unix.WEXITED 0))
in
try wait () with Unix.Unix_error (Unix.EINTR, _, _) -> wait ()
);
Sys.set_signal Sys.sigint old;;
+got_sigint = true
+interrupted!
- : unit = ()
```

0 comments on commit a7083e5

Please sign in to comment.