HTTPASYNC-86: fixed a race condition upon connection lease from the connection pool: in very rare  circumstances the main execution thread can get jammed for so long that the I/O dispatch thread succeeds in completing the request and releasing the connection while the main still is performing connection validation

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpasyncclient/branches/4.0.x@1658693 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt
index 6be5c39..3ab75df 100644
--- a/RELEASE_NOTES.txt
+++ b/RELEASE_NOTES.txt
@@ -1,6 +1,17 @@
 Changes since 4.0.2
 -------------------
 
+* [HTTPASYNC-86]: fixed a race condition upon connection lease from the connection pool: 
+  in very rare  circumstances the main execution thread can get jammed for so long 
+  that the I/O dispatch thread succeeds in completing the request and releasing the connection 
+  while the main still is performing connection validation.
+  Contributed by Oleg Kalnichevski <olegk at apache.org>
+
+
+
+Release 4.0.2
+-------------------
+
 HttpAsyncClient 4.0.2 (GA) is a bug fix release that addresses several issues reported since 
 release 4.0.1. This release also upgrades HttpClient dependency to the latest stable version. 
 
diff --git a/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultClientExchangeHandlerImpl.java b/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultClientExchangeHandlerImpl.java
index ee325b5..00f0506 100644
--- a/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultClientExchangeHandlerImpl.java
+++ b/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultClientExchangeHandlerImpl.java
@@ -304,7 +304,7 @@
 
             managedConn.getContext().setAttribute(HttpAsyncRequestExecutor.HTTP_HANDLER, this);
             managedConn.requestOutput();
-            if (!managedConn.isOpen()) {
+            if (managedConn.isStale()) {
                 failed(new ConnectionClosedException("Connection closed"));
             }
         } catch (final RuntimeException runex) {
diff --git a/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/CPoolProxy.java b/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/CPoolProxy.java
index 5908b8b..fa097e7 100644
--- a/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/CPoolProxy.java
+++ b/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/CPoolProxy.java
@@ -152,9 +152,9 @@
     public boolean isStale() {
         final NHttpClientConnection conn = getConnection();
         if (conn != null) {
-            return conn.isStale() || !conn.isOpen();
+            return !conn.isOpen();
         } else {
-            return true;
+            return false;
         }
     }