Skip to content

Commit

Permalink
Fix fut_await cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
patricoferris committed Mar 8, 2023
1 parent 313cefb commit 9b2587b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib_eio_js/browser/eio_browser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ module Scheduler = struct

let stop t = Brr.Ev.unlisten t.listener

let wakeup =
(* A new message must be created for every call. *)
let wakeup t =
let open Brr_io in
let args = [| Ev.create Message.Ev.message |> Ev.to_jv |] in
fun t -> Jv.call (El.to_jv t.scheduler) "dispatchEvent" args |> ignore
Jv.call (El.to_jv t.scheduler) "dispatchEvent" args |> ignore

let enqueue_thread t k v =
Run_queue.push t.run_q (fun () -> Suspended.continue k v);
Expand Down Expand Up @@ -124,7 +125,7 @@ let await fut =
enter_unchecked @@ fun st k ->
(* There is no way to cancel a Javascript promise (which Fut wraps) so we
have to leak this memory unfortunately. *)
let cancelled = ref true in
let cancelled = ref false in
Fiber_context.set_cancel_fn k.fiber (fun exn -> cancelled := true; Scheduler.enqueue_failed_thread st k exn);
Fut.await fut (fun v ->
if not !cancelled then begin
Expand Down
7 changes: 7 additions & 0 deletions lib_eio_js/browser/test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ module Browser_tests = struct
in
Alcotest.(check string) "timeout cancelled" "B" v

let test_fut_await () =
let p, r = Fut.create () in
Fiber.both
(fun () -> Eio_browser.await p)
(fun () -> r ())

let test_fut_cancel () =
let p, _ = Fut.create () in
let v =
Expand All @@ -76,6 +82,7 @@ module Browser_tests = struct

let tests = [
Alcotest.test_case "timeout cancelled" `Quick test_timeout_cancel;
Alcotest.test_case "fut await" `Quick test_fut_await;
Alcotest.test_case "fut cancelled" `Quick test_fut_cancel;
Alcotest.test_case "test timeout" `Quick test_timeout;
Alcotest.test_case "test multiple timeouts" `Quick test_multiple_timeouts
Expand Down

0 comments on commit 9b2587b

Please sign in to comment.