Placing timeout for SSLSocket handshake - java

Recently, I am having trouble with our SOAP calls which uses apache axis 1.4. I have set the timeout as 30s, however it sometimes reaches around ~1000s before throwing an exception. After exploring internal implementation of axis, I found out that it is due to the SSLSocket handshake.
See sample error below.
java.net.SocketException: Connection timed out (Read failed)
at java.net.SocketInputStream.$$YJP$$socketRead0(Native Method)
at java.net.SocketInputStream.socketRead0(SocketInputStream.java)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)
at org.apache.axis.components.net.JSSESocketFactory.create(JSSESocketFactory.java:186)
Is it possible to put a timeout also on the handshake? Or is there another way to forcibly cut the connection.
Also, see the handshake part on the code
sslSocket = sslFactory.createSocket(tunnel, host, port, true);
if (log.isDebugEnabled()) {
log.debug(Messages.getMessage("setupTunnel00",
tcp.getProxyHost(),
"" + tunnelPort));
}
}
((SSLSocket) sslSocket).startHandshake();

When you create the tunnel, call:
tunnel.connect(new InetSocketAddress(host, port), 30000);

Related

java.net.ConnectException: Connection refused timeout

I saw a lot of "java.net.ConnectException: Connection refused" questions but none referring to timeout of this error. My problem is I have to connect to a server that, in some cases, is blocked (connected by another software to the same port). So, I'm doing a loop with some max retries to try to connect:
My current code (of course, is depending on a lot of configurations for my software, but is working fine):
public TCPConnector(TCPDefinition tcpDefinition) throws IAException {
ivTcpDefinition = tcpDefinition;
// Initialize the socket
boolean retry = false;
int counter = 1;
do {
try {
ivSocket = new Socket();
ivSocket.connect(new InetSocketAddress(tcpDefinition.getHostname(), tcpDefinition.getPort()), tcpDefinition.getConnectTimeOut());
ivSocket.setSoTimeout(tcpDefinition.getAckTimeOut());
retry = false;
}
catch (UnknownHostException uhe) {
throw new IAException(null, new StringBuffer("Can't find host: ").append(tcpDefinition.getHostname()).toString(), uhe);
}
catch (SocketException see) {
StringBuilder sb = new StringBuilder("Connection refused to host ").append(tcpDefinition.getHostname()).
append(" port ").append(tcpDefinition.getPort()).append(". Connection Attempt Nr. ").append(counter);
logger.error(sb.toString(), see);
retry = true;
if (counter++ > tcpDefinition.getConnectRetries())
throw new IAException(null, sb.toString(), see);
else
logger.error("will retry to connect");
}
catch (IOException ioe) {
StringBuilder sb = new StringBuilder("I/O error while connecting to host ").append(tcpDefinition.getHostname()).
append(" port ").append(tcpDefinition.getPort()).append(". Connection Attempt Nr. ").append(counter);
logger.error(sb.toString(), ioe);
retry = true;
if (counter++ > tcpDefinition.getConnectRetries())
throw new IAException(null, sb.toString(), ioe);
else
logger.error("will retry to connect");
}
}
while (retry);
}
Well, the problem is this:
On Windows, every second, the SocketException is thrown, instead the IOException, while I have configured a timeout of 5000 msec to ivSocket.connect
On Linux, this is thrown every millisecond!!
Windows:
2019-12-05 12:40:47,609 ERROR DefaultQuartzScheduler_Worker-1 TCPConnector - Connection refused to host localhost port 13002. Connection Attempt Nr. 1
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
2019-12-05 12:40:48,703 ERROR DefaultQuartzScheduler_Worker-1 TCPConnector - Connection refused to host localhost port 13002. Connection Attempt Nr. 2
java.net.ConnectException: Connection refused: connect
Linux:
2019-12-05 12:45:47,609 ERROR DefaultQuartzScheduler_Worker-1 TCPConnector - Connection refused to host localhost port 13002. Connection Attempt Nr. 1
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
2019-12-05 12:45:47,610 ERROR DefaultQuartzScheduler_Worker-1 TCPConnector - Connection refused to host localhost port 13002. Connection Attempt Nr. 2
java.net.ConnectException: Connection refused: connect
Why the timeout is not executed? Well this is not exactly right. If I configure a timeout less than 1 second on Windows, then the timeout is executed. 500 msec:
2019-12-05 11:47:07,375 ERROR DefaultQuartzScheduler_Worker-1 TCPConnector - I/O error while connecting to host localhost port 13002. Connection Attempt Nr. 1
java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
2019-12-05 11:47:07,875 ERROR DefaultQuartzScheduler_Worker-1 TCPConnector - I/O error while connecting to host localhost port 13002. Connection Attempt Nr. 2
java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
It is possible to configure a "connect refuse" timeout?
There is no such thing as a "connection refused timeout".
"Connection refused" happens when the server sees the connection request, but there is no service listening for connections on the IP + port that the request is directed to. The server then "refuses" the connection. This typically happens instantly, so so no timeout is triggered.
"Connection timed out" happens (typically) when something stops the connection request from reaching the server1, 2. So the client-side will wait for the response from the server, and then resend / wait a few times. And eventually the time allotted for establishing a connection will expire ... and the connection times out.
As you can see these are different scenarios. And they are reported back to the Java client-side differently.
So the reason you are not getting timeouts is that the "connection refused" responses are coming back quick enough that your configured timeout is not exceeded.
That might also explain why setting the connect timeout small might have changed the behavior. There may also be issues with the granularity of the timeout that the OS allows Java to set.
To investigate this further, I think we would need a minimal reproducible example. For example, we need to see how you have implemented the code that manages the server-socket and accepts connections on the server side.
1 - The blockage could be on the server's reply packets.
2 - There are various possible causes for this kind of thing. The most likely are a firewall blocking traffic somewhere, a network routing problem, or using a private IP address on the wrong network.

