Skip to content

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

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

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)(⚠️ The fixVersion in this issue is [26] but the fixVersion in .jcheck/conf is 25, a new backport will be created when this pr is integrated.)

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 no new commits pushed to the master branch. If another commit should be pushed before you perform the /integrate command, your PR will be automatically rebased. If you prefer to avoid any potential 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.

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs [email protected] ready Pull request is ready to be integrated rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

2 participants