Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lwt_stream.get leaks on cancel #56

Closed
ygrek opened this issue Apr 30, 2014 · 1 comment
Closed

Lwt_stream.get leaks on cancel #56

ygrek opened this issue Apr 30, 2014 · 1 comment

Comments

@ygrek
Copy link
Contributor

ygrek commented Apr 30, 2014

let mem_usage v =
  let open Objsize in
  let x = objsize v in
  show_info ~map:string_of_int x

let () =
  let (stream,_) = Lwt_stream.create () in
  let rec loop () =
    lwt _ = Lwt.pick [
      (lwt () = Lwt_unix.sleep 0.1 in Lwt.return ());
      (lwt _ = Lwt_stream.get stream in Lwt.return ());
    ] in
    loop ()
  in
  let tasks = Array.to_list (Array.init 10_000 (fun _ -> loop ())) in
  let nr = ref 0 in
  let rec memory () =
    lwt () = Lwt_unix.sleep 0.05 in
    Gc.compact ();
    let st = Gc.stat () in
    Printf.printf "%d) heap live %d, tasks %s\n%!" !nr st.Gc.live_words (mem_usage tasks);
    incr nr;
    memory ()
  in
  Lwt_main.run (Lwt.join (memory () :: tasks))

mem_usage requires objsize, you can remove it's usage and just observe heap growth.
Build with ocamlbuild -use-ocamlfind -tags 'syntax_camlp4o, package(lwt.syntax)' -pkgs lwt.unix,objsize lwt_leak.native

@aantron
Copy link
Collaborator

aantron commented Jun 29, 2016

Thanks for the very nice report. This is fixed in the attached commit. The underlying problem was in Lwt.protected, used internally by Lwt_stream.get. If interested, please see the commit message for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants