Hi --

Just to be clear, is this an actual persistent leak that we can observe, or is 
it that we could close earlier with try-with-resources?
I'm not seeing a leak when calling a line like this over and over in a tight 
loop:
  final var cmdline = Files.lines(path).findFirst();

Thanks!
Kevin



________________________________
From: serviceability-dev <serviceability-dev-r...@openjdk.org> on behalf of 
Philippe Marschall <kus...@gmx.net>
Sent: Saturday, May 24, 2025 8:26 PM
To: serviceability-...@openjdk.java.net <serviceability-...@openjdk.java.net>
Subject: VirtualMachineImpl.checkCatchesAndSendQuitTo leaks file handles

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

Reply via email to