Skip to content

Conversation

AlanBateman
Copy link
Contributor

@AlanBateman AlanBateman commented Mar 9, 2025

Network channels in blocking mode, and the NIO based SocketImpl, have to deal with async close when there are threads blocked on the channel. Virtual threads blocked on the channel need to be unparked. On Unix systems, platform threads blocked on the channel require the file descriptor to be dup'ed to a special file descriptor and the threads signalled.

There is a bit of duplication in the implementation of the 5 channels, and in the SocketImpl. In addition, there is discussion on net-dev about an issue in AIX that will require allowing for signals to be queued (the current implementation does not require OS to support queuing of signals).

We refactor this code so that the "prepare for close" is in one place, UnixDispatcher.


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-8351458: (ch) Move preClose to UnixDispatcher (Enhancement - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 23956

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Mar 9, 2025

👋 Welcome back alanb! 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
Copy link

openjdk bot commented Mar 9, 2025

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

8351458: (ch) Move preClose to UnixDispatcher

Reviewed-by: bpb, jpai

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

  • 4cf6316: 8351414: C2: MergeStores must happen after RangeCheck smearing
  • 8a5ed47: 8350148: Native stack overflow when writing Java heap objects into AOT cache
  • 5928209: 8347405: MergeStores with reverse bytes order value
  • f984c2b: 8351505: (fs) Typo in the documentation of java.nio.file.spi.FileSystemProvider.getFileSystem()
  • ffa6340: 8351567: Jar Manifest test ValueUtf8Coding produces misleading diagnostic output
  • 8d8bd0c: 8349492: Update sun/security/pkcs12/KeytoolOpensslInteropTest.java to use a recent Openssl version
  • 73465b9: 8160327: Support for thumbnails present in APP1 marker for JPEG
  • dbdbbd4: 8348597: Update HarfBuzz to 10.4.0
  • 7999091: 8351555: Help section added in JDK-8350638 uses invalid HTML
  • 8450ae9: 8351440: Link with -reproducible on macOS
  • ... and 18 more: https://git.openjdk.org/jdk/compare/a90f323d05f1c90767823b8729b124de0bead265...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
Copy link

openjdk bot commented Mar 9, 2025

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

  • nio

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.

@AlanBateman AlanBateman changed the title 8351458: (ch) Move preclose to UnixDispatcher 8351458: (ch) Move preClose to UnixDispatcher Mar 10, 2025
@AlanBateman AlanBateman marked this pull request as ready for review March 10, 2025 14:43
@openjdk openjdk bot added the rfr Pull request is ready for review label Mar 10, 2025
@mlbridge
Copy link

mlbridge bot commented Mar 10, 2025

Webrevs

Copy link
Member

@bplb bplb left a comment

Choose a reason for hiding this comment

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

I don't see any problems. This looks like a nice cleanup.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Mar 10, 2025
*/
final void preClose(FileDescriptor fd, long reader, long writer) throws IOException {
if (NativeThread.isVirtualThread(reader) || NativeThread.isVirtualThread(writer)) {
int fdVal = JIOFDA.get(fd);
Copy link
Member

Choose a reason for hiding this comment

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

Hello Alan, previously, in the case of DatagramChannelImpl, the fdVal was a final that was determined when the channel was constructed. Now, that fdVal gets determined as and when needed and I see that FileDescriptor allows this underlying value to be changed/reset. Is there anything practical to consider here in context of a fdVal that has been reset in the FileDescriptor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

repairSocket doesn't change fd/fdVal, instead they will be connected to a new socket if the method succeeds (or the old socket if it fails).

Copy link
Member

Choose a reason for hiding this comment

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

It was the FileDescriptor.set(int fd) method which I had in mind. But I now looked at its references and I see that there aren't any in the code paths that this PR deals with. So I believe the proposed change looks good.

* dup'ed to a special fd and the thread signalled so that the syscall fails with EINTR.
*/
final void preClose(FileDescriptor fd, long reader, long writer) throws IOException {
if (NativeThread.isVirtualThread(reader) || NativeThread.isVirtualThread(writer)) {
Copy link
Member

Choose a reason for hiding this comment

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

There used to be a if (reader != 0 || writer != 0) {...} check before doing any of this pre-close work, in DatagramChannelImpl. Is that no longer needed?

Copy link
Member

Choose a reason for hiding this comment

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

There used to be a if (reader != 0 || writer != 0) {...} check before doing any of this pre-close work, in DatagramChannelImpl. Is that no longer needed?

Looking at the implementation of NativeThread.isNativeThread() and NativeThread.isVirtualThread(), it already implicitly takes into account the != 0 check, so I guess the previous explicit checks before calling those methods was redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, there are a several places where check for != 0 would be redundant.

Copy link
Member

@jaikiran jaikiran left a comment

Choose a reason for hiding this comment

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

The changes look good to me.

Copy link
Member

@Michael-Mc-Mahon Michael-Mc-Mahon left a comment

Choose a reason for hiding this comment

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

So, on Unix, the fd needs to be dup'ed and the native thread signalled. On Windows, the dup() operation does not happen, but the threads were being signalled previously, but not any more. Was that redundant previously?

@AlanBateman
Copy link
Contributor Author

So, on Unix, the fd needs to be dup'ed and the native thread signalled. On Windows, the dup() operation does not happen, but the threads were being signalled previously, but not any more. Was that redundant previously?

There's never been any signalling of threads on Windows, NativeThread::isNativeThread always returns false. So no change on Windows.

@AlanBateman
Copy link
Contributor Author

Thanks for the reviews/comments.

@AlanBateman
Copy link
Contributor Author

/integrate

@openjdk
Copy link

openjdk bot commented Mar 11, 2025

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

  • cd9f1d3: 8286204: [Accessibility,macOS,VoiceOver] VoiceOver reads the spinner value 10 as 1 when user iterates to 10 for the first time on macOS
  • 4cf6316: 8351414: C2: MergeStores must happen after RangeCheck smearing
  • 8a5ed47: 8350148: Native stack overflow when writing Java heap objects into AOT cache
  • 5928209: 8347405: MergeStores with reverse bytes order value
  • f984c2b: 8351505: (fs) Typo in the documentation of java.nio.file.spi.FileSystemProvider.getFileSystem()
  • ffa6340: 8351567: Jar Manifest test ValueUtf8Coding produces misleading diagnostic output
  • 8d8bd0c: 8349492: Update sun/security/pkcs12/KeytoolOpensslInteropTest.java to use a recent Openssl version
  • 73465b9: 8160327: Support for thumbnails present in APP1 marker for JPEG
  • dbdbbd4: 8348597: Update HarfBuzz to 10.4.0
  • 7999091: 8351555: Help section added in JDK-8350638 uses invalid HTML
  • ... and 19 more: https://git.openjdk.org/jdk/compare/a90f323d05f1c90767823b8729b124de0bead265...master

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Mar 11, 2025

@AlanBateman Pushed as commit 0de2cdd.

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

@shruacha1234
Copy link
Contributor

/backport jdk21u-dev

@openjdk
Copy link

openjdk bot commented Jul 29, 2025

@shruacha1234 Could not automatically backport 0de2cddf to openjdk/jdk21u-dev due to conflicts in the following files:

  • src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java
  • src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java
  • src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java
  • src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java

Please fetch the appropriate branch/commit and manually resolve these conflicts by using the following commands in your personal fork of openjdk/jdk21u-dev. Note: these commands are just some suggestions and you can use other equivalent commands you know.

# Fetch the up-to-date version of the target branch
$ git fetch --no-tags https://git.openjdk.org/jdk21u-dev.git master:master

# Check out the target branch and create your own branch to backport
$ git checkout master
$ git checkout -b backport-shruacha1234-0de2cddf-master

# Fetch the commit you want to backport
$ git fetch --no-tags https://git.openjdk.org/jdk.git 0de2cddf3a7be23f67af93972875af1235f3107e

# Backport the commit
$ git cherry-pick --no-commit 0de2cddf3a7be23f67af93972875af1235f3107e
# Resolve conflicts now

# Commit the files you have modified
$ git add files/with/resolved/conflicts
$ git commit -m 'Backport 0de2cddf3a7be23f67af93972875af1235f3107e'

Once you have resolved the conflicts as explained above continue with creating a pull request towards the openjdk/jdk21u-dev with the title Backport 0de2cddf3a7be23f67af93972875af1235f3107e.

Below you can find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 0de2cddf from the openjdk/jdk repository.

The commit being backported was authored by Alan Bateman on 11 Mar 2025 and was reviewed by Brian Burkhalter and Jaikiran Pai.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integrated Pull request has been integrated nio [email protected]
Development

Successfully merging this pull request may close these issues.

5 participants