lwt icon indicating copy to clipboard operation
lwt copied to clipboard

How to use Lwt_unix with OCaml 5 effects?

Open aantron opened this issue 2 years ago • 4 comments

The following code is a minimized version of https://github.com/aantron/dream/issues/297 that no longer calls into Dream at all:

type _ Effect.t += E : unit Effect.t

let () =
  Effect.Deep.try_with
    begin fun () ->
      Lwt_main.run begin
        Lwt.bind (Lwt_unix.sleep 1.) @@ fun () ->
        Effect.perform E;
        assert false
      end
    end
    ()
    {
      effc = fun (type a) (e : a Effect.t) ->
        match e with
        | E ->
          Option.some @@ fun (k : (a, _) Effect.Deep.continuation) ->
            prerr_endline "handling E";
            Effect.Deep.continue k ()
        | _ -> None
    }

Running this on OCaml 5.1.0 with Lwt 5.7.0 results in

Fatal error: exception Stdlib.Effect.Unhandled(Dune__exe__Json.E)

Replacing Lwt_unix.sleep 1. by, say, Lwt.pause () results in

handling E                           
Fatal error: exception File "dream/example/e-json/json.ml", line 9, characters 8-14: Assertion failed

which is the correct output, and what I would expect in both cases.

aantron avatar Nov 12 '23 14:11 aantron

related pointers:

  • https://github.com/ocsigen/lwt/issues/897
  • https://github.com/ocsigen/lwt/pull/961

raphael-proust avatar Nov 16 '23 13:11 raphael-proust

I looked at the code linked in #961 and the comments, but I don't see why an outer effect handler would not work, and the comments in the examples suggest that it still should. So I'm surprised by the behavior of this particular example.

aantron avatar Nov 20 '23 13:11 aantron

cc @ul

aantron avatar Nov 20 '23 13:11 aantron