Created
April 3, 2014 06:26
-
-
Save toolslive/9949236 to your computer and use it in GitHub Desktop.
memory leak with Lwt.catch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Lwt | |
let do_something i = | |
let x = Random.int 100 in | |
Lwt_io.printlf "%i: x = %i" i x >>= fun () -> | |
if (x < 2) | |
then Lwt.fail (Failure "kaboom") | |
else Lwt.return () | |
let rec loop1 i = | |
Lwt.catch | |
(fun () -> | |
do_something i >>= fun () -> | |
loop1 (i+1)) | |
(fun exn -> | |
Lwt_io.printlf "%s" (Printexc.to_string exn) >>= fun () -> | |
loop1 (i+1) | |
) | |
let rec loop2 i = | |
Lwt.catch | |
(fun () -> do_something i) | |
(fun exn -> Lwt_io.printlf "%s" (Printexc.to_string exn)) | |
>>= fun () -> | |
loop2 (i+1) | |
let main () = loop2 0 | |
let () = Lwt_main.run (main());; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you run it with loop1, the memory keeps on growing.
The annotations (C-c C-f) tell you the recursive call on line 14 is a tail call.