I found the strange behavior of the Tomcat 9 servlet: when a client is disconnected before the servlet returns the response, the response is still “returned” without raising any exception.
Here’s the test servlet:
public class TestClientDisconnectServlet extends HttpServlet {
private static final Logger log = LoggerFactory.getLogger(TestClientDisconnectServlet.class);
public TestClientDisconnectServlet() {
}
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
log.info("Start generating the response");
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("Finish generating the response");
try {
resp.setBufferSize(64);
// Here we always get 8192, so when the client is disconnected, the error is not raised...
// Even if I set in server.xml: <Connector ... socket.appWriteBufSize="1" /> it doesn't change anything!
log.info("Buffer size=" + resp.getBufferSize());
resp.setStatus(200);
resp.setContentType("text/html");
// If the line exceeds 8192 bytes, we get the exception
String response = "<html><body>This is a test servlet.</body></html>";
resp.getOutputStream().write(response.getBytes());
resp.getOutputStream().flush();
resp.getOutputStream().close();
log.info("Succesfully delivered");
} catch (Exception e) {
log.info("Failed to deliver the request: {}", e.getMessage());
}
}
}
When a client is disconnected before getting the answer, the expected behavior is to raise an exception and output the message Failed to deliver the request
. However the servlet is successfully executed up to Successfully delivered
.
After some investigation, I found that the issue may be due to the write buffering: when a large (longer than 8192 bytes) request is returned, the servlet behaves as expected (raises an exception).
An obvious solution would be to decrease the write buffer size (as per the documentation, resp.setBufferSize()
should do the trick). However it doesn’t work! When I pass the buffer size less than 8192, it remains equal to 8192.
I tried adding the attribute socket.appWriteBufSize
to the Connector
attribute in server.xml
, as per the Tomcat documentation (I am using org.apache.coyote.http11.Http11NioProtocol
, SSL-enabled). But it still cannot change the buffer size.
What am I doing wrong? How to reduce the write buffer size in HttpServletResponse
, or, more general, how to determine when the client is disconnected during sending the (short) response?