From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 3F4AE7EE51 for ; Thu, 11 Apr 2013 01:35:40 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of yminsky@janestreet.com) identity=pra; client-ip=38.105.200.229; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="yminsky@janestreet.com"; x-sender="yminsky@janestreet.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of yminsky@janestreet.com designates 38.105.200.229 as permitted sender) identity=mailfrom; client-ip=38.105.200.229; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="yminsky@janestreet.com"; x-sender="yminsky@janestreet.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@tot-dmz-mxout1.janestreet.com) identity=helo; client-ip=38.105.200.229; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="yminsky@janestreet.com"; x-sender="postmaster@tot-dmz-mxout1.janestreet.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApEBANH2ZVEmacjlnGdsb2JhbABQgzyvLZIXgQUeDgEBAQEBBg0JCRQogh8BAQUnGQEBLAsBDwsLDQ0hIQESAQUBChIGExKHcAMPAwmgV4pvhDsBBYU/AwqJVwaMQ4JTB4NBlSCBY4EhilGDOhYphEo X-IPAS-Result: ApEBANH2ZVEmacjlnGdsb2JhbABQgzyvLZIXgQUeDgEBAQEBBg0JCRQogh8BAQUnGQEBLAsBDwsLDQ0hIQESAQUBChIGExKHcAMPAwmgV4pvhDsBBYU/AwqJVwaMQ4JTB4NBlSCBY4EhilGDOhYphEo X-IronPort-AV: E=Sophos;i="4.87,450,1363129200"; d="scan'208";a="10529040" Received: from mx5.janestreet.com (HELO tot-dmz-mxout1.janestreet.com) ([38.105.200.229]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 11 Apr 2013 01:35:39 +0200 Received: from tot-oib-smtp1.delacy.com ([172.27.22.15] helo=tot-smtp) by tot-dmz-mxout1.janestreet.com with esmtp (Exim 4.76) (envelope-from ) id 1UQ4YB-0001jN-Tp for caml-list@inria.fr; Wed, 10 Apr 2013 19:35:35 -0400 Received: from tot-dmz-mxgoog1.delacy.com ([172.27.224.14] helo=mxgoog2.janestreet.com) by tot-smtp with esmtps (TLSv1:AES256-SHA:256) (Exim 4.72) (envelope-from ) id 1UQ4YB-0007om-Se for caml-list@inria.fr; Wed, 10 Apr 2013 19:35:35 -0400 Received: from mail-ee0-f70.google.com ([74.125.83.70]) by mxgoog2.janestreet.com with esmtp (Exim 4.76) (envelope-from ) id 1UQ4YB-0000F4-Ot for caml-list@inria.fr; Wed, 10 Apr 2013 19:35:35 -0400 Received: by mail-ee0-f70.google.com with SMTP id e51so1225063eek.1 for ; Wed, 10 Apr 2013 16:35:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=janestreet.com; s=google; h=x-received:mime-version:x-received:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=Tmx4Dm/n8XybQVD/e7LXnJtOgBNbjA/z7IC0EBoYsXQ=; b=Z/rlwY/RrfZ9bBlpqL0LIyd4wsuZB8enxsKwgM75E0GwEG8Rx/aEAQcuv041d6f5Id 3P8EPelQshyabvGlulBoMrkeKfMVKVGdMeqQTlnnavwaObQrFwDsggMGA6wa9XLikCHR ODTLczaGNNz6VeJ9IZGtOAkUh5q4E5BZS/Wp0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:mime-version:x-received:in-reply-to:references:date :message-id:subject:from:to:cc:content-type:x-gm-message-state; bh=Tmx4Dm/n8XybQVD/e7LXnJtOgBNbjA/z7IC0EBoYsXQ=; b=E1DIdhzst8xnqY+PRBHTG/6YG21rfn05vaT29waSTLMlqF8YuAfiiUhqWAKwitb5YZ rlenaYBzkeX9UnA6pCgh2j2S38Y+cEOQj7VgUSB/UJwDmPmWRXrVuynRaJFbCVoXCdfY XemYXKvilZhLZX3TDJdov2le/AKg/X/Hw9iJOkixv2h20ZWxye8LAYnNaionOL33DDNI 14Q7crm84KZcbtSc+J1mHScU8FMmnxhNOMZ/15DL4Oz1MtN0O2R/+FJuWGo0PJJR6MBv hd5kDoSjWmt2ThZQeOlmBQJp5gk6P7ysyOrzZ82SKIxCWphbOeLB5xhFd3DSnOOyMHqP TI4w== X-Received: by 10.14.202.71 with SMTP id c47mr10371619eeo.39.1365636935224; Wed, 10 Apr 2013 16:35:35 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.14.202.71 with SMTP id c47mr10371601eeo.39.1365636935057; Wed, 10 Apr 2013 16:35:35 -0700 (PDT) Received: by 10.223.85.155 with HTTP; Wed, 10 Apr 2013 16:35:34 -0700 (PDT) In-Reply-To: <4989654.hHte10Um7f@groupon> References: <4989654.hHte10Um7f@groupon> Date: Wed, 10 Apr 2013 19:35:34 -0400 Message-ID: From: Yaron Minsky To: Chet Murthy Cc: Caml List Content-Type: text/plain; charset=ISO-8859-1 X-Gm-Message-State: ALoCoQmx5vCILiXE+SQDyH55aIivQbovjPxDljp/1cyynhNZxMrRU3sHgUnQSP5vs5L4dewGNA3zXPcGLxR9SeIqk3pmSi5n0YYQXMk5RAm0bDLFCiuGEN0eYOFPjIw9Qci0WuowSgzB+CdzBb+g6ByEHPAN/DG4LQ== Subject: Re: [Caml-list] try...finally , threads, stack-tracebacks .... in ocaml Chet, are you sure that one looses the stack trace in this case? My example using Core seems to preserve it. Here's the code: open Core.Std let a () = let _ = "a" in raise Not_found let b () = let _ = "b" in a () let c () = let _ = "c" in protect ~f:b ~finally:(fun () -> ()) let d () = let _ = "d" in c () let () = d () And here's the native code stack-trace: $ ./z.native Fatal error: exception Not_found Raised at file "z.ml", line 3, characters 32-41 Called from file "lib/exn.ml", line 63, characters 8-11 Re-raised at file "lib/exn.ml", line 66, characters 12-15 Called from file "z.ml", line 11, characters 26-30 Here's the code for protect, which is a little different than your finally, but not by a lot. Maybe the biggest difference is that we have a special exception (Finally) which we use when the finally clause throws an exception from an exception handler, so we can deliver both the exception tha triggered the [finally] and the exception thrown by the [finally]. This is from the Exn module in Core. let protectx ~f x ~(finally : _ -> unit) = let res = try f x with exn -> (try finally x with final_exn -> raise (Finally (exn, final_exn))); raise exn in finally x; res ;; let protect ~f ~finally = protectx ~f () ~finally On Wed, Apr 10, 2013 at 6:16 PM, Chet Murthy wrote: > > People have previously asked about try...finally support in Ocaml, and > it's been observed (correctly) that you can write a little combinator > to give you this support, e.g. > > let finally f arg finf = > let rv = try Inl(f arg) with e -> > Inr e > in (try finf arg rv with e -> ()); > match rv with > Inl v -> v > | Inr e -> raise e > > The problem is, you discard stack-traceback when you rethrow the > exception. One can program around this explicitly by capturing the > backtrace string and appending it to the rethrown exception, but it's > cumbersome and won't work for exceptions like Not_found that are > already defined without a mutable string slot. > > It sure would be nice of ocaml had try...finally that preserved the > traceback information properly .... though maybe it isn't possible. > Certainly in the case where the finally block doesn't raise any > exceptions itself (even those that are caught silently), it seems like > it ought to be possible. > > In an unrelated but similar sense, when programming with threads in > ocaml, it's easy (easy!) to deadlock your program. Now, I've been > writing Java programs for years, and so am aware of how careful one > must be, and I'm writing my code using a single mutex protecting the > critical section. But I forgot and didn't mutex-protect one method -- > what merely printed out the contents of a shared daa-structure, and > when that printout coincided with a thread actually mutating the > data-structure, I got a deadlock. Not hard to track down, and I > chided myself for being lax. > > But the thing is, in Java (blecch!) I would have been able to use the > "javacore" facility to get a full-thread stack-traceback, and could > have used that to get a good idea of where my deadlock was. > > I'm not saying that this is something ocaml should have, but I figured > I'd ask: are others (who use threads in ocaml) wishing for something > like this? > > --chet-- > > > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa.inria.fr/sympa/arc/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs