From ba0e5dd086a43234ab4c20755da5834075605b61 Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Fri, 5 May 2023 12:05:39 +0100 Subject: [PATCH] Deprecate Fiber.fork_sub 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. --- lib_eio/core/eio__core.mli | 9 +-------- tests/fiber.md | 2 +- tests/switch.md | 21 ++++++++++++++------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lib_eio/core/eio__core.mli b/lib_eio/core/eio__core.mli index e15b887c7..4b842da63 100644 --- a/lib_eio/core/eio__core.mli +++ b/lib_eio/core/eio__core.mli @@ -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. diff --git a/tests/fiber.md b/tests/fiber.md index 877af7681..53cdf7662 100644 --- a/tests/fiber.md +++ b/tests/fiber.md @@ -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: diff --git a/tests/switch.md b/tests/switch.md index df1fed936..d3dda6875 100644 --- a/tests/switch.md +++ b/tests/switch.md @@ -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 @@ -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 @@ -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 (); @@ -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 @@ -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" @@ -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"