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

8278326: Socket close is not thread safe and other cleanup #11863

Closed
wants to merge 6 commits into from

Conversation

AlanBateman
Copy link
Contributor

@AlanBateman AlanBateman commented Jan 5, 2023

java.net.Socket is not specified to be thread safe but it is required to support async close. If you create an unbound Socket and close it at around the time that another thread is binding, connecting, or anything else that creates the underlying socket then it can leak. The simplest thing to do is to synchronize all methods but the underlying SocketImpl implementation is thread safe, and all we really need is for Socket (and ServerSocket) to synchronize the creation of the underlying socket (SocketImpl.create) with close. As part of this change I've replaced the 6 flags with a bit mask. A new test is added to the Socket/asyncClose directory to test closing concurrently with another operation, the test will detect if the closed Socket is connected to a SocketImpl with an open socket.

Related is that ServerSocket.implAccept can be overridden to provide the Socket to accept. Its behavior is unspecified when called with a Socket that isn't newly created/unbound and there are number of silly scenarios that can arise. I've changed implAccept to coordinate with close so that accept doesn't return a closed Socket that is connected to an underlying socket. A new test is added to exercise these scenarios.

There are a couple of random cleanup/formatting nits in this patch.


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-8278326: Socket close is not thread safe and other cleanup

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 11863

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

Using diff file

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

@bridgekeeper
Copy link

bridgekeeper bot commented Jan 5, 2023

👋 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 Jan 5, 2023

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

  • net

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 net net-dev@openjdk.org label Jan 5, 2023
@AlanBateman AlanBateman marked this pull request as ready for review January 5, 2023 17:36
@openjdk openjdk bot added the rfr Pull request is ready for review label Jan 5, 2023
@mlbridge
Copy link

mlbridge bot commented Jan 5, 2023

Webrevs

@jaikiran
Copy link
Member

I've added some minor review comments, but overall the changes in this PR look good to me.

@jaikiran
Copy link
Member

With the removal of synchronized from the Socket APIs, would this require a CSR?

@AlanBateman
Copy link
Contributor Author

With the removal of synchronized from the Socket APIs, would this require a CSR?

"synchronized" isn't part of the signature and there isn't any behavior change there. The only change in behavior is in the silly (and unspecified) area of calling implAccept with a connected or closed socket.

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 in this PR look good to me.
(Leaky.java line 231 has a typo in a comment, that I think you missed, but that's a trivial thing)

@openjdk
Copy link

openjdk bot commented Jan 11, 2023

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

8278326: Socket close is not thread safe and other cleanup

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

  • f857f8a: 8299327: Allow super late barrier expansion of store barriers in C2
  • 257f667: 8296789: -completion in jshell fails to expose synthetic bridge methods
  • 030e88d: 8299829: In jshell, the output of "0".repeat(49999)+"2" ends with a '0'
  • ef32fdb: 8284825: sun/java2d/DirectX/MultiPaintEventTest/MultiPaintEventTest.java failed with "RuntimeException: Processed unnecessary paint()."

Please see this link for an up-to-date comparison between the source branch of this pull request and 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 added the ready Pull request is ready to be integrated label Jan 11, 2023
@AlanBateman
Copy link
Contributor Author

(Leaky.java line 231 has a typo in a comment, that I think you missed, but that's a trivial thing)

Well spotted, I fixed the typo in another place so missed it in Leaky.

Copy link
Member

@dfuch dfuch left a comment

Choose a reason for hiding this comment

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

This looks reasonable. It's a bit strange that shutdownInput() / shutdownOutput() are not protected by the same lock than close() but they were not before either.

@AlanBateman
Copy link
Contributor Author

It's a bit strange that shutdownInput() / shutdownOutput() are not protected by the same lock than close() but they were not before either.

Thanks for going through this. The shutdownXXX methods require the Socket to be connected so they don't create the SocketImpl or the underlying socket. It's okay for these methods to race with each other, or close, as it will be handled by the SocketImpl (NioSocketImpl).

@AlanBateman
Copy link
Contributor Author

/integrate

@openjdk
Copy link

openjdk bot commented Jan 12, 2023

Going to push as commit 4b57334.
Since your change was applied there have been 28 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 Jan 12, 2023
@openjdk openjdk bot closed this Jan 12, 2023
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Jan 12, 2023
@openjdk
Copy link

openjdk bot commented Jan 12, 2023

@AlanBateman Pushed as commit 4b57334.

💡 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
integrated Pull request has been integrated net net-dev@openjdk.org
5 participants