Skip to content

Commit

Permalink
IGNITE-22605 Add check for SSL errors on TcpDiscovery writing failures (
Browse files Browse the repository at this point in the history
  • Loading branch information
timoninmaxim committed Jul 16, 2024
1 parent fb4e5ba commit ad1bbcf
Showing 1 changed file with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,39 @@ protected Socket openSocket(Socket sock, InetSocketAddress remAddr, IgniteSpiOpe
}
}

/**
* Writing to a socket might fail due to a broken connection. It might happen due to a recipient has closed the connection
* before, on SSL handshake, and doesn't accept new messages. In a such case it's possible to check the original error
* by reading the socket input stream.
*
* @param sock Socket to check.
* @param writeErr Error on writing a message to the socket.
* @return {@code SSLException} in case of SSL error, or {@code null} otherwise.
*/
private @Nullable SSLException checkSslException(Socket sock, Exception writeErr) {
if (!sslEnable)
return null;

SSLException sslEx = X.cause(writeErr, SSLException.class);

if (sslEx != null)
return sslEx;

try {
// Set timeout to 1ms, in this case of closed socket it should return fast.
if (X.hasCause(writeErr, SocketException.class))
readReceipt(sock, 1);
}
catch (SSLException sslErr) {
return sslErr;
}
catch (Exception err) {
// Skip.
}

return null;
}

/**
* Creates socket binding it to a local host address. This operation is not blocking.
*
Expand Down Expand Up @@ -1716,7 +1749,9 @@ protected void writeToSocket(Socket sock, TcpDiscoveryAbstractMessage msg, byte[
out.flush();
}
catch (IOException e) {
err = e;
SSLException sslEx = checkSslException(sock, e);

err = sslEx == null ? e : sslEx;
}
finally {
boolean cancelled = obj.cancel();
Expand Down Expand Up @@ -1824,7 +1859,9 @@ protected void writeToSocket(Socket sock,
U.marshal(marshaller(), msg, out);
}
catch (IgniteCheckedException e) {
err = e;
SSLException sslEx = checkSslException(sock, e);

err = sslEx == null ? e : new IgniteCheckedException(sslEx);
}
finally {
boolean cancelled = obj.cancel();
Expand Down Expand Up @@ -1869,7 +1906,9 @@ protected void writeToSocket(TcpDiscoveryAbstractMessage msg, Socket sock, int r
out.flush();
}
catch (IOException e) {
err = e;
SSLException sslEx = checkSslException(sock, e);

err = sslEx == null ? e : sslEx;
}
finally {
boolean cancelled = obj.cancel();
Expand Down

0 comments on commit ad1bbcf

Please sign in to comment.