<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Hi --</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
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?</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I'm not seeing a leak when calling a line like this over and over in a tight loop:</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
final var cmdline = Files.lines(path).findFirst();</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Thanks!</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Kevin</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
<br>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> serviceability-dev <serviceability-dev-retn@openjdk.org> on behalf of Philippe Marschall <kustos@gmx.net><br>
<b>Sent:</b> Saturday, May 24, 2025 8:26 PM<br>
<b>To:</b> serviceability-dev@openjdk.java.net <serviceability-dev@openjdk.java.net><br>
<b>Subject:</b> VirtualMachineImpl.checkCatchesAndSendQuitTo leaks file handles</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">Hello<br>
<br>
I believe sun.tools.attach.VirtualMachineImpl#checkCatchesAndSendQuitTo <br>
on Linux leaks file handles after JDK-8327114 [1].<br>
<br>
The issue is the line 361 [2]<br>
<br>
final var cmdline = Files.lines(procPid.resolve("cmdline")).findFirst();<br>
<br>
Because the Stream is not closed the file handle is not closed as well.<br>
<br>
Compare this to ProcessHelper#getCommandLine which uses the correct idiom.<br>
<br>
try (Stream<String> lines =<br>
Files.lines(Paths.get("/proc/" + pid + "/cmdline"))) {<br>
return lines.findFirst().orElse(null);<br>
}<br>
<br>
The fix is easy, rewrite the code like so:<br>
<br>
final Optional<String> cmdline;<br>
try (var lines = Files.lines(procPid.resolve("cmdline"))) {<br>
cmdline = lines.findFirst();<br>
}<br>
<br>
If somebody opens a JIRA bug I can work on a PR.<br>
<br>
[1] <a href="https://bugs.openjdk.org/browse/JDK-8327114">https://bugs.openjdk.org/browse/JDK-8327114</a><br>
[2] <br>
<a href="https://github.com/openjdk/jdk/blob/master/src/jdk.attach/linux/classes/sun/tools/attach/VirtualMachineImpl.java#L361">https://github.com/openjdk/jdk/blob/master/src/jdk.attach/linux/classes/sun/tools/attach/VirtualMachineImpl.java#L361</a><br>
[3] <br>
<a href="https://github.com/openjdk/jdk/blob/master/src/jdk.jcmd/linux/classes/sun/tools/common/ProcessHelper.java#L117">https://github.com/openjdk/jdk/blob/master/src/jdk.jcmd/linux/classes/sun/tools/common/ProcessHelper.java#L117</a><br>
<br>
Regards<br>
Philippe<br>
</div>
</span></font></div>
</body>
</html>