Why does this code output nothing?

run.ml:

let () = 
  begin
    Stdio.eprintf "Starting Server\n";
    let forever, _ = Lwt.wait() in
    Lwt_main.run forever;
  end;;

I am building this with “dune build -w” and running it with “_build/default/bin/run.exe”

I get no output.

===

However, if I delete the

let forever, _ = Lwt.wait() in
Lwt_main.run forever;

lines, then I do get the eprintf output.

===

Question: what is going on? I’m using Stdio.eprintf “…\n”, so I’m expecting this to hit stderr. It is not clear to me why the output is being buffered / hidden.

Output is buffered by default in OCaml. If you want to force flushing, you can do it by adding a %! indicator at the end of your format string:

Stdio.eprintf "...\n%!"

Aternatively, you can call Out_channel.flush Out_channel.stderr.

Cheers,
Nicolas

5 Likes