VirtualMachineImpl.checkCatchesAndSendQuitTo leaks file handles

Laurence Cable larry.cable at oracle.com
Thu May 29 14:56:07 UTC 2025


probably makes sense to fix regardless... my bad

On 5/29/25 3:21 AM, Kevin Walls wrote:
> 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/d1e764a4/attachment-0001.htm>


More information about the serviceability-dev mailing list