VirtualMachineImpl.checkCatchesAndSendQuitTo leaks file handles

Philippe Marschall kustos at gmx.net
Sat May 24 19:26:54 UTC 2025


Hello

I believe sun.tools.attach.VirtualMachineImpl#checkCatchesAndSendQuitTo 
on Linux leaks file handles after JDK-8327114 [1].

The issue is the line 361 [2]

final var cmdline = Files.lines(procPid.resolve("cmdline")).findFirst();

Because the Stream is not closed the file handle is not closed as well.

Compare this to ProcessHelper#getCommandLine which uses the correct idiom.

try (Stream<String> lines =
     Files.lines(Paths.get("/proc/" + pid + "/cmdline"))) {
     return lines.findFirst().orElse(null);
}

The fix is easy, rewrite the code like so:

final Optional<String> cmdline;
try (var lines = Files.lines(procPid.resolve("cmdline"))) {
     cmdline = lines.findFirst();
}

If somebody opens a JIRA bug I can work on a PR.

  [1] https://bugs.openjdk.org/browse/JDK-8327114
  [2] 
https://github.com/openjdk/jdk/blob/master/src/jdk.attach/linux/classes/sun/tools/attach/VirtualMachineImpl.java#L361
  [3] 
https://github.com/openjdk/jdk/blob/master/src/jdk.jcmd/linux/classes/sun/tools/common/ProcessHelper.java#L117

Regards
Philippe


More information about the serviceability-dev mailing list