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


The following commit(s) were added to refs/heads/main by this push:
     new 40f1644185 Fix handling of spurious wake-ups while waiting for network 
read/write
40f1644185 is described below

commit 40f16441851660deb8d1a4fb8023045b34a7e09b
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Sat Aug 30 16:55:23 2025 +0100

    Fix handling of spurious wake-ups while waiting for network read/write
---
 java/org/apache/tomcat/util/net/SocketWrapperBase.java | 8 +++++++-
 res/spotbugs/filter-false-positives.xml                | 6 ------
 webapps/docs/changelog.xml                             | 4 ++++
 3 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/java/org/apache/tomcat/util/net/SocketWrapperBase.java 
b/java/org/apache/tomcat/util/net/SocketWrapperBase.java
index 1457791521..214d984356 100644
--- a/java/org/apache/tomcat/util/net/SocketWrapperBase.java
+++ b/java/org/apache/tomcat/util/net/SocketWrapperBase.java
@@ -1359,7 +1359,13 @@ public abstract class SocketWrapperBase<E> {
             synchronized (state) {
                 if (state.state == CompletionState.PENDING) {
                     try {
-                        state.wait(unit.toMillis(timeout));
+                        long timeoutExpiry = System.nanoTime() + 
unit.toNanos(timeout);
+                        long timeoutMillis = unit.toMillis(timeout);
+                         // Spurious wake-ups are possible. Keep waiting until 
state changes or timeout expires.
+                        while (state.state == CompletionState.PENDING && 
timeoutMillis > 0) {
+                            state.wait(unit.toMillis(timeout));
+                            timeoutMillis = (timeoutExpiry - 
System.nanoTime()) / 1_000_000;
+                        }
                         if (state.state == CompletionState.PENDING) {
                             if (handler != null && 
state.callHandler.compareAndSet(true, false)) {
                                 handler.failed(new 
SocketTimeoutException(getTimeoutMsg(read)), attachment);
diff --git a/res/spotbugs/filter-false-positives.xml 
b/res/spotbugs/filter-false-positives.xml
index f252ba4255..000827a625 100644
--- a/res/spotbugs/filter-false-positives.xml
+++ b/res/spotbugs/filter-false-positives.xml
@@ -1663,12 +1663,6 @@
     <Bug code="SF" />
   </Match>
   <Match>
-  <!-- Single condition so fine -->
-    <Class name="org.apache.tomcat.util.net.SocketWrapperBase" />
-    <Method name="vectoredOperation"/>
-    <Bug pattern="WA_NOT_IN_LOOP" />
-  </Match>
-  <Match>
   <!-- Single condition so notify is fine -->
     <Class 
name="org.apache.tomcat.util.net.SocketWrapperBase$VectoredIOCompletionHandler" 
/>
     <Or>
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 29479a81e8..11c6cd159b 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -220,6 +220,10 @@
         Add new <code>ML-DSA</code> key algorithm to <code>PEMFile</code>
         and improve reporting when reading a key fails. (remm)
       </fix>
+      <fix>
+        Fix possible early timeouts for network operations caused by a spurious
+        wake-up of a waiting thread. Found by Coverity Scan. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Jasper">


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to