Servletoutputstream Failed To Flush Java.io.ioexception Broken Pipe
The operating system detects that the receiving end is gone. When the Java process attempts to write to that closed socket, the OS sends a signal (SIGPIPE) or an error code, which the JVM translates into a java.io.IOException: Broken pipe .
You cannot reliably test if the client is still connected without writing data. TCP does not provide a "is peer alive" flag without sending packets. However, you can catch the first write failure and abandon further processing: The operating system detects that the receiving end is gone
If the error occurs due to network device timeouts, you can adjust lower-level parameters: TCP does not provide a "is peer alive"
A: No. The output stream is managed by the container. Closing it manually will break the response. Closing it manually will break the response
public class BrokenPipeFilter implements Filter { public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { try { chain.doFilter(req, res); // If the response is already committed, flushing might still throw if (res instanceof HttpServletResponse) { ((HttpServletResponse) res).getOutputStream().flush(); } } catch (IOException e) { if (e.getMessage() != null && e.getMessage().contains("Broken pipe")) { log.warn("Client disconnected - broken pipe suppressed"); // Do not rethrow. The error is already logged. // Ensure no further handling that expects a complete response. } else { throw e; // rethrow other IOExceptions } } } }