diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AbstractAsyncPushHandler.java b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AbstractAsyncPushHandler.java index f46dd201f..1415c13d2 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AbstractAsyncPushHandler.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AbstractAsyncPushHandler.java @@ -29,6 +29,7 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.util.List; +import java.util.concurrent.atomic.AtomicReference; import org.apache.hc.core5.concurrent.FutureCallback; import org.apache.hc.core5.http.EntityDetails; @@ -52,9 +53,11 @@ public abstract class AbstractAsyncPushHandler implements AsyncPushConsumer { private final AsyncResponseConsumer responseConsumer; + private final AtomicReference promiseRef; public AbstractAsyncPushHandler(final AsyncResponseConsumer responseConsumer) { this.responseConsumer = Args.notNull(responseConsumer, "Response consumer"); + this.promiseRef = new AtomicReference<>(); } /** @@ -83,6 +86,7 @@ public final void consumePromise( final HttpResponse response, final EntityDetails entityDetails, final HttpContext httpContext) throws HttpException, IOException { + promiseRef.compareAndSet(null, promise); responseConsumer.consumeResponse(response, entityDetails, httpContext, new FutureCallback() { @Override @@ -96,7 +100,6 @@ public void completed(final T result) { @Override public void failed(final Exception cause) { - handleError(promise, cause); releaseResources(); } @@ -126,6 +129,7 @@ public final void streamEnd(final List trailers) throws HttpEx @Override public final void failed(final Exception cause) { responseConsumer.failed(cause); + handleError(promiseRef.get(), cause); releaseResources(); }