Dealing with 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 error you will see on 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 of 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 connection was closed when the server was writing a response, possibly due to timeout.  Btw, I expect that you are familiar with error and exception 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 error rand 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 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 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.

Further Reading
The Complete Java MasterClass 
Java Network Programming - TCP/IP Socket Programming
Java Network Programming, (4th Addition) by Harold, Elliotte Rusty
TCP/IP and Networking Fundamentals for IT Pros


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 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.


No comments:

Post a Comment