Skip to content

Commit

Permalink
Deprecate Fiber.fork_sub
Browse files Browse the repository at this point in the history
Originally, you needed the parent switch to create the child one, but
that happens automatically now, so we don't need a special function for
this.
  • Loading branch information
talex5 committed May 5, 2023
1 parent df197a8 commit ba0e5dd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
9 changes: 1 addition & 8 deletions lib_eio/core/eio__core.mli
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,7 @@ module Fiber : sig
The calling fiber is placed at the head of the run queue, ahead of any previous items. *)

val fork_sub : sw:Switch.t -> on_error:(exn -> unit) -> (Switch.t -> unit) -> unit
(** [fork_sub ~sw ~on_error fn] is like [fork], but it creates a new sub-switch for the fiber.
This means that you can cancel the child switch without cancelling the parent.
This is a convenience function for running {!Switch.run} inside a {!fork}.
@param on_error This is called if the fiber raises an exception.
If it raises in turn, the parent switch is failed.
It is not called if the parent [sw] itself is cancelled. *)
[@@deprecated "Use Fiber.fork and Switch.run separately instead"]

val fork_promise : sw:Switch.t -> (unit -> 'a) -> 'a Promise.or_exn
(** [fork_promise ~sw fn] schedules [fn ()] to run in a new fiber and returns a promise for its result.
Expand Down
2 changes: 1 addition & 1 deletion tests/fiber.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ Exception: Stdlib.Exit.
Exception: Failure "simulated error".
```

# Fiber.fork
# Fiber.fork_promise

`Fiber.fork_promise ~sw` inherits the cancellation context from `sw`, not from the current fiber:

Expand Down
21 changes: 14 additions & 7 deletions tests/switch.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ open Eio.Std
let run (fn : Switch.t -> _) =
Eio_mock.Backend.run @@ fun () ->
Switch.run fn
let fork_sub ~sw ~on_error fn = Fiber.fork ~sw (fun () ->
try Switch.run fn
with
| Eio.Cancel.Cancelled _ -> ()
| ex -> on_error ex
)
```

# Test cases
Expand Down Expand Up @@ -200,8 +207,8 @@ Child switches are cancelled when the parent is cancelled, but `on_error` isn't
# run (fun sw ->
let p, _ = Promise.create () in
let on_error ex = traceln "child: %s" (Printexc.to_string ex) in
Fiber.fork_sub ~sw ~on_error (fun sw -> traceln "Child 1"; Promise.await p);
Fiber.fork_sub ~sw ~on_error (fun sw -> traceln "Child 2"; Promise.await p);
fork_sub ~sw ~on_error (fun sw -> traceln "Child 1"; Promise.await p);
fork_sub ~sw ~on_error (fun sw -> traceln "Child 2"; Promise.await p);
Switch.fail sw (Failure "Cancel parent")
);;
+Child 1
Expand All @@ -216,8 +223,8 @@ A child can fail independently of the parent:
let p1, r1 = Promise.create () in
let p2, r2 = Promise.create () in
let on_error ex = traceln "child: %s" (Printexc.to_string ex) in
Fiber.fork_sub ~sw ~on_error (fun sw -> traceln "Child 1"; Promise.await_exn p1);
Fiber.fork_sub ~sw ~on_error (fun sw -> traceln "Child 2"; Promise.await_exn p2);
fork_sub ~sw ~on_error (fun sw -> traceln "Child 1"; Promise.await_exn p1);
fork_sub ~sw ~on_error (fun sw -> traceln "Child 2"; Promise.await_exn p2);
Promise.resolve_error r1 (Failure "Child error");
Promise.resolve_ok r2 ();
Fiber.yield ();
Expand All @@ -237,7 +244,7 @@ A child can be cancelled independently of the parent:
let p, _ = Promise.create () in
let on_error ex = traceln "child: %s" (Printexc.to_string ex) in
let child = ref None in
Fiber.fork_sub ~sw ~on_error (fun sw ->
fork_sub ~sw ~on_error (fun sw ->
traceln "Child 1";
child := Some sw;
Promise.await ~sw p
Expand All @@ -258,7 +265,7 @@ A child error handler raises:
# run (fun sw ->
let p, r = Promise.create () in
let on_error = raise in
Fiber.fork_sub ~sw ~on_error (fun sw -> traceln "Child"; Promise.await_exn p);
fork_sub ~sw ~on_error (fun sw -> traceln "Child"; Promise.await_exn p);
Promise.resolve_error r (Failure "Child error escapes");
Fiber.yield ();
traceln "Not reached"
Expand All @@ -273,7 +280,7 @@ A child error handler deals with the exception:
# run (fun sw ->
let p, r = Promise.create () in
let on_error = traceln "caught: %a" Fmt.exn in
Fiber.fork_sub ~sw ~on_error (fun sw -> traceln "Child"; Promise.await_exn p);
fork_sub ~sw ~on_error (fun sw -> traceln "Child"; Promise.await_exn p);
Promise.resolve_error r (Failure "Child error is caught");
Fiber.yield ();
traceln "Still running"
Expand Down

0 comments on commit ba0e5dd

Please sign in to comment.