Skip to content

Conversation

@tstuefe
Copy link
Member

@tstuefe tstuefe commented May 19, 2025

Hi, please consider the following patch.

This patch replaces the existing close-file-descriptors-logic we follow before exec'ing a target binary: instead of explicitly closing the file descriptors, we mark them as CLOEXEC. That simplifies the logic: it gets rid of the awkward tiptoeing around the fact that we need to keep alive a few file descriptors: the fail pipe fd needs to be kept open right up to the exec(), and we cause opening internal file descriptors during our iteration of open file handles from /proc.

This patch also makes future developments easier: I am working on improving logging during child process spawning (https://bugs.openjdk.org/browse/JDK-8357100), and there we have a similar problem where we need to keep a logfile fd open right up to the point exec() happens).

Note: Using fcntl() with FD_CLOEXEC should work on all our POSIX platforms, since we rely on it already, see unconditional use of that flag here:

if (fcntl(FAIL_FILENO, F_SETFD, FD_CLOEXEC) == -1)
goto WhyCantJohnnyExec;

This patch also fixes two subtle bugs:

  • we didn't check the return value of the close() inside closeAllFileDescriptors
  • the final fcntl for the fail pipe was subtly wrong (should have or'd the FD_CLOEXEC flag with the existing state before setting it)

Testing:

We already have the PipelineLeak test, but I also added a new test that checks that we don't accidentally leak file descriptors even if those had been opened outside the JVM and without FD_CLOEXEC.

  • in the parent JVM, the test opens a file in native code without FD_CLOEXEC
  • test then spawns a child program that checks that no file descriptors beyond the expected stdin/out/err are open

I verified that the test correctly detects a broken implementation that leaks file descriptors.

I verified that with this patch, we close all file descriptors. I also verified the fallback path (where we brute-force-iterate all descriptors up to _SC_OPEN_MAX).

I ran manually all tests from test/jdk/java/base/Process*, and verified that these tests run as part of the GHAs, which are green.


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-8210549: Runtime.exec: in closeDescriptors(), use FD_CLOEXEC instead of close() (Enhancement - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 25301

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented May 19, 2025

👋 Welcome back stuefe! 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.

@tstuefe tstuefe force-pushed the JDK-8210549-Runtime-exec-in-closeDescriptors-use-FD_CLOEXEC-instead-of-close- branch from 4eef7ba to cc80bb3 Compare May 19, 2025 12:23
@openjdk
Copy link

openjdk bot commented May 19, 2025

@tstuefe 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:

8210549: Runtime.exec: in closeDescriptors(), use FD_CLOEXEC instead of close()

Reviewed-by: rriggs

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 48 new commits pushed to the master branch:

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 changed the title JDK-8210549: Runtime.exec: in closeDescriptors(), use FD_CLOEXEC instead of close() 8210549: Runtime.exec: in closeDescriptors(), use FD_CLOEXEC instead of close() May 19, 2025
@openjdk
Copy link

openjdk bot commented May 19, 2025

@tstuefe 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 May 19, 2025
@tstuefe tstuefe force-pushed the JDK-8210549-Runtime-exec-in-closeDescriptors-use-FD_CLOEXEC-instead-of-close- branch 4 times, most recently from 3e17175 to cd9c002 Compare May 20, 2025 04:16
@tstuefe tstuefe force-pushed the JDK-8210549-Runtime-exec-in-closeDescriptors-use-FD_CLOEXEC-instead-of-close- branch from cd9c002 to c5dc302 Compare May 20, 2025 04:48
@tstuefe tstuefe marked this pull request as ready for review May 20, 2025 07:28
@openjdk openjdk bot added the rfr Pull request is ready for review label May 20, 2025
@mlbridge
Copy link

mlbridge bot commented May 20, 2025

Webrevs

@tstuefe
Copy link
Member Author

tstuefe commented May 21, 2025

Ping @RogerRiggs ? And @simonis, perhaps?

Copy link
Contributor

@RogerRiggs RogerRiggs left a comment

Choose a reason for hiding this comment

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

Looks good

@openjdk openjdk bot added the ready Pull request is ready to be integrated label May 21, 2025
@tstuefe
Copy link
Member Author

tstuefe commented May 22, 2025

@RogerRiggs Thank you. I hold this up a few days until JDK26 since its not super critical. Would one review be sufficient or do I need more?

@RogerRiggs
Copy link
Contributor

Leaving it to 26 is good. One review is ok.
If we were pushing for JDK 25, a second review would be warranted.

@openjdk
Copy link

openjdk bot commented Jun 5, 2025

@tstuefe this pull request can not be integrated into master due to one or more merge conflicts. To resolve these merge conflicts and update this pull request you can run the following commands in the local repository for your personal fork:

git checkout JDK-8210549-Runtime-exec-in-closeDescriptors-use-FD_CLOEXEC-instead-of-close-
git fetch https://git.openjdk.org/jdk.git master
git merge FETCH_HEAD
# resolve conflicts and follow the instructions given by git merge
git commit -m "Merge master"
git push

@openjdk openjdk bot added merge-conflict Pull request has merge conflict with target branch and removed ready Pull request is ready to be integrated labels Jun 5, 2025
@openjdk openjdk bot removed the merge-conflict Pull request has merge conflict with target branch label Jun 13, 2025
Comment on lines 52 to 59
// - a library invoked with -agentlib that ensures that, in the parent JVM, we open a native fd without setting
// FD_CLOEXEC (libFDLeaker.c). This is necessary because there is no way to do this from Java: if Java functions
// correctly, all files the user could open via its APIs should be marked with FD_CLOEXEC.
// - a small native executable that tests - without using /proc - whether any file descriptors other than
// stdin/out/err are open.
//
// What should happen: In the child process, between the initial fork and the exec of the target binary, we should
// close all filedescriptors that are not stdin/out/err. If that works, the child process should not see any other
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you shorten the long lines to < 100 chars. It makes side-by-side reviews easier.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

Copy link
Member Author

Choose a reason for hiding this comment

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

@RogerRiggs need a re-review

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jun 16, 2025
@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Jun 17, 2025
Copy link
Contributor

@RogerRiggs RogerRiggs left a comment

Choose a reason for hiding this comment

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

Thanks for the updates.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jun 17, 2025
@tstuefe
Copy link
Member Author

tstuefe commented Jun 17, 2025

Thank you, Roger!

/integrate

@openjdk
Copy link

openjdk bot commented Jun 17, 2025

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

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Jun 17, 2025

@tstuefe Pushed as commit afa52e4.

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

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.

2 participants