RFR: 8343791: Socket.connect API should document whether the socket will be closed when hostname resolution fails or another error occurs [v16]

Volkan Yazıcı duke at openjdk.org
Tue Nov 26 10:51:39 UTC 2024


On Tue, 26 Nov 2024 10:33:21 GMT, Daniel Fuchs <dfuchs at openjdk.org> wrote:

>> test/jdk/java/net/Socket/CloseOnFailureTest.java line 345:
>> 
>>> 343:             SocketAddress serverSocketAddress = serverSocket().getLocalSocketAddress();
>>> 344:             try (Socket socket = createConnectedSocket(serverSocketAddress)) {
>>> 345:                 assertReconnectFailure(() -> socket.connect(REFUSING_SOCKET_ADDRESS));
>> 
>> I needed to delegate the assertion to the implementation because when I call `connect()` on a `Socket` created using `SocketChannel.open(address).socket()` and `new Socket(address, port)`, the former throws an `AlreadyConnectedException`, whereas the latter throws a `SocketException`. Is this discrepancy expected?
>
> There are some subtle behaviour discrepancies between plain Socket (created with new Socket()) and the SocketAdaptor (created from SocketChannel.socket()). SocketChannel::connect will throw AlreadyConnectedException, and so that's what the socket adaptor also does. Ideally we'd want to reduce the distance between plain Socket and the Socket adaptor - but changing behaviours in these APIs is always tricky and a bit risky (and can require a lot of efforts). FWIW - SocketChannel does the right thing - but we're stuck with SocketException in the Socket API due to compatibility reason.

Got it. Thanks for the explanation.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/22160#discussion_r1858251746


More information about the net-dev mailing list