From e22748f171ea55aa1c6343e763f797f69e720983 Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Wed, 9 Aug 2017 11:27:28 -0400 Subject: [PATCH] Fixed NPE PoolingAsyncClientConnectionManager PoolingAsyncClientConnectionManager.validateAfterInactivity causes an NPE when PoolEntries are initially created with no Connection. Added logic to avoid checking the connection on every pool checkout similar to the blocking pool. Added an connection.isOpen check to http1.x connections leased from the pool, although I'm unsure if it's at all effective. --- .../impl/nio/PoolingAsyncClientConnectionManager.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/nio/PoolingAsyncClientConnectionManager.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/nio/PoolingAsyncClientConnectionManager.java index e7039ea1b3..25dbae90d2 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/nio/PoolingAsyncClientConnectionManager.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/nio/PoolingAsyncClientConnectionManager.java @@ -164,8 +164,9 @@ void leaseCompleted(final PoolEntry poo @Override public void completed(final PoolEntry poolEntry) { - if (TimeValue.isPositive(validateAfterInactivity)) { - final ManagedAsyncClientConnection connection = poolEntry.getConnection(); + final ManagedAsyncClientConnection connection = poolEntry.getConnection(); + if (TimeValue.isPositive(validateAfterInactivity) && connection != null && + poolEntry.getUpdated() + validateAfterInactivity.toMillis() <= System.currentTimeMillis()) { final ProtocolVersion protocolVersion = connection.getProtocolVersion(); if (HttpVersion.HTTP_2_0.greaterEquals(protocolVersion)) { connection.submitPriorityCommand(new PingCommand(new BasicPingHandler(new Callback() { @@ -183,6 +184,12 @@ public void execute(final Boolean result) { }))); } else { + if (!connection.isOpen()) { + if (log.isDebugEnabled()) { + log.debug("Connection " + ConnPoolSupport.getId(connection) + " is closed"); + } + poolEntry.discardConnection(ShutdownType.IMMEDIATE); + } leaseCompleted(poolEntry); } } else {