caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Andreas Hauptmann <andreashauptmann@t-online.de>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Lwt and exceptions (2015)
Date: Mon, 13 Jul 2015 20:01:11 +0000	[thread overview]
Message-ID: <mo15e8$6be$1@ger.gmane.org> (raw)
In-Reply-To: <BAY176-W30339E26747DB8EBA3CCFBAB920@phx.gbl>

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).


      reply	other threads:[~2015-07-13 20:01 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-07  5:40 Mike Lin
2015-07-13 20:01 ` Andreas Hauptmann [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='mo15e8$6be$1@ger.gmane.org' \
    --to=andreashauptmann@t-online.de \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).