java.net.SocketException: Broken pipe

The error

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)

The above error comes up from a servlet, or a controller when you try to write any thing to response. In most cases, this happens when writing a file to response output stream

Reason

The reason of the above exception is

  1. Other end has already closed the connection
  2. Client browser is exited, or tab closed, or page reloaded.
  3. Api caller has closed the connection before finishing reading the response

Solution

There’s no solution to fix this error. As the peer has already closed the connection, the max you can do is, catch the exception and check the message for “Broken Pipe” and if it is broken pipe, do not try to write any further response or bytes to response output stream

Leave a comment