SocketException: Connection reset - Client Jersey 2.0 Java 7

I'm getting similar error to this link1.
When I post(client to server) a small xml via REST, everything works fine. Unfortunately I'm getting an error while I'm posting some bigger xmls, when the connection lasts longer than 10/15 min. (I assume It's some kind of timeout)
I corrected my SSL certs as it was mentioned in the link1 - configureClient() method is the same in my case as the solution from the one in the link1.
I added also System.setProperty("java.net.preferIPv4Stack" , "true"); - sometimes it solves Connection reset
Essential info:
Error: javax.ws.rs.ProcessingException: java.net.SocketException: Connection reset
Method: REST POST
Java version: 7
Engine: Jersey 2.x
Side: Client
System: Windows 7
My client:
System.setProperty("java.net.preferIPv4Stack" , "true");
RestMethodes restMethodes = new RestMethodes();
ClientConfig config = new ClientConfig();
config = config.property(ClientProperties.CONNECT_TIMEOUT, 0);
config = config.property(ClientProperties.READ_TIMEOUT, 0);
config.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
Client client = configureClient(config);
client.register(HttpAuthenticationFeature.basic(USER, PASS));
WebTarget target = client.target(SERVER_URL + "/bundles/assets");
Invocation.Builder responseInvocation = target.request(MediaType.APPLICATION_JSON);
// My exception is thrown there
Response response = responseInvocation.post(Entity.xml(assetsString));
String entity = response.readEntity(String.class);
//entity = jsonPrettyPrinter(entity);
if (entity.isEmpty() || entity.equals("")) {
log.info("[POST ASSETS] Failed : Response is empty");
}
Response.StatusType codeName = response.getStatusInfo();
int code = response.getStatus();
if (code != 200) {
log.error("[POST ASSETS] Failed : HTTP error code : " + response.getStatus() + "\n" + entity);
response.close();
} else {
log.info("[POST ASSETS] RESPONSE CODE ID : " + code + " CODE NAME : " + codeName);
log.info("[POST ASSETS] RESPONSE : " + entity);
response.close();
}
client.close();
My ERROR
Exception in thread "main" javax.ws.rs.ProcessingException: java.net.SocketException: Connection reset
at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:287)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:252)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:684)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:681)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:681)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:437)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:343)
at com.sas.spl.saslineagebridges.test.PostTester.main(PostTester.java:61)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:930)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:704)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:675)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1569)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
at org.glassfish.jersey.client.internal.HttpUrlConnector._apply(HttpUrlConnector.java:399)
at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:285)
... 11 more
EDIT:
I forgot to mention that I managed to post my request via CURL from bash with infinity timeout and keep alive, so It shouldn't be the servers issue. The CURL REST post took 24 min. and my java client throws connection reset after 15 min. In my opinion It might be my fault.
You are facing a timeout issue, although your client seems well configured. This timeout can be caused by any firewall, proxy, load-balancer, or even the server itself if it is running a web server like Apache in front of the application server. Please check what is in-between your client and the application server, and set timeouts accordingly everywhere.
It doesn't mean that you can't do nothing on the client itself, but it's much more difficult to solve there.
On Windows you will need to enable the TCP keep-alives, first.
After, according to the Client Transport you are using, we will try to add TCP keep-alives to the sockets built by the underlying factory, something similar that we know possible on Axis clients. This solution is more time-consuming for you.

