caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Lwt and exceptions (2015)
@ 2015-07-07  5:40 Mike Lin
  2015-07-13 20:01 ` Andreas Hauptmann
  0 siblings, 1 reply; 2+ messages in thread
From: Mike Lin @ 2015-07-07  5:40 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 982 bytes --]

Dear all,
I've long been observing the admonishment to avoid raising native exceptions in Lwt code [1,2]. However, it has not escaped my notice that between Lwt.catch, the try%lwt syntax extension, and the fact that Lwt.bind sets up a handler for native exceptions raised in asynchronous callbacks [3], they're usually handled just as one would naively expect. Actually, I spent a little time trying to come up with a non-terribly-contrived example where using a native exception breaks the intended handling logic, and did not succeed...
Perhaps the situation has evolved over time as some of the aforementioned features have been added to Lwt. Can anyone venture to define precisely when it's wrong to raise native exceptions in Lwt code, as of 2015?
[1] https://mirage.io/wiki/tutorial-lwt[2] http://caml-list.inria.narkive.com/YRIVPWeD/lwt-and-exceptions[3] https://github.com/ocsigen/lwt/blob/bb75e9185e2cf9a71d13b5008d08dae8fdc90f4d/src/core/lwt.ml#L653
Mike
 		 	   		  

[-- Attachment #2: Type: text/html, Size: 1341 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] Lwt and exceptions (2015)
  2015-07-07  5:40 [Caml-list] Lwt and exceptions (2015) Mike Lin
@ 2015-07-13 20:01 ` Andreas Hauptmann
  0 siblings, 0 replies; 2+ messages in thread
From: Andreas Hauptmann @ 2015-07-13 20:01 UTC (permalink / raw)
  To: caml-list

On Tue, 7 Jul 2015 05:40:18 +0000
Mike Lin wrote:

> I've long been observing the admonishment to avoid raising native
> exceptions in Lwt code [1,2].
[...]
> that Lwt.bind sets up a handler for native exceptions raised in
> asynchronous callbacks [3], they're usually handled just as one would
> naively expect. Actually, I spent a little time trying to come up
> with a non-terribly-contrived example where using a native exception
> breaks the intended handling logic, and did not succeed... 

Consider the following code snippet:
----
let () =
  let open Lwt.Infix in
  let test = ref "1" in
  let help () = Lwt.return_unit in
  let help2 () =
    match Random.int 2 with
    | 0 -> Lwt.return_unit
    | _ -> Lwt.pause () >>= fun () -> Lwt.return_unit
  in
  let f _ = Lwt.fail Not_found in
(*
  let f _ = raise Not_found in
  let f _ = help () >>= fun () -> raise Not_found in
  let f _ = Lwt_main.yield () >>= fun () -> raise Not_found in
  let f _ = help2 () >>= fun () -> raise Not_found in
*)
  let g () = Lwt_unix.sleep 3. >|= fun () -> test := "2" in
  let t =
    Lwt.catch ( fun () -> Lwt.join [ f 3 ; g () ] )
      (function
      | Not_found -> Lwt_io.printl !test
      | x -> Lwt.fail x)
  in
  Lwt_main.run t
----

The program will print something different, depending if you use
'Lwt.fail' or 'raise' inside f. (version one and two of f)
The third and fourth version of f will behave differently, although
both use 'raise Not_found' and help and Lwt_main.yield have the same
signature.
Finally the fifth version will behave unpredictable (help2 could be a
function that either connects to a database or returns a cached value -
it's not a terribly-contrived example).


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-07-13 20:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-07  5:40 [Caml-list] Lwt and exceptions (2015) Mike Lin
2015-07-13 20:01 ` Andreas Hauptmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).