VirtualMachineImpl.checkCatchesAndSendQuitTo leaks file handles
Kevin Walls
kevin.walls at oracle.com
Thu May 29 10:21:16 UTC 2025
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-retn at openjdk.org> on behalf of Philippe Marschall <kustos at gmx.net>
Sent: Saturday, May 24, 2025 8:26 PM
To: serviceability-dev at openjdk.java.net <serviceability-dev at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/serviceability-dev/attachments/20250529/f6b75092/attachment.htm>
More information about the serviceability-dev
mailing list