javax.net.ssl.SSLException: Connection reset on few request out of the total

20.3k Views Asked by At

We have a service that is sending POST request once every second to one of our mircoservices. The sent request is to the same URL each time it is sent.

The code class TrackTask that sends request is using org.apache.http.impl.client.CloseableHttpClient and creating a POST request using org.apache.http.client.methods.HttpPost.

Out of 3600 request we send in one hour we get SSLException on few requests but it is very random. Can be on 2 requests out of 3600 and can be 8.

The exception we got:

javax.net.ssl.SSLException: Connection reset
        at sun.security.ssl.Alert.createSSLException(Alert.java:127) ~[na:1.8.0_312]
        at sun.security.ssl.TransportContext.fatal(TransportContext.java:324) ~[na:1.8.0_312]
        at sun.security.ssl.TransportContext.fatal(TransportContext.java:267) ~[na:1.8.0_312]
        at sun.security.ssl.TransportContext.fatal(TransportContext.java:262) ~[na:1.8.0_312]
        at sun.security.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1563) ~[na:1.8.0_312]
        at sun.security.ssl.SSLSocketImpl.access$400(SSLSocketImpl.java:73) ~[na:1.8.0_312]
        at sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:973) ~[na:1.8.0_312]
        at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:137) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:153) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:280) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:138) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:163) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:157) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:272) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at com.cooladata.da.TrackTask.sendTrack(TrackTask.java:62) [da-qa-1.0-SNAPSHOT.jar:na]
        at com.cooladata.da.TrackTask.startListening(TrackTask.java:108) [da-qa-1.0-SNAPSHOT.jar:na]
        at com.cooladata.da.TrackTask.run(TrackTask.java:120) [da-qa-1.0-SNAPSHOT.jar:na]
        at java.lang.Thread.run(Thread.java:748) [na:1.8.0_312]
        Suppressed: java.net.SocketException: Broken pipe (Write failed)
                at java.net.SocketOutputStream.socketWrite0(Native Method) ~[na:1.8.0_312]
                at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111) ~[na:1.8.0_312]
                at java.net.SocketOutputStream.write(SocketOutputStream.java:155) ~[na:1.8.0_312]
                at sun.security.ssl.SSLSocketOutputRecord.encodeAlert(SSLSocketOutputRecord.java:81) ~[na:1.8.0_312]
                at sun.security.ssl.TransportContext.fatal(TransportContext.java:355) ~[na:1.8.0_312]
                ... 26 common frames omitted
Caused by: java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:210) ~[na:1.8.0_312]
        at java.net.SocketInputStream.read(SocketInputStream.java:141) ~[na:1.8.0_312]
        at sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:464) ~[na:1.8.0_312]
        at sun.security.ssl.SSLSocketInputRecord.bytesInCompletePacket(SSLSocketInputRecord.java:68) ~[na:1.8.0_312]
        at sun.security.ssl.SSLSocketImpl.readApplicationRecord(SSLSocketImpl.java:1341) ~[na:1.8.0_312]
        at sun.security.ssl.SSLSocketImpl.access$300(SSLSocketImpl.java:73) ~[na:1.8.0_312]
        at sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:957) ~[na:1.8.0_312]
        ... 21 common frames omitted

We use to have none or almost no such exceptions at all.

Can anyone help us understand what is the reason for such random exception and how we can resolve it ?

2

There are 2 best solutions below

0
Ran Koren On

Fixed thanks to the post of Spring Boot random "SSLException: Connection reset" in Kubernetes with JDK11

Once I configured the httpclient to NO REUSE connection it worked and we dont get anymore SSLExceptions:

httpClient = HttpClients.custom().setDefaultRequestConfig(config)
        .setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE)
        .setConnectionManager(poolingConnManager)
        .build();
0
DrAchernar On

I saw this error while using getInputStream() method of java.net.URLConnection. If your url has space, java could throw this error