From b2085068410558b1f241a8579e37cba223156d12 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 26 Jun 2024 11:20:29 -0400 Subject: [PATCH] cancellation in main --- src/fib/fiber.ml | 14 ++++++++------ src/fib/main.ml | 4 +++- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/fib/fiber.ml b/src/fib/fiber.ml index b298c243..3ae33478 100644 --- a/src/fib/fiber.ml +++ b/src/fib/fiber.ml @@ -10,7 +10,7 @@ type cancel_callback = Exn_bt.t -> unit let prom_of_fut : 'a Fut.t -> 'a Fut.promise = Fut.Private_.unsafe_promise_of_fut -module Private_ = struct +module Private0 = struct type 'a t = { id: Handle.t; (** unique identifier for this fiber *) state: 'a state A.t; (** Current state in the lifetime of the fiber *) @@ -40,13 +40,9 @@ module Private_ = struct match A.get self.state with | Alive _ -> false | Terminating_or_done _ -> true - - let cancel_from_outside (self : _ t) ebt : unit = - Fut.fulfill_idempotent (Fut.Private_.unsafe_promise_of_fut self.res) - @@ Error ebt end -include Private_ +include Private0 let create_ ~ls ~runner () : 'a t = let id = Handle.generate_fresh () in @@ -321,3 +317,9 @@ let yield () : unit = check_if_cancelled_ self; Suspend_.yield (); check_if_cancelled_ self + +module Private_ = struct + include Private0 + + let cancel_from_outside = resolve_as_failed_ +end diff --git a/src/fib/main.ml b/src/fib/main.ml index 26112015..b31f8711 100644 --- a/src/fib/main.ml +++ b/src/fib/main.ml @@ -8,7 +8,9 @@ let main (f : Runner.t -> 'a) : 'a = Fiber.on_result fiber (fun _ -> Runner.shutdown_without_waiting runner); (* run the main thread *) Fifo_pool.Private_.run_thread st runner ~on_exn:(fun e bt -> - raise (Oh_no (Exn_bt.make e bt))); + let ebt = Exn_bt.make e bt in + Fiber.Private_.cancel_from_outside fiber ebt; + raise (Oh_no ebt)); match Fiber.peek fiber with | Some (Ok x) -> x | Some (Error ebt) -> Exn_bt.raise ebt