RFR: 8278326: Socket close is not thread safe and other cleanup [v2]
Alan Bateman
alanb at openjdk.org
Tue Jan 10 20:03:53 UTC 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.
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 four additional commits since the last revision:
- Review comments
- Merge
- Fix typos
- Initial commit
-------------
Changes:
- all: https://git.openjdk.org/jdk/pull/11863/files
- new: https://git.openjdk.org/jdk/pull/11863/files/3d62c906..6184f476
Webrevs:
- full: https://webrevs.openjdk.org/?repo=jdk&pr=11863&range=01
- incr: https://webrevs.openjdk.org/?repo=jdk&pr=11863&range=00-01
Stats: 4754 lines in 215 files changed: 2621 ins; 1179 del; 954 mod
Patch: https://git.openjdk.org/jdk/pull/11863.diff
Fetch: git fetch https://git.openjdk.org/jdk pull/11863/head:pull/11863
PR: https://git.openjdk.org/jdk/pull/11863
More information about the net-dev
mailing list