Skip to content

Commit

Permalink
[java] do not leak file descriptors when redirecting stderr to /dev/null
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jvillard authored and Facebook Github Bot committed Dec 14, 2016
1 parent 1bb6401 commit 475f37e
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions infer/src/base/Utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,15 @@ let realpath path =
| 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 restore_stderr src =
Unix.dup2 ~src ~dst:Unix.stderr;
Unix.close src in
let orig_stderr = Unix.dup Unix.stderr in
let silent_stderr = Unix.openfile "/dev/null" ~mode:[Unix.O_RDWR] in
let restore_stderr () =
Unix.dup2 ~src:orig_stderr ~dst:Unix.stderr;
Unix.close silent_stderr in
Unix.dup2 ~src:silent_stderr ~dst:Unix.stderr;
Unix.dup2 ~src:(Lazy.force devnull) ~dst:Unix.stderr;
let f () = f2 x1 x2 in
protect ~f ~finally:restore_stderr
let finally () = restore_stderr orig_stderr in
protect ~f ~finally

0 comments on commit 475f37e

Please sign in to comment.