RFR: 8278326: Socket close is not thread safe and other cleanup [v3]
Daniel Fuchs
dfuchs at openjdk.org
Wed Jan 11 14:22:15 UTC 2023
On Wed, 11 Jan 2023 09:57:58 GMT, Alan Bateman <alanb at openjdk.org> wrote:
>> 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.
>
> Alan Bateman has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains six additional commits since the last revision:
>
> - Fix comment
> - Merge
> - Review comments
> - Merge
> - Fix typos
> - Initial commit
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.
-------------
PR: https://git.openjdk.org/jdk/pull/11863
More information about the net-dev
mailing list