This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 8a762896a7f6c8f7870322651e7bf1cb53abb8d8
Author: Mark Thomas <[email protected]>
AuthorDate: Fri Sep 30 18:17:14 2022 +0100

    BZ 66281 - unexpected timeouts when using HTTP/2 and NIO2
    
    Tiemouts may appear as client disconnections
    https://bz.apache.org/bugzilla/show_bug.cgi?id=66281
---
 java/org/apache/coyote/http2/Http2UpgradeHandler.java | 18 ++++++++++++++----
 webapps/docs/changelog.xml                            |  4 ++++
 2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/java/org/apache/coyote/http2/Http2UpgradeHandler.java 
b/java/org/apache/coyote/http2/Http2UpgradeHandler.java
index 4073411e25..e5f8d73a00 100644
--- a/java/org/apache/coyote/http2/Http2UpgradeHandler.java
+++ b/java/org/apache/coyote/http2/Http2UpgradeHandler.java
@@ -267,6 +267,10 @@ class Http2UpgradeHandler extends AbstractStream 
implements InternalHttpUpgradeH
             log.debug(sm.getString("upgradeHandler.prefaceReceived", 
connectionId));
         }
 
+        // Allow streams and connection to determine timeouts
+        socketWrapper.setReadTimeout(-1);
+        socketWrapper.setWriteTimeout(-1);
+
         processConnection(webConnection, stream);
     }
 
@@ -348,10 +352,7 @@ class Http2UpgradeHandler extends AbstractStream 
implements InternalHttpUpgradeH
                     socketWrapper.getLock().unlock();
                 }
                 try {
-                    // There is data to read so use the read timeout while
-                    // reading frames ...
-                    socketWrapper.setReadTimeout(protocol.getReadTimeout());
-                    // ... and disable the connection timeout
+                    // Disable the connection timeout while frames are 
processed
                     setConnectionTimeout(-1);
                     while (true) {
                         try {
@@ -1528,6 +1529,15 @@ class Http2UpgradeHandler extends AbstractStream 
implements InternalHttpUpgradeH
         int thisRead = 0;
 
         while (len > 0) {
+            // Blocking reads use the protocol level read timeout. Non-blocking
+            // reads do not timeout. The intention is that once a frame has
+            // started to be read, the read timeout applies until it is
+            // completely read.
+            if (nextReadBlock) {
+                socketWrapper.setReadTimeout(protocol.getReadTimeout());
+            } else {
+                socketWrapper.setReadTimeout(-1);
+            }
             thisRead = socketWrapper.read(nextReadBlock, data, pos, len);
             if (thisRead == 0) {
                 if (nextReadBlock) {
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index e7b4a6868e..dc92160c4c 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -111,6 +111,10 @@
         <bug>66276</bug>: Fix incorrect class cast when adding
         a descendant of HTTP/2 streams. (lihan)
       </fix>
+      <fix>
+        <bug>66281</bug>: Fix unexpected timeouts that may appear as client
+        disconnections when using HTTP/2 and NIO2. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Jasper">


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to