SocketException connection reset exception

I am trying to do a get request on an URL. When I hit the URL in a web browser it normally takes around 2 mins to load.
When I am trying to call it from Java - I get a SocketException connection reset error.
Error: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:940)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:735)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:678)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:706)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1569)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
I am trying to do a getResponseCode, I am getting the same error when I am trying to read from the stream as well.
How do I resolve this?
I tried setting the connection timeout and read timeout to 5 mins. None of them worked.
Edit: [Adding code] The following is the code, I am getting the same error while doing a getResponseCode and reading the stream using getInputStream.
HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(TIMEOUT); //5 mins
connection.setReadTimeout(TIMEOUT); //5 mins
connection.getResponseCode();
IOUtils.toString(connection.getInputStream(), StandardCharsets.UTF_8);
I don't think this is a server issue because I am able to do a get with other params which would usually load in 15-30 secs in the browser. Only this particular request is failing. (this request takes 2 mins to load in the browser)

Threads hang using java.net.HttpURLConnection when it's a self https connection

I'm currently writing an API which requires to make an HTTPS connection from server A to itself. Using Jmeter to test, I find that threads are easily hang with even 10 threads starting simultaneously. But if the connection is made to a different server, the problem doesn't exist. I used just a few lines of code to test it. It presents the same problem.
try {
URL url = new URL("some URL that connects to the server itself");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(20000);
con.setReadTimeout(30000);
con.setRequestMethod("GET");
con.setDoInput(true);
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = reader.readLine();
int count = 0;
while (line != null) {
count += line.length();
line = reader.readLine();
}
//con.disconnect();
}catch(Exception e){
e.printStackTrace();
}
Among 10 threads, only 2 of them return with the results as expected, all the other seems to be timed out with stack trace
[err] java.net.SocketTimeoutException: Read timed out
[err] at java.net.SocketInputStream.socketRead0(Native Method)
[err] at java.net.SocketInputStream.read(SocketInputStream.java:150)
[err] at java.net.SocketInputStream.read(SocketInputStream.java:121)
[err] at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
[err] at sun.security.ssl.InputRecord.read(InputRecord.java:503)
[err] at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:954)
[err] at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1343)
[err] at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1371)
[err] at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355)
[err] at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
[err] at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
[err] at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1511)
[err] at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
[err] at com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl.getInputStream(HttpsURLConnectionOldImpl.java:224)
I enable the debug option with -Djavax.net.debug=all, here are some traces show on the console
Default Executor-thread-15, called closeInbound()
Default Executor-thread-15, fatal error: 80: Inbound closed before receiving peer's close_notify: possible truncation attack?
javax.net.ssl.SSLException: Inbound closed before receiving peer's close_notify: possible truncation attack?
%% Invalidated: [Session-19, TLS_RSA_WITH_AES_128_CBC_SHA256]
Default Executor-thread-15
, SEND TLSv1.2 ALERT:
fatal,
description = internal_error
Default Executor-thread-15, Exception sending alert: java.io.IOException: writer side was already closed.
My question is why does it has problems when making a connection to the server itself? Why do only two connections make it back everytime? How should I solve it?
I tried to set http.maxConnections to system property using
System.setProperty("http.maxConnections", String.valueOf(300)); but it didn't help.

