How to Fix java.net.SocketException: Broken pipe in Java - Cause and Solution

Hello guys, If you have worked in a client-server Java application then you may be familiar with the  "java.net.SocketException: Broken pipe" error, It is one of the many socket-related errors you will see on the server-side application log.  This error comes intermittently when many clients are connecting to a server and many clients closing the connection before a response is fully transferred. Some of the common causes of java.net.SocketException: Broken pipe exception includes
  • The user closed the browser before the page loaded.
  • Their internet connection failed during loading.
  • They went to another page before the page loaded.
  • The browser timed the connection out before the page loaded (would have to be a large page).

Btw, this is my third article in the series on how to deal with Socket related exceptions in Java. If you haven't read the other articles of this series, here are them

And, is how the error looks like when it comes into your application log file:
java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)

This error says that the connection was closed when the server was writing a response, possibly due to timeout.  Btw, I expect that you are familiar with errors and exceptions in Java and how to handle them. If you are a complete beginner, I would suggest you join a comprehensive Java course to first understand basics like how to handle errors and exceptions.

If you need a recommendation then I suggest you check out The Complete Java Programming MasterClass course by Tim Buchalaka on Udemy. It's not only the most comprehensive course but also the most up-to-date and covers news features from recent Java releases. It's also very affordable and you can buy in just $10 on many Udemy flash sales which happen every now and then.




What to do when "java.net.SocketException: Broken pipe" error comes?

This is an important question, what are you really going to do when you see this error in your log file? Are you going to ignore it, or are you going to find out the root cause, will you inform developers? I'll give you some idea and context to deal with java.net.SocketException: Broken pipe error in a server-side Java application. 

In general, for a Server application, it's important to handle this kind of error so that server is not affected by the client.  As a support person, most of the time, you can ignore this error because the client is disconnected before the response is fully sent.

The warning log basically means that the connection to the client browser is aborted before the response is fully transferred. It is a harmless warning as it can be due to transient network problems or the browser aborts/refreshes the page before it loaded.

These broken pipe exceptions happen when the client (browser) has closed the connection, but the server (your tag) continues to try to write to the stream.  This usually happens when someone clicks Back, Stop, etc. in the browser and it disconnects from the server before the request is finished. Sometimes, it can happen because, for example, the Content-Length header is incorrect (and the browser takes its value as true).

Btw, If you want to learn more about Socket Programming and Network Programming in Java the I also suggest you check the Java Network Programming - TCP/IP Socket Programming course on Udemy.

Dealing with java.net.SocketException: Broken pipe in Java


Usually, this is a non-event, and nothing to worry about. But if you are seeing them in your dev environment when you know you have not interrupted your browser, you might dig a bit more to find out why.

The first is that many databases have a timeout for inactive connections ( the MySQL default is 8 hours, but on some hosts, this may be configured in seconds!). After some time, the database kills this idle connection in an attempt to save resources, as it concludes that someone simply forgot it open.

When, on the client-side, your pool decides to use it, the socket quickly realizes that the connection was closed on the other side, it was broken (hence the name broken pipe ). Very common when starting to use a pool!

The second reason is that you may be handling your transactions without due care: forgetting to commit or rollback in some cases. The database can then kill this connection after some transaction timeout, but your pool does not know it, and when you use that connection, it's broken!

This is a very low-level issue, so if your application is using socket indirectly like via JDBC for opening database connection, or by using Hibernate which opens and closes the database connection, or by using a connection pool library like C3PO which does that.


Other Java troubleshooting articles you may like:

  • java.sql.SQLException: No suitable driver found for 'jdbc:mysql://localhost:3306/mysql [Solution]
  • How to solve java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error? (hint)
  • java.sql.SQLServerException: The index 58 is out of range - JDBC (solution)
  • How to fix Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger (solution)
  • How to fix 'javac' is not recognized as an internal or external command (solution)
  • java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory error (solution)
  • Common reasons of java.lang.ArrayIndexOutOfBoundsException in Java? (solution)
  • java.lang.ClassNotFoundException : 
  • How to avoid ConcurrentModificationException in Java? (tutorial)
  • org.Springframework.Web.Context.ContextLoaderListener (solution)
  • How to connect to MySQL database in Java? (tutorial)
  • Fixing java.lang.UnsupportedClassVersionError Unsupported major.minor version 50.0 (solution)
  • How to solve "could not create the Java virtual machine" error in Java? (solution)
  • How to solve java.lang.OutOfMemoryError: Java Heap Space in Eclipse, Tomcat? (solution)
  • How to fix "Error: Could not find or load main class" in Eclipse? (guide)
  • How to solve java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver? (solution)
  • 10 common reasons for java.lang.NumberFormatException in Java? (tutorial)
  • How to fix the "illegal start of expression" compile-time error in Java? (tutorial)
  • Cause and solution of "class, interface, or enum expected" compiler error in Java? (fix)
  • How to solve "variable might not have initialized" compile-time error in Java? (answer)

