Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id E8E89200C7F for ; Tue, 9 May 2017 22:01:30 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id E78BF160BCB; Tue, 9 May 2017 20:01:30 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 7159F160BC3 for ; Tue, 9 May 2017 22:01:29 +0200 (CEST) Received: (qmail 77408 invoked by uid 500); 9 May 2017 20:01:28 -0000 Mailing-List: contact commits-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list commits@hc.apache.org Received: (qmail 76836 invoked by uid 99); 9 May 2017 20:01:28 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 09 May 2017 20:01:28 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 01442DFAB4; Tue, 9 May 2017 20:01:27 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: olegk@apache.org To: commits@hc.apache.org Date: Tue, 09 May 2017 20:01:46 -0000 Message-Id: <0f3067e5cf0d491b9bc7ad230aec0d00@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [20/50] httpcomponents-core git commit: HTTPCORE-446: fixed deadlock in AbstractConnPool shutdown code archived-at: Tue, 09 May 2017 20:01:31 -0000 HTTPCORE-446: fixed deadlock in AbstractConnPool shutdown code git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpcore/branches/4.4.x@1783929 13f79535-47bb-0310-9956-ffa450edef68 Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/f7b81af5 Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/f7b81af5 Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/f7b81af5 Branch: refs/heads/4.4.x Commit: f7b81af572dbd129850825ce240d0685f8664d04 Parents: 217e68e Author: Oleg Kalnichevski Authored: Tue Feb 21 20:00:33 2017 +0000 Committer: Oleg Kalnichevski Committed: Tue Feb 21 20:00:33 2017 +0000 ---------------------------------------------------------------------- .../org/apache/http/pool/AbstractConnPool.java | 47 +++++++++++--------- 1 file changed, 25 insertions(+), 22 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/f7b81af5/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java ---------------------------------------------------------------------- diff --git a/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java b/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java index d34cf24..25a6bfc 100644 --- a/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java +++ b/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java @@ -38,6 +38,8 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -190,37 +192,37 @@ public abstract class AbstractConnPool> return new Future() { - private volatile boolean cancelled; - private volatile boolean done; - private volatile E entry; + private final AtomicBoolean cancelled = new AtomicBoolean(false); + private final AtomicBoolean done = new AtomicBoolean(false); + private final AtomicReference entryRef = new AtomicReference(null); @Override public boolean cancel(final boolean mayInterruptIfRunning) { - cancelled = true; - lock.lock(); - try { - condition.signalAll(); - } finally { - lock.unlock(); - } - synchronized (this) { - final boolean result = !done; - done = true; + if (cancelled.compareAndSet(false, true)) { + done.set(true); + lock.lock(); + try { + condition.signalAll(); + } finally { + lock.unlock(); + } if (callback != null) { callback.cancelled(); } - return result; + return true; + } else { + return false; } } @Override public boolean isCancelled() { - return cancelled; + return cancelled.get(); } @Override public boolean isDone() { - return done; + return done.get(); } @Override @@ -234,6 +236,7 @@ public abstract class AbstractConnPool> @Override public E get(final long timeout, final TimeUnit tunit) throws InterruptedException, ExecutionException, TimeoutException { + final E entry = entryRef.get(); if (entry != null) { return entry; } @@ -250,16 +253,16 @@ public abstract class AbstractConnPool> } } } - entry = leasedEntry; - done = true; - onLease(entry); + entryRef.set(leasedEntry); + done.set(true); + onLease(leasedEntry); if (callback != null) { - callback.completed(entry); + callback.completed(leasedEntry); } - return entry; + return leasedEntry; } } catch (IOException ex) { - done = true; + done.set(true); if (callback != null) { callback.failed(ex); }