I am using apache commons http client to call url using post method to post the parameters and it is throwing the below error rarely.
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)
at java.io.BufferedOutputStream.write(BufferedOutputStream.java:105)
at java.io.FilterOutputStream.write(FilterOutputStream.java:80)
at org.apache.commons.httpclient.methods.ByteArrayRequestEntity.writeRequest(ByteArrayRequestEntity.java:90)
at org.apache.commons.httpclient.methods.EntityEnclosingMethod.writeRequestBody(EntityEnclosingMethod.java:499)
at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:2114)
at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1096)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:398)
Can someone suggest what is causing this Exception and how to debug it?
In our case we experienced this while performing a load test on our app server. The issue turned out that we need to add additional memory to our JVM because it was running out. This resolved the issue.
Try increasing the memory available to the JVM and or monitor the memory usage when you get those errors.
I have implemented data downloading functionality through FTP server and found the same exception there too while resuming that download. To resolve this exception, you will always have to disconnect from the previous session and create new instance of the Client and new connection with the server. This same approach could be helpful for HTTPClient too.
You should increase "backlog" parameter of your ServerSocket, for example
All the open streams & connections need to be properly closed, so the next time we try to use the urlConnection object, it does not throw an error. As an example, the following code change fixed the error for me.
Before:
After:
The above answers illustrate the reason for this
java.net.SocketException: Broken pipe
: the other end closed the connection. I would like to share experience what happened when I encountered it:Content-Type
header is mistakenly set larger than request body actually is (in fact there was no body at all)Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception java.net.SocketTimeoutException: null
java.net.SocketException: Broken pipe
because client closed it.Sometimes, tomcat does not throw broken pip exception, because timeout exception close the connection, why such a difference is confusing me too.
In my case this happened because the version was not a dynamic variable in pom.xml.
Version in pom.xml was "1.0.SNAPSHOT"
I changed it to
${ver}
The first build passed but the next build is trying to deploy into the same location for which the connection is already closed.
After changing the version it got resolved for me.