Created attachment 36892 [details] patch for NioEndpoint to use pool writes when blocking & non-blocking Product: Tomcat 9.0.20+ Component: tomcat/java/org/apache/tomcat/util/net/NioEndpoint.java Platform: macOS Mojave 10.14.6 Java version: 1.8.0_202 (Oracle Corporation) Using the NIO Connector with SSL, I am seeing issues with the chunked responses. It's been sporadic, but repeatable with my application within 20 attempts (though usually just the first after a fresh start is enough). Tried on 8.5.40 and 8.5.47, and did not experience the issue. It shows up in Chrome as: > net::ERR_INCOMPLETE_CHUNKED_ENCODING Going back to older Tomcat versions, we were able to narrow it down to 9.0.20 being the first version with the issue, specifically this commit: https://github.com/apache/tomcat/commit/4377d2db40 With the move to a straight write during non-blocking writes, the case for a non-blocking write with a shared selector is different. By reverting back to a pool write, I was able to make fix the issue. Attached patch applies to master branch, commit 0872b1a498610b008acbdaa719895ae15370359a.
-1 for the patch, you're doing a blocking write, so it "works". We'll examine the issue eventually, but it most likely works fine as is. If it is non blocking, the "selector" was not doing any magic, it simply does a non blocking write, hence the patch.
The previous code was using a write loop, that's the only difference I can see and you can try it. Such as: --- a/java/org/apache/tomcat/util/net/NioEndpoint.java +++ b/java/org/apache/tomcat/util/net/NioEndpoint.java @@ -1273,9 +1273,13 @@ // registered for write once as both container and user code can trigger // write registration. } else { - if (socket.write(from) == -1) { - throw new EOFException(); - } + int n = 0; + do { + n = socket.write(from); + if (n == -1) { + throw new EOFException(); + } + } while (n > 0 && from.hasRemaining()); } updateLastWrite(); } If you are still experiencing an issue, please provide a test case (there's an extensive non blocking write test in the Tomcat testsuite, which is not failing).
Made that change, and don't have the problem anymore. Would adding that write loop back in be a possibility? Thanks, Justin
Ok, sorry for the trouble, the loop seemed only useful for blocking mode so I didn't add it. The fix will be in Tomcat 9.0.30.
Thanks for the fast turn around!