From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id B59E07EEF8 for ; Mon, 13 Jul 2015 22:01:21 +0200 (CEST) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of andreashauptmann@t-online.de) identity=pra; client-ip=80.91.229.3; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="gclci-caml-list@m.gmane.org"; x-sender="andreashauptmann@t-online.de"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of gclci-caml-list@m.gmane.org designates 80.91.229.3 as permitted sender) identity=mailfrom; client-ip=80.91.229.3; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="gclci-caml-list@m.gmane.org"; x-sender="gclci-caml-list@m.gmane.org"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of postmaster@plane.gmane.org designates 80.91.229.3 as permitted sender) identity=helo; client-ip=80.91.229.3; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="gclci-caml-list@m.gmane.org"; x-sender="postmaster@plane.gmane.org"; x-conformance=sidf_compatible; x-record-type="v=spf1" X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0C5AwDdF6RVgQPlW1Bbg2dpvTiFf4E/TAEBAQEBARIBARYzLoQkAQQBJxNECwtGEEeIPwwBzAuLTIRTUIQVBZQxhGmJH4Y9kCqBbwELNxyBU2+CSwEBAQ X-IPAS-Result: A0C5AwDdF6RVgQPlW1Bbg2dpvTiFf4E/TAEBAQEBARIBARYzLoQkAQQBJxNECwtGEEeIPwwBzAuLTIRTUIQVBZQxhGmJH4Y9kCqBbwELNxyBU2+CSwEBAQ X-IronPort-AV: E=Sophos;i="5.15,465,1432591200"; d="scan'208";a="170070879" Received: from plane.gmane.org ([80.91.229.3]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/AES256-SHA; 13 Jul 2015 22:01:20 +0200 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1ZEjug-0001UW-81 for caml-list@inria.fr; Mon, 13 Jul 2015 22:01:18 +0200 Received: from p548c9808.dip0.t-ipconnect.de ([84.140.152.8]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 13 Jul 2015 22:01:18 +0200 Received: from andreashauptmann by p548c9808.dip0.t-ipconnect.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 13 Jul 2015 22:01:18 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: caml-list@inria.fr From: Andreas Hauptmann Date: Mon, 13 Jul 2015 20:01:11 +0000 Organization: - Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: p548c9808.dip0.t-ipconnect.de In-Reply-To: X-Archive: encrypt Archive: no X-Validation-by: andreashauptmann@t-online.de Subject: Re: [Caml-list] Lwt and exceptions (2015) 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).