[java] do not leak file descriptors when redirecting stderr to /dev/null

Summary: Turns out I forgot to close the fd returned by dup(2) so we were leaking a lot.

Reviewed By: jeremydubreil

Differential Revision: D4327389

fbshipit-source-id: 74574ac
master
Jules Villard 8 years ago committed by Facebook Github Bot
parent 1bb6401d15
commit 475f37ea03

@ -330,12 +330,15 @@ let realpath path =
| Error (code, f, arg) -> raise (Unix.Unix_error (code, f, arg)) | Error (code, f, arg) -> raise (Unix.Unix_error (code, f, arg))
(* never closed *)
let devnull = lazy (Unix.openfile "/dev/null" ~mode:[Unix.O_WRONLY])
let suppress_stderr2 f2 x1 x2 = let suppress_stderr2 f2 x1 x2 =
let restore_stderr src =
Unix.dup2 ~src ~dst:Unix.stderr;
Unix.close src in
let orig_stderr = Unix.dup Unix.stderr in let orig_stderr = Unix.dup Unix.stderr in
let silent_stderr = Unix.openfile "/dev/null" ~mode:[Unix.O_RDWR] in Unix.dup2 ~src:(Lazy.force devnull) ~dst:Unix.stderr;
let restore_stderr () =
Unix.dup2 ~src:orig_stderr ~dst:Unix.stderr;
Unix.close silent_stderr in
Unix.dup2 ~src:silent_stderr ~dst:Unix.stderr;
let f () = f2 x1 x2 in let f () = f2 x1 x2 in
protect ~f ~finally:restore_stderr let finally () = restore_stderr orig_stderr in
protect ~f ~finally

Loading…
Cancel
Save