jedis connection exception: socket write error

Using jedis client to save data to redis localhost.
Once I connect to redisHost using jedis, I am not disconnecting as long as I get messages Is that an issue? Tried to change the timeout from default 2000 to 6000 but still the same error!
connecting and sending data:
if (jedis == null)
jedis = new Jedis(redisHost);
if (!jedis.isConnected())
jedis.connect();
if (jedis.isConnected())
jedis.zadd("someKey", doubleTime, "someValue");
following is the stack trace of the error receiving:
redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Software caused connection abort: socket write error
at redis.clients.jedis.Protocol.sendCommand(Protocol.java:92)
at redis.clients.jedis.Protocol.sendCommand(Protocol.java:72)
at redis.clients.jedis.Connection.sendCommand(Connection.java:80)
at redis.clients.jedis.BinaryClient.zadd(BinaryClient.java:387)
at redis.clients.jedis.Client.zadd(Client.java:327)
at redis.clients.jedis.Jedis.zadd(Jedis.java:1468)
at com.comcast.xre.WebSocketServer.saveLogToDB(WebSocketServer.java:105)
at com.comcast.xre.WebSocketServer.access$000(WebSocketServer.java:27)
at com.comcast.xre.WebSocketServer$1$1.onFullTextMessage(WebSocketServer.java:69)
at io.undertow.websockets.core.AbstractReceiveListener$2.complete(AbstractReceiveListener.java:138)
at io.undertow.websockets.core.AbstractReceiveListener$2.complete(AbstractReceiveListener.java:134)
at io.undertow.websockets.core.BufferedTextMessage.read(BufferedTextMessage.java:87)
at io.undertow.websockets.core.AbstractReceiveListener.readBufferedText(AbstractReceiveListener.java:134)
at io.undertow.websockets.core.AbstractReceiveListener.bufferFullMessage(AbstractReceiveListener.java:72)
at io.undertow.websockets.core.AbstractReceiveListener.onText(AbstractReceiveListener.java:52)
at io.undertow.websockets.core.AbstractReceiveListener.handleEvent(AbstractReceiveListener.java:26)
at io.undertow.websockets.core.AbstractReceiveListener.handleEvent(AbstractReceiveListener.java:15)
at org.xnio.ChannelListeners.invokeChannelListener(ChannelListeners.java:92)
at io.undertow.server.protocol.framed.AbstractFramedChannel$FrameReadListener.handleEvent(AbstractFramedChannel.java:632)
at io.undertow.server.protocol.framed.AbstractFramedChannel$FrameReadListener.handleEvent(AbstractFramedChannel.java:618)
at org.xnio.ChannelListeners.invokeChannelListener(ChannelListeners.java:92)
at org.xnio.conduits.ReadReadyHandler$ChannelListenerHandler.readReady(ReadReadyHandler.java:66)
at org.xnio.nio.NioSocketConduit.handleReady(NioSocketConduit.java:87)
at org.xnio.nio.WorkerThread.run(WorkerThread.java:531)
Caused by: java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)
at java.net.SocketOutputStream.write(SocketOutputStream.java:159)
at redis.clients.util.RedisOutputStream.flushBuffer(RedisOutputStream.java:31)
at redis.clients.util.RedisOutputStream.write(RedisOutputStream.java:38)
at redis.clients.jedis.Protocol.sendCommand(Protocol.java:78)
... 23 more
Issue was with Jedis connection. It's connected but after few messages, connection is broken.
After each save to DB, I am disconnecting and removing the jedis client completely and creating a new client and connection. Looks awkward but it solves the issue.

Categories

Resources