Joseph Walton avatar Joseph Walton committed 8132068

Allow for spurious wakeups when LDAP connections have a timeout.

Comments (0)

Files changed (1)

src/share/classes/com/sun/jndi/ldap/Connection.java

     BerDecoder readReply(LdapRequest ldr)
             throws IOException, NamingException {
         BerDecoder rber;
-        boolean waited = false;
 
-        while (((rber = ldr.getReplyBer()) == null) && !waited) {
+        long now = System.currentTimeMillis();
+
+        // Ensure we wait at least readTimeout millis even
+        //  with spurious wakeups
+        final long until;
+        if (readTimeout > 0) {
+            until = now + readTimeout;
+        } else {
+            until = Long.MAX_VALUE;
+        }
+
+        while (((rber = ldr.getReplyBer()) == null) && (now < until)) {
             try {
                 // If socket closed, don't even try
                 synchronized (this) {
                     // check if condition has changed since our last check
                     rber = ldr.getReplyBer();
                     if (rber == null) {
-                        if (readTimeout > 0) {  // Socket read timeout is specified
+                        long remaining = until - now;
 
-                            // will be woken up before readTimeout only if reply is
-                            // available
-                            ldr.wait(readTimeout);
-                            waited = true;
-                        } else {
-                            ldr.wait(15 * 1000); // 15 second timeout
-                        }
+                        // Retain behaviour of only waiting fifteen seconds without a timeout
+                        ldr.wait(Math.min(remaining, 15 * 1000));
                     } else {
                         break;
                     }
                 throw new InterruptedNamingException(
                     "Interrupted during LDAP operation");
             }
+
+            now = System.currentTimeMillis();
         }
 
-        if ((rber == null) && waited) {
+        if ((rber == null) && now >= until) {
             removeRequest(ldr);
             throw new NamingException("LDAP response read timed out, timeout used:"
                             + readTimeout + "ms." );
-
         }
         return rber;
     }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.