Thanks for reading this article, if you like this article then please share it with your friends and colleagues too. If you have any doubt or questions then please drop a comment.

P. S. -  It's worth noting that both Socket and Network Programming is advanced skills and worth learning. If you want to learn more about Socket Programming in Java, I suggest you check the Java: Socket Programming Simplified, a free course on Udemy.


6 comments:

  1. I didnt find any solution in the article, can you please state what should be the resolution? is there any property i should add in application , I am closing the connections as soon as the query executed and provided results . Please suggest

    ReplyDelete
    Replies
    1. Hello Anonymous, can you tell more about what error are you getting? may be just post the stack trace? also a bit more about your application? Do you have any Timeout, how many clients are connecting at a time? As most of the Socket errors are due to network, timeout, and client side errors so having a context around is important. From you message, you are doing find if you are closing the connections as soon as your request is completed but just make sure that you receive the response before you close the connection

      Delete
  2. I am getting below error when i try to connect server using ssl scoket.
    in this case what it expect and which side (client/server)

    Caused by: javax.net.ssl.SSLHandshakeException: Empty server certificate chain
    at sun.security.ssl.Alert.createSSLException(Alert.java:131)
    at sun.security.ssl.Alert.createSSLException(Alert.java:117)
    at sun.security.ssl.TransportContext.fatal(TransportContext.java:364)
    at sun.security.ssl.TransportContext.fatal(TransportContext.java:320)
    at sun.security.ssl.TransportContext.fatal(TransportContext.java:311)
    at sun.security.ssl.CertificateMessage$T12CertificateConsumer.onCertificate(CertificateMessage.java:388)
    at sun.security.ssl.CertificateMessage$T12CertificateConsumer.consume(CertificateMessage.java:373)
    at sun.security.ssl.SSLHandshake.consume(SSLHandshake.java:376)
    at sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:479)
    at sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:457)
    at sun.security.ssl.TransportContext.dispatch(TransportContext.java:200)
    at sun.security.ssl.SSLTransport.decode(SSLTransport.java:155)
    at sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1320)
    at sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1233)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:417)
    at sun.security.ssl.SSLSocketImpl.ensureNegotiated(SSLSocketImpl.java:837)
    at sun.security.ssl.SSLSocketImpl.access$200(SSLSocketImpl.java:76)
    at sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:923)
    at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2837)
    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2853)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3330)
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:939)
    at java.io.ObjectInputStream.(ObjectInputStream.java:401)

    ReplyDelete
  3. I am getting below error when i try to connect server using SSL Scoket. in below what it expect? i mean which side client/server

    Caused by: javax.net.ssl.SSLHandshakeException: Empty server certificate chain
    at sun.security.ssl.Alert.createSSLException(Alert.java:131)
    at sun.security.ssl.Alert.createSSLException(Alert.java:117)
    at sun.security.ssl.TransportContext.fatal(TransportContext.java:364)
    at sun.security.ssl.TransportContext.fatal(TransportContext.java:320)
    at sun.security.ssl.TransportContext.fatal(TransportContext.java:311)
    at sun.security.ssl.CertificateMessage$T12CertificateConsumer.onCertificate(CertificateMessage.java:388)
    at sun.security.ssl.CertificateMessage$T12CertificateConsumer.consume(CertificateMessage.java:373)
    at sun.security.ssl.SSLHandshake.consume(SSLHandshake.java:376)
    at sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:479)
    at sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:457)
    at sun.security.ssl.TransportContext.dispatch(TransportContext.java:200)
    at sun.security.ssl.SSLTransport.decode(SSLTransport.java:155)
    at sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1320)
    at sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1233)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:417)
    at sun.security.ssl.SSLSocketImpl.ensureNegotiated(SSLSocketImpl.java:837)
    at sun.security.ssl.SSLSocketImpl.access$200(SSLSocketImpl.java:76)
    at sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:923)
    at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2837)
    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2853)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3330)
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:939)
    at java.io.ObjectInputStream.(ObjectInputStream.java:401)

    ReplyDelete
    Replies
    1. Please check if your trust store has server certificate or not.

      Delete
    2. You can also enable SSL/TLS debugging on the Java client side by adding the system property -Djavax.net.debug=all to get more detailed information about the SSL handshake process. It could also be possible that server is not sending the certificate chain

      Delete

Feel free to comment, ask questions if you have any doubt.