# HG changeset patch # User igerasim # Date 1432221026 -10800 # Thu May 21 18:10:26 2015 +0300 # Node ID b6ab063b0581c449065be3ef6a3692f40fc3f903 # Parent 72fdb709f3563183db0bded7c35c2c571786f134 imported patch 7011441-Workaround-spurious-wakeups-in-com.sun.jndi.ldap.Connection diff --git a/src/java.naming/share/classes/com/sun/jndi/ldap/Connection.java b/src/java.naming/share/classes/com/sun/jndi/ldap/Connection.java --- a/src/java.naming/share/classes/com/sun/jndi/ldap/Connection.java +++ b/src/java.naming/share/classes/com/sun/jndi/ldap/Connection.java @@ -393,9 +393,14 @@ BerDecoder readReply(LdapRequest ldr) throws IOException, NamingException { BerDecoder rber; - boolean waited = false; - while (((rber = ldr.getReplyBer()) == null) && !waited) { + // Track down elapsed time to workaround spurious wakeups + long elapsedMilli = 0; + long elapsedNano = 0; + + while (((rber = ldr.getReplyBer()) == null) && + (readTimeout == 0 || elapsedMilli < readTimeout)) + { try { // If socket closed, don't even try synchronized (this) { @@ -409,11 +414,15 @@ rber = ldr.getReplyBer(); if (rber == null) { if (readTimeout > 0) { // Socket read timeout is specified + long beginNano = System.nanoTime(); - // will be woken up before readTimeout only if reply is + // will be woken up before readTimeout if reply is // available - ldr.wait(readTimeout); - waited = true; + ldr.wait(readTimeout - elapsedMilli); + elapsedNano += (System.nanoTime() - beginNano); + elapsedMilli += elapsedNano / 1000000; + elapsedNano %= 1000000; + } else { // no timeout is set so we wait infinitely until // a response is received @@ -430,7 +439,7 @@ } } - if ((rber == null) && waited) { + if ((rber == null) && (elapsedMilli >= readTimeout)) { abandonRequest(ldr, null); throw new NamingException("LDAP response read timed out, timeout used:" + readTimeout + "ms." );