Skip to content

Instantly share code, notes, and snippets.

@toolslive
Created April 3, 2014 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toolslive/9949236 to your computer and use it in GitHub Desktop.
Save toolslive/9949236 to your computer and use it in GitHub Desktop.
memory leak with Lwt.catch
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());;
@toolslive
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment