Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8289643: File descriptor leak with ProcessBuilder.startPipeline #9414

Closed

Conversation

RogerRiggs
Copy link
Contributor

@RogerRiggs RogerRiggs commented Jul 7, 2022

The ProcessBuilder.pipelineStart() implementation does not close all of the file descriptors it uses to create the pipeline of processes.

The process calling pipelineStart() is creating the pipes between the stages.
As each process is launched, the file descriptor is inherited by the child process and
the child process dups them to the respective stdin/stdout/stderr fd.
These copies of inherited file descriptors are handled correctly.

Between the launching of each Process, the file descriptor for the read-side of the pipe for the output of the previous process is kept open (in the parent process invoking pipelineStart). The file descriptor is correctly passed to the child and is dup'd to the stdin of the next process.

However, the open file descriptor in the parent is not closed after it has been used as the input for the next Process.
The fix is to close the fd after it has been used as the input of the next process.

A new test verifies that after pipelineStart is complete, the same file descriptors are open for Unix Pipes as before the test.
The test only runs on Linux using the /proc//fd filesystem to identify open file descriptors.

The bug fix is in ProcessBuilder.pipelineStart and is applicable to all platforms.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8289643: File descriptor leak with ProcessBuilder.startPipeline

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk pull/9414/head:pull/9414
$ git checkout pull/9414

Update a local copy of the PR:
$ git checkout pull/9414
$ git pull https://git.openjdk.org/jdk pull/9414/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 9414

View PR using the GUI difftool:
$ git pr show -t 9414

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/9414.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Jul 7, 2022

👋 Welcome back rriggs! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label Jul 7, 2022
@openjdk
Copy link

openjdk bot commented Jul 7, 2022

@RogerRiggs The following label will be automatically applied to this pull request:

  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the core-libs core-libs-dev@openjdk.org label Jul 7, 2022
@mlbridge
Copy link

mlbridge bot commented Jul 7, 2022

@jaikiran
Copy link
Member

jaikiran commented Jul 8, 2022

Hello Roger, the change looks OK to me. The ProcessBuilder file will need a copyright year update.

Comment on lines 120 to 130
Stream<Path> s = Files.walk(path);
return s.filter(Files::isSymbolicLink)
.map(p -> {
try {
return new PipeRecord(p, Files.readSymbolicLink(p));
} catch (IOException ioe) {
}
return new PipeRecord(p, null);
})
.filter(p1 -> p1.link().toString().startsWith("pipe:"))
.collect(Collectors.toSet());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intentionally leaking the returned DirectoryStream from Files.walk to trigger the previous failure mode or should this be auto-closed (i.e. https://errorprone.info/bugpattern/StreamResourceLeak )?

Suggested change
Stream<Path> s = Files.walk(path);
return s.filter(Files::isSymbolicLink)
.map(p -> {
try {
return new PipeRecord(p, Files.readSymbolicLink(p));
} catch (IOException ioe) {
}
return new PipeRecord(p, null);
})
.filter(p1 -> p1.link().toString().startsWith("pipe:"))
.collect(Collectors.toSet());
try (Stream<Path> s = Files.walk(path)) {
return s.filter(Files::isSymbolicLink)
.map(p -> {
try {
return new PipeRecord(p, Files.readSymbolicLink(p));
} catch (IOException ioe) {
}
return new PipeRecord(p, null);
})
.filter(p1 -> p1.link().toString().startsWith("pipe:"))
.collect(Collectors.toSet());
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@schlosna An oversight, thanks for the reminder.

@openjdk
Copy link

openjdk bot commented Jul 11, 2022

@RogerRiggs This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8289643: File descriptor leak with ProcessBuilder.startPipeline

Reviewed-by: alanb, jpai, lancea

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 82 new commits pushed to the master branch:

  • 80bd8c3: 8290504: Close streams returned by ModuleReader::list
  • 15f4b30: 8290115: ArrayCopyObject JMH has wrong package
  • 4c1cd66: 8288368: simplify code in ValueTaglet, remove redundant code
  • 6346c33: 8290826: validate-source failures after JDK-8290016
  • 604a115: 8290016: IGV: Fix graph panning when mouse dragged outside of window
  • 59e495e: 8290704: x86: TemplateTable::_new should not call eden_allocate() without contiguous allocs enabled
  • 799a2c8: 8276561: URL$DefaultFactory::PREFIX should be static final
  • 52cc6cd: 8288723: Avoid redundant ConcurrentHashMap.get call in java.time
  • 3582fd9: 8290359: Ensure that all directory streams are closed in jdk.link
  • 53fc495: 8290316: Ensure that all directory streams are closed in java.base
  • ... and 72 more: https://git.openjdk.org/jdk/compare/6882f0eb39a1a1db1393925fab4143a725a96b6a...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jul 11, 2022
@jaikiran
Copy link
Member

Hello Roger, I just noticed that the failures reported in the GitHub actions job are genuine and related to this new test. 2 separate failures:

test PipelineLeaksFD.checkForLeaks(java.util.ImmutableCollections$List12@73c167a8): failure
java.lang.AssertionError: More or fewer pipes than expected
	at org.testng.Assert.fail(Assert.java:99)
	at PipelineLeaksFD.checkForLeaks(PipelineLeaksFD.java:94)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:578)
	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:132)
	at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:599)
	at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:174)
	at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
	at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:822)
	at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at org.testng.TestRunner.privateRun(TestRunner.java:764)
	at org.testng.TestRunner.run(TestRunner.java:585)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
	at org.testng.SuiteRunner.run(SuiteRunner.java:286)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1218)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
	at org.testng.TestNG.runSuites(TestNG.java:1069)
	at org.testng.TestNG.run(TestNG.java:1037)
	at com.sun.javatest.regtest.agent.TestNGRunner.main(TestNGRunner.java:94)
	at com.sun.javatest.regtest.agent.TestNGRunner.main(TestNGRunner.java:54)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:578)
	at com.sun.javatest.regtest.agent.MainWrapper$MainThread.run(MainWrapper.java:127)
	at java.base/java.lang.Thread.run(Thread.java:1589)

and

test PipelineLeaksFD.checkForLeaks(java.util.ImmutableCollections$ListN@13c9b949): failure
java.io.UncheckedIOException: java.nio.file.NoSuchFileException: /proc/18459/fd/20
	at java.base/java.nio.file.FileTreeIterator.fetchNextIfNeeded(FileTreeIterator.java:87)
	at java.base/java.nio.file.FileTreeIterator.hasNext(FileTreeIterator.java:103)
	at java.base/java.util.Iterator.forEachRemaining(Iterator.java:132)
	at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1921)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682)
	at PipelineLeaksFD.myPipes(PipelineLeaksFD.java:130)
	at PipelineLeaksFD.checkForLeaks(PipelineLeaksFD.java:86)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:578)
	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:132)
	at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:599)
	at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:174)
	at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
	at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:822)
	at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at org.testng.TestRunner.privateRun(TestRunner.java:764)
	at org.testng.TestRunner.run(TestRunner.java:585)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
	at org.testng.SuiteRunner.run(SuiteRunner.java:286)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1218)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
	at org.testng.TestNG.runSuites(TestNG.java:1069)
	at org.testng.TestNG.run(TestNG.java:1037)
	at com.sun.javatest.regtest.agent.TestNGRunner.main(TestNGRunner.java:94)
	at com.sun.javatest.regtest.agent.TestNGRunner.main(TestNGRunner.java:54)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:578)
	at com.sun.javatest.regtest.agent.MainWrapper$MainThread.run(MainWrapper.java:127)
	at java.base/java.lang.Thread.run(Thread.java:1589)
Caused by: java.nio.file.NoSuchFileException: /proc/18459/fd/20
	at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)
	at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:106)
	at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
	at java.base/sun.nio.fs.UnixFileAttributeViews$Basic.readAttributes(UnixFileAttributeViews.java:55)
	at java.base/sun.nio.fs.UnixFileSystemProvider.readAttributes(UnixFileSystemProvider.java:148)
	at java.base/sun.nio.fs.LinuxFileSystemProvider.readAttributes(LinuxFileSystemProvider.java:99)
	at java.base/java.nio.file.Files.readAttributes(Files.java:1848)
	at java.base/java.nio.file.FileTreeWalker.getAttributes(FileTreeWalker.java:220)
	at java.base/java.nio.file.FileTreeWalker.visit(FileTreeWalker.java:277)
	at java.base/java.nio.file.FileTreeWalker.next(FileTreeWalker.java:374)
	at java.base/java.nio.file.FileTreeIterator.fetchNextIfNeeded(FileTreeIterator.java:83)
	... 39 more

@RogerRiggs
Copy link
Contributor Author

Thanks @jaikiran, I should have expected Files.walk to have to deal with changes to /proc.
I don't see a way to handle the IOE in the Files.walk stream source and switched to Files.DirectoryStream.

@AlanBateman
Copy link
Contributor

Another alternative is to use Files.find as it can be used to find the sym links in the tree, e.g.

Path dir = Path.of("/proc", ""+ProcessHandle.current().pid(), "fd");
try (Stream<Path> s = Files.find(dir, Integer.MAX_VALUE, (p, a) -> a.isSymbolicLink())) {
     :
}

@openjdk
Copy link

openjdk bot commented Jul 18, 2022

⚠️ @RogerRiggs This pull request contains merges that bring in commits not present in the target repository. Since this is not a "merge style" pull request, these changes will be squashed when this pull request in integrated. If this is your intention, then please ignore this message. If you want to preserve the commit structure, you must change the title of this pull request to Merge <project>:<branch> where <project> is the name of another project in the OpenJDK organization (for example Merge jdk:master).

@jaikiran
Copy link
Member

Hello Roger, the latest state of this PR looks fine to me. The GitHub actions job too has now passed. I have added just a couple of minor comments inline.

@RogerRiggs
Copy link
Contributor Author

/integrate

@openjdk
Copy link

openjdk bot commented Jul 21, 2022

Going to push as commit 620c8a0.
Since your change was applied there have been 83 commits pushed to the master branch:

  • 7ec0132: 8286844: com/sun/jdi/RedefineCrossEvent.java failed with 1 threads completed while VM suspended
  • 80bd8c3: 8290504: Close streams returned by ModuleReader::list
  • 15f4b30: 8290115: ArrayCopyObject JMH has wrong package
  • 4c1cd66: 8288368: simplify code in ValueTaglet, remove redundant code
  • 6346c33: 8290826: validate-source failures after JDK-8290016
  • 604a115: 8290016: IGV: Fix graph panning when mouse dragged outside of window
  • 59e495e: 8290704: x86: TemplateTable::_new should not call eden_allocate() without contiguous allocs enabled
  • 799a2c8: 8276561: URL$DefaultFactory::PREFIX should be static final
  • 52cc6cd: 8288723: Avoid redundant ConcurrentHashMap.get call in java.time
  • 3582fd9: 8290359: Ensure that all directory streams are closed in jdk.link
  • ... and 73 more: https://git.openjdk.org/jdk/compare/6882f0eb39a1a1db1393925fab4143a725a96b6a...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Jul 21, 2022
@openjdk openjdk bot closed this Jul 21, 2022
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Jul 21, 2022
@openjdk
Copy link

openjdk bot commented Jul 21, 2022

@RogerRiggs Pushed as commit 620c8a0.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@RogerRiggs RogerRiggs deleted the 8289643-pipeline-start-leak branch December 11, 2023 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs core-libs-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

5 participants