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

8278270: ServerSocket is not thread safe #6712

Closed
wants to merge 2 commits into from

Conversation

AlanBateman
Copy link
Contributor

@AlanBateman AlanBateman commented Dec 5, 2021

There are several thread safety issues in java.net.ServerSocket, issues that go back to at least JDK 1.4.

The issue of most concern is async close of a ServerSocket that is initially created unbound and where close may be called at or around the time the underlying SocketImpl is created or the socket is bound.

The summary of the changes are:

  1. The "impl" field is changed to be final field.
  2. The closeLock is renamed to stateLock and is required to change the (now volatile) created, bound or closed fields.
  3. The needless synchronization has been removed from xxxSoTimeout and xxxReceiveBufferSize.

There are many redundant checks for isClosed() and other state that could be removed. Removing them would subtle change the exception thrown when there are two or more failure conditions. So they are left as is.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/6712/head:pull/6712
$ git checkout pull/6712

Update a local copy of the PR:
$ git checkout pull/6712
$ git pull https://git.openjdk.java.net/jdk pull/6712/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 6712

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/6712.diff

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented Dec 5, 2021

👋 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 openjdk bot commented Dec 5, 2021

@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 label Dec 5, 2021
@AlanBateman AlanBateman marked this pull request as ready for review Dec 6, 2021
@openjdk openjdk bot added the rfr label Dec 6, 2021
@mlbridge
Copy link

@mlbridge mlbridge bot commented Dec 6, 2021

Webrevs

if (created)
impl.close();
closed = true;
synchronized (stateLock) {
Copy link
Contributor

@shipilev shipilev Dec 6, 2021

Choose a reason for hiding this comment

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

Maybe makes some sense to check closed outside the lock here? Looks like we don't need to re-acquire the lock for repeated close() calls.

Copy link
Contributor Author

@AlanBateman AlanBateman Dec 6, 2021

Choose a reason for hiding this comment

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

That would be okay too although I wouldn't expect it is very common to attempt to close a ServerSocket more than once (the only scenario that comes to mind is where there is an async close which triggers a shutdown to server and the shutdown invokes close too).

@fweimer-rh
Copy link

@fweimer-rh fweimer-rh commented Dec 6, 2021

To what extent is ServerSocket required to be thread-safe? I don't think it's part of the specification.

This question applies to the other socket classes as well. For example, there is quite a bit of code in SSLSocket to make it thread-safe, but that does not seem to be required by the specification, either.

dfuch
dfuch approved these changes Dec 6, 2021
Copy link
Member

@dfuch dfuch left a comment

LGTM. It's a bit distateful to change the semantics of createImpl but since the method is now private it does look OK.

@AlanBateman
Copy link
Contributor Author

@AlanBateman AlanBateman commented Dec 6, 2021

To what extent is ServerSocket required to be thread-safe? I don't think it's part of the specification.

A ServerSocket is required by the spec to be asynchronously closable, that is the motivation for the changes here.

java.net.Socket is similar. In that case, an async close is required to wakeup threads that are blocked on the input stream or output stream. There are several issues there, something for another (related) PR.

@openjdk
Copy link

@openjdk openjdk bot commented Dec 6, 2021

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

8278270: ServerSocket is not thread safe

Reviewed-by: dfuchs

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

  • 6994d80: 8278291: compiler/uncommontrap/TraceDeoptimizationNoRealloc.java fails with release VMs after JDK-8154011
  • 286a26c: 8278277: G1: Simplify implementation of G1GCPhaseTimes::record_or_add_time_secs
  • d14f06a: 8278031: MultiThreadedRefCounter should not use relaxed atomic decrement
  • 8d190dd: 8277496: Remove duplication in c1 Block successor lists
  • 194cdf4: 8277864: Compilation error thrown while doing a boxing conversion on selector expression
  • f39fe5b: 8154011: Make TraceDeoptimization a diagnostic flag
  • f180a45: 8278016: Add compiler tests to tier{2,3}
  • 104aa1f: 8268575: Annotations not visible on model elements before they are generated
  • 839b606: 8278143: Remove unused "argc" from ConstantPool::copy_bootstrap_arguments_at_impl

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 label Dec 6, 2021
@AlanBateman
Copy link
Contributor Author

@AlanBateman AlanBateman commented Dec 7, 2021

/integrate

@openjdk
Copy link

@openjdk openjdk bot commented Dec 7, 2021

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

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot closed this Dec 7, 2021
@openjdk openjdk bot added integrated and removed ready rfr labels Dec 7, 2021
@openjdk
Copy link

@openjdk openjdk bot commented Dec 7, 2021

@AlanBateman Pushed as commit 24877ac.

💡 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 net
4 participants