You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2025/09/13 05:00:33 UTC

(httpcomponents-core) branch master updated (c7ba1cb99 -> 8d85a8ca4)

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git


 discard c7ba1cb99 RFC 9113 conformance: use command queue to activate streams reserved with a push promise; improved test cases
 discard 96e83c990 RFC 9113 conformance: improved H2 stream creation and initialization, improved H2 stream state tracking and tracking of total stream counts
 discard 95be71ffc Disabled ReactiveClient test case failing intermittently with Github Actions
     new e103380ec RFC 9113 conformance: improved H2 stream creation and initialization, improved H2 stream state tracking and tracking of total stream counts
     new 8d85a8ca4 RFC 9113 conformance: use command queue to activate streams reserved with a push promise; improved test cases

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (c7ba1cb99)
            \
             N -- N -- N   refs/heads/master (8d85a8ca4)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../org/apache/hc/core5/testing/reactive/ReactiveClientTest.java     | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)


(httpcomponents-core) 02/02: RFC 9113 conformance: use command queue to activate streams reserved with a push promise; improved test cases

Posted by ol...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git

commit 8d85a8ca44e517260c672f396a44395721ea69af
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Tue Sep 9 14:06:28 2025 +0200

    RFC 9113 conformance: use command queue to activate streams reserved with a push promise; improved test cases
---
 .../impl/nio/AbstractH2StreamMultiplexer.java      |  31 +++-
 .../apache/hc/core5/http2/impl/nio/H2Stream.java   |   6 +-
 .../apache/hc/core5/http2/impl/nio/H2Streams.java  |  12 +-
 .../http2/nio/command/PushResponseCommand.java     |  65 +++++++++
 .../hc/core5/testing/nio/H2IntegrationTest.java    | 159 ++++++++++++---------
 5 files changed, 193 insertions(+), 80 deletions(-)

diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java
index 3d35f6655..99b6c7491 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java
@@ -82,6 +82,7 @@
 import org.apache.hc.core5.http2.impl.BasicH2TransportMetrics;
 import org.apache.hc.core5.http2.nio.AsyncPingHandler;
 import org.apache.hc.core5.http2.nio.command.PingCommand;
+import org.apache.hc.core5.http2.nio.command.PushResponseCommand;
 import org.apache.hc.core5.io.CloseMode;
 import org.apache.hc.core5.reactor.Command;
 import org.apache.hc.core5.reactor.ProtocolIOSession;
@@ -499,7 +500,7 @@ public final void onOutput() throws HttpException, IOException {
         }
 
         if (connState.compareTo(ConnectionHandshake.ACTIVE) <= 0 && remoteSettingState == SettingsHandshake.ACKED) {
-            while (streams.size() < remoteConfig.getMaxConcurrentStreams()) {
+            while (streams.getLocalCount() < remoteConfig.getMaxConcurrentStreams()) {
                 final Command command = ioSession.poll();
                 if (command == null) {
                     break;
@@ -510,6 +511,8 @@ public final void onOutput() throws HttpException, IOException {
                     executePing((PingCommand) command);
                 } else if (command instanceof RequestExecutionCommand) {
                     executeRequest((RequestExecutionCommand) command);
+                } else if (command instanceof PushResponseCommand) {
+                    executePush((PushResponseCommand) command);
                 }
                 if (!outputQueue.isEmpty()) {
                     return;
@@ -638,6 +641,23 @@ private void executeRequest(final RequestExecutionCommand requestExecutionComman
         }
     }
 
+    private void executePush(final PushResponseCommand pushResponseCommand) throws IOException, HttpException {
+        if (pushResponseCommand.isCancelled()) {
+            return;
+        }
+        final H2Stream stream = streams.lookupSeen(pushResponseCommand.getStreamId());
+        if (stream != null && stream.isReserved()) {
+            if (!stream.isLocalClosed()) {
+                stream.activate();
+                if (stream.isOutputReady()) {
+                    stream.produceOutput();
+                }
+            } else {
+                stream.abort();
+            }
+        }
+    }
+
     public final void onException(final Exception cause) {
         try {
             for (;;) {
@@ -812,7 +832,7 @@ private void consumeFrame(final RawFrame frame) throws HttpException, IOExceptio
                 if (streamId == 0) {
                     throw new H2ConnectionException(H2Error.PROTOCOL_ERROR, "Illegal stream id: " + streamId);
                 }
-                final H2Stream stream = streams.lookupValidOrNull(streamId);
+                final H2Stream stream = streams.lookupSeen(streamId);
                 if (stream != null) {
                     final ByteBuffer payload = frame.getPayload();
                     if (payload == null || payload.remaining() != 4) {
@@ -1247,7 +1267,8 @@ void appendState(final StringBuilder buf) {
                 .append(", connInputWindow=").append(connInputWindow)
                 .append(", connOutputWindow=").append(connOutputWindow)
                 .append(", outputQueue=").append(outputQueue.size())
-                .append(", streams.size=").append(streams.size())
+                .append(", streams.localCoubt=").append(streams.getLocalCount())
+                .append(", streams.remoteCount=").append(streams.getRemoteCount())
                 .append(", streams.lastLocal=").append(streams.getLastLocalId())
                 .append(", streams.lastRemote=").append(streams.getLastRemoteId());
     }
@@ -1362,9 +1383,11 @@ public void push(final List<Header> headers, final AsyncPushProducer pushProduce
                 ensureNotClosed();
                 final int promisedStreamId = streams.generateStreamId();
                 final H2StreamChannel channel = createChannel(promisedStreamId);
-                streams.createReserved(channel, outgoingPushPromise(channel, pushProducer));
+                final H2Stream stream = streams.createReserved(channel, outgoingPushPromise(channel, pushProducer));
 
                 commitPushPromise(id, promisedStreamId, headers);
+                stream.markRemoteClosed();
+                submitCommand(new PushResponseCommand(promisedStreamId));
             } finally {
                 ioSession.getLock().unlock();
             }
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Stream.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Stream.java
index 7ec30a035..676d929e9 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Stream.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Stream.java
@@ -120,6 +120,10 @@ boolean isRemoteClosed() {
         return remoteClosed;
     }
 
+    void markRemoteClosed() {
+        remoteClosed = true;
+    }
+
     boolean isLocalClosed() {
         return channel.isLocalClosed();
     }
@@ -170,7 +174,7 @@ void consumeData(final ByteBuffer src, final boolean endOfStream) throws HttpExc
     }
 
     boolean isOutputReady() {
-        return !channel.isLocalClosed() && handler.isOutputReady();
+        return !reserved && !channel.isLocalClosed() && handler.isOutputReady();
     }
 
     void produceOutput() throws HttpException, IOException {
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Streams.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Streams.java
index 62b348e4c..f602ebf98 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Streams.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Streams.java
@@ -60,10 +60,6 @@ public H2Streams(final StreamIdGenerator idGenerator) {
         this.remoteCount = new AtomicInteger(0);
     }
 
-    public int size() {
-        return streams.size();
-    }
-
     public boolean isEmpty() {
         return streams.isEmpty();
     }
@@ -80,6 +76,14 @@ public int getLastRemoteId() {
         return lastRemoteId.get();
     }
 
+    public int getLocalCount() {
+        return localCount.get();
+    }
+
+    public int getRemoteCount() {
+        return remoteCount.get();
+    }
+
     private H2Stream createStream(final H2StreamChannel channel, final H2StreamHandler streamHandler) {
         final int streamId = channel.getId();
         final boolean remoteStream = isOtherSide(streamId);
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/nio/command/PushResponseCommand.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/nio/command/PushResponseCommand.java
new file mode 100644
index 000000000..c5b22997f
--- /dev/null
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/nio/command/PushResponseCommand.java
@@ -0,0 +1,65 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.hc.core5.http2.nio.command;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.apache.hc.core5.annotation.Internal;
+import org.apache.hc.core5.reactor.Command;
+import org.apache.hc.core5.util.Args;
+
+/**
+ * Activates the stream reserved with a push promise
+ *
+ * @since 5.4
+ */
+@Internal
+public final class PushResponseCommand implements Command {
+
+    private final int streamId;
+    private final AtomicBoolean cancelled;
+
+    public PushResponseCommand(final int streamId) {
+        this.streamId = Args.positive(streamId, "Stream Id");
+        this.cancelled = new AtomicBoolean();
+    }
+
+    public int getStreamId() {
+        return streamId;
+    }
+
+    public boolean isCancelled() {
+        return cancelled.get();
+    }
+
+    @Override
+    public boolean cancel() {
+        return cancelled.compareAndSet(false, true);
+    }
+
+}
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2IntegrationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2IntegrationTest.java
index e825eae5a..9a5f4afba 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2IntegrationTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2IntegrationTest.java
@@ -30,20 +30,15 @@
 import static org.hamcrest.MatcherAssert.assertThat;
 
 import java.io.IOException;
-import java.io.InterruptedIOException;
 import java.net.InetSocketAddress;
 import java.util.LinkedList;
 import java.util.Queue;
 import java.util.StringTokenizer;
-import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
-import java.util.concurrent.LinkedBlockingDeque;
-import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 
-import org.apache.hc.core5.function.Supplier;
 import org.apache.hc.core5.http.ContentType;
 import org.apache.hc.core5.http.EndpointDetails;
 import org.apache.hc.core5.http.HeaderElements;
@@ -58,7 +53,6 @@
 import org.apache.hc.core5.http.ProtocolException;
 import org.apache.hc.core5.http.URIScheme;
 import org.apache.hc.core5.http.message.BasicHttpRequest;
-import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
 import org.apache.hc.core5.http.nio.AsyncServerRequestHandler;
 import org.apache.hc.core5.http.nio.entity.AsyncEntityProducers;
 import org.apache.hc.core5.http.nio.entity.DiscardingEntityConsumer;
@@ -169,53 +163,70 @@ protected void handle(
                 .build());
         client.start();
 
-        final BlockingQueue<Message<HttpResponse, String>> pushMessageQueue = new LinkedBlockingDeque<>();
+        final int reqNo = 200;
 
         final Future<ClientSessionEndpoint> connectFuture = client.connect(target, TIMEOUT);
         final ClientSessionEndpoint streamEndpoint = connectFuture.get();
 
-        final BasicHttpRequest request = BasicRequestBuilder.get()
-                .setHttpHost(target)
-                .setPath("/hello")
-                .build();
+        final CountDownLatch latch = new CountDownLatch(reqNo);
+        final Queue<Future<Message<HttpResponse, String>>> responseQueue = new LinkedList<>();
+        final Queue<Message<HttpResponse, String>> pushMessageQueue = new LinkedList<>();
 
-        final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(
-                new BasicRequestProducer(request, null),
-                new BasicResponseConsumer<>(new StringAsyncEntityConsumer()),
-                (r, c) -> new AbstractAsyncPushHandler<Message<HttpResponse, String>>(new BasicResponseConsumer<>(new StringAsyncEntityConsumer())) {
+        for (int i = 0; i < reqNo; i++) {
+            final BasicHttpRequest request = BasicRequestBuilder.get()
+                    .setHttpHost(target)
+                    .setPath("/hello")
+                    .build();
 
-                    @Override
-                    protected void handleResponse(
-                            final HttpRequest promise,
-                            final Message<HttpResponse, String> responseMessage) throws IOException, HttpException {
-                        try {
-                            pushMessageQueue.put(responseMessage);
-                        } catch (final InterruptedException ex) {
-                            Thread.currentThread().interrupt();
-                            throw new InterruptedIOException(ex.getMessage());
+            responseQueue.add(streamEndpoint.execute(
+                    new BasicRequestProducer(request, null),
+                    new BasicResponseConsumer<>(new StringAsyncEntityConsumer()),
+                    (r, c) -> new AbstractAsyncPushHandler<Message<HttpResponse, String>>(new BasicResponseConsumer<>(new StringAsyncEntityConsumer())) {
+
+                        @Override
+                        protected void handleResponse(
+                                final HttpRequest promise,
+                                final Message<HttpResponse, String> responseMessage) throws IOException, HttpException {
+                            pushMessageQueue.add(responseMessage);
+                            latch.countDown();
                         }
-                    }
 
-                },
-                null,
-                null);
-        final Message<HttpResponse, String> result1 = future1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
-        Assertions.assertNotNull(result1);
-        final HttpResponse response1 = result1.getHead();
-        final String entity1 = result1.getBody();
-        Assertions.assertNotNull(response1);
-        Assertions.assertEquals(200, response1.getCode());
-        Assertions.assertEquals("Hi there", entity1);
+                        @Override
+                        protected void handleError(final HttpRequest promise, final Exception cause) {
+                            latch.countDown();
+                        }
+                    },
+                    null,
+                    null));
+        }
 
-        final Message<HttpResponse, String> result2 = pushMessageQueue.poll(5, TimeUnit.SECONDS);
-        Assertions.assertNotNull(result2);
-        final HttpResponse response2 = result2.getHead();
-        final String entity2 = result2.getBody();
-        Assertions.assertEquals(200, response2.getCode());
-        Assertions.assertNotNull(entity2);
-        final StringTokenizer t1 = new StringTokenizer(entity2, "\r\n");
-        while (t1.hasMoreTokens()) {
-            Assertions.assertEquals("Pushing lots of stuff", t1.nextToken());
+        Assertions.assertTrue(latch.await(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
+
+        Assertions.assertEquals(reqNo, responseQueue.size());
+        Assertions.assertEquals(reqNo, pushMessageQueue.size());
+
+        while (!responseQueue.isEmpty()) {
+            final Future<Message<HttpResponse, String>> future = responseQueue.remove();
+            final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
+            Assertions.assertNotNull(result);
+            final HttpResponse response1 = result.getHead();
+            final String entity = result.getBody();
+            Assertions.assertNotNull(response1);
+            Assertions.assertEquals(200, response1.getCode());
+            Assertions.assertEquals("Hi there", entity);
+        }
+
+        while (!pushMessageQueue.isEmpty()) {
+            final Message<HttpResponse, String> pushMessage = pushMessageQueue.remove();
+            Assertions.assertNotNull(pushMessage);
+            final HttpResponse response = pushMessage.getHead();
+            final String entity = pushMessage.getBody();
+            Assertions.assertEquals(200, response.getCode());
+            Assertions.assertNotNull(entity);
+            final StringTokenizer t1 = new StringTokenizer(entity, "\r\n");
+            while (t1.hasMoreTokens()) {
+                Assertions.assertEquals("Pushing lots of stuff", t1.nextToken());
+            }
         }
     }
 
@@ -224,12 +235,10 @@ void testPushRefused() throws Exception {
         final H2TestServer server = resources.server();
         final H2TestClient client = resources.client();
 
-        final BlockingQueue<Exception> pushResultQueue = new LinkedBlockingDeque<>();
-        server.register("/hello", new Supplier<AsyncServerExchangeHandler>() {
-
-            @Override
-            public AsyncServerExchangeHandler get() {
-                return new MessageExchangeHandler<Void>(new DiscardingEntityConsumer<>()) {
+        final CountDownLatch latch = new CountDownLatch(2);
+        final Queue<Exception> pushResultQueue = new LinkedList<>();
+        server.register("/hello", () ->
+                new MessageExchangeHandler<Void>(new DiscardingEntityConsumer<>()) {
 
                     @Override
                     protected void handle(
@@ -239,34 +248,35 @@ protected void handle(
 
                         responseTrigger.pushPromise(
                                 new BasicHttpRequest(Method.GET, new HttpHost(scheme.id, "localhost"), "/stuff"),
-                                context, new BasicPushProducer(AsyncEntityProducers.create("Pushing all sorts of stuff")) {
+                                context,
+                                new BasicPushProducer(new MultiLineEntityProducer("Pushing lots of stuff", 50000)) {
 
-                            @Override
-                            public void failed(final Exception cause) {
-                                pushResultQueue.add(cause);
-                                super.failed(cause);
-                            }
+                                    @Override
+                                    public void failed(final Exception cause) {
+                                        pushResultQueue.add(cause);
+                                        latch.countDown();
+                                    }
+
+                                });
 
-                        });
                         responseTrigger.pushPromise(
                                 new BasicHttpRequest(Method.GET, new HttpHost(scheme.id, "localhost"), "/more-stuff"),
-                                context, new BasicPushProducer(new MultiLineEntityProducer("Pushing lots of stuff", 500)) {
+                                context,
+                                new BasicPushProducer(new MultiLineEntityProducer("Pushing lots of stuff", 50000)) {
+
+                                    @Override
+                                    public void failed(final Exception cause) {
+                                        pushResultQueue.add(cause);
+                                        latch.countDown();
+                                    }
 
-                            @Override
-                            public void failed(final Exception cause) {
-                                pushResultQueue.add(cause);
-                                super.failed(cause);
-                            }
+                                });
 
-                        });
                         responseTrigger.submitResponse(
                                 new BasicResponseProducer(HttpStatus.SC_OK, AsyncEntityProducers.create("Hi there")),
                                 context);
                     }
-                };
-            }
-
-        });
+                });
         final InetSocketAddress serverEndpoint = server.start();
 
         final HttpHost target = target(serverEndpoint);
@@ -286,7 +296,10 @@ public void failed(final Exception cause) {
 
         final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(
                 new BasicRequestProducer(request1, null),
-                new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
+                new BasicResponseConsumer<>(new StringAsyncEntityConsumer()),
+                (r, c) -> null, // refuse all push messages
+                null,
+                null);
         final Message<HttpResponse, String> result1 = future1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
         Assertions.assertNotNull(result1);
         final HttpResponse response1 = result1.getHead();
@@ -295,12 +308,16 @@ public void failed(final Exception cause) {
         Assertions.assertEquals(200, response1.getCode());
         Assertions.assertEquals("Hi there", entity1);
 
-        final Object result2 = pushResultQueue.poll(5, TimeUnit.SECONDS);
+        Assertions.assertTrue(latch.await(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
+
+        Assertions.assertEquals(2, pushResultQueue.size());
+
+        final Object result2 = pushResultQueue.poll();
         Assertions.assertNotNull(result2);
         Assertions.assertTrue(result2 instanceof H2StreamResetException);
         Assertions.assertEquals(H2Error.REFUSED_STREAM.getCode(), ((H2StreamResetException) result2).getCode());
 
-        final Object result3 = pushResultQueue.poll(5, TimeUnit.SECONDS);
+        final Object result3 = pushResultQueue.poll();
         Assertions.assertNotNull(result3);
         Assertions.assertTrue(result3 instanceof H2StreamResetException);
         Assertions.assertEquals(H2Error.REFUSED_STREAM.getCode(), ((H2StreamResetException) result3).getCode());


(httpcomponents-core) 01/02: RFC 9113 conformance: improved H2 stream creation and initialization, improved H2 stream state tracking and tracking of total stream counts

Posted by ol...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git

commit e103380eca2dbdb21306e0d7b31fce21dd89aa01
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Sun Sep 7 17:19:47 2025 +0200

    RFC 9113 conformance: improved H2 stream creation and initialization, improved H2 stream state tracking and tracking of total stream counts
---
 .../impl/nio/AbstractH2StreamMultiplexer.java      | 167 +++++++++------------
 .../http2/impl/nio/ClientH2StreamMultiplexer.java  |  52 +++----
 .../apache/hc/core5/http2/impl/nio/H2Stream.java   |  78 ++++++++--
 .../apache/hc/core5/http2/impl/nio/H2Streams.java  |  86 +++++++----
 .../http2/impl/nio/ServerH2StreamMultiplexer.java  |  42 ++++--
 .../impl/nio/TestAbstractH2StreamMultiplexer.java  |  43 +++---
 6 files changed, 269 insertions(+), 199 deletions(-)

diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java
index 861bc9f87..3d35f6655 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java
@@ -57,13 +57,14 @@
 import org.apache.hc.core5.http.impl.BasicEndpointDetails;
 import org.apache.hc.core5.http.impl.BasicHttpConnectionMetrics;
 import org.apache.hc.core5.http.impl.CharCodingSupport;
+import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
 import org.apache.hc.core5.http.nio.AsyncPushConsumer;
 import org.apache.hc.core5.http.nio.AsyncPushProducer;
 import org.apache.hc.core5.http.nio.HandlerFactory;
 import org.apache.hc.core5.http.nio.command.CommandSupport;
-import org.apache.hc.core5.http.nio.command.ExecutableCommand;
+import org.apache.hc.core5.http.nio.command.RequestExecutionCommand;
 import org.apache.hc.core5.http.nio.command.ShutdownCommand;
-import org.apache.hc.core5.http.protocol.HttpCoreContext;
+import org.apache.hc.core5.http.protocol.HttpContext;
 import org.apache.hc.core5.http.protocol.HttpProcessor;
 import org.apache.hc.core5.http2.H2ConnectionException;
 import org.apache.hc.core5.http2.H2Error;
@@ -172,6 +173,18 @@ public String getId() {
         return ioSession.getId();
     }
 
+    BasicHttpConnectionMetrics getConnMetrics() {
+        return connMetrics;
+    }
+
+    HttpProcessor getHttpProcessor() {
+        return httpProcessor;
+    }
+
+    void submitCommand(final Command command) {
+        ioSession.enqueue(command, Command.Priority.NORMAL);
+    }
+
     abstract void validateSetting(H2Param param, int value) throws H2ConnectionException;
 
     abstract H2Setting[] generateSettings(H2Config localConfig);
@@ -182,18 +195,18 @@ public String getId() {
 
     abstract void acceptPushFrame() throws H2ConnectionException;
 
-    abstract H2StreamHandler createRemotelyInitiatedStream(
-            H2StreamChannel channel,
+    abstract H2StreamHandler incomingRequest(H2StreamChannel channel) throws IOException;
+
+    abstract H2StreamHandler incomingPushPromise(H2StreamChannel channel,
+                                                 HandlerFactory<AsyncPushConsumer> pushHandlerFactory) throws IOException;
 
-            HttpProcessor httpProcessor,
-            BasicHttpConnectionMetrics connMetrics,
-            HandlerFactory<AsyncPushConsumer> pushHandlerFactory) throws IOException;
+    abstract H2StreamHandler outgoingRequest(H2StreamChannel channel,
+                                             AsyncClientExchangeHandler exchangeHandler,
+                                             HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
+                                             HttpContext context) throws IOException;
 
-    abstract H2StreamHandler createLocallyInitiatedStream(
-            ExecutableCommand command,
-            H2StreamChannel channel,
-            HttpProcessor httpProcessor,
-            BasicHttpConnectionMetrics connMetrics) throws IOException;
+    abstract H2StreamHandler outgoingPushPromise(H2StreamChannel channel,
+                                                 AsyncPushProducer pushProducer) throws IOException;
 
     abstract boolean allowGracefulAbort(H2Stream stream);
 
@@ -495,11 +508,11 @@ public final void onOutput() throws HttpException, IOException {
                     executeShutdown((ShutdownCommand) command);
                 } else if (command instanceof PingCommand) {
                     executePing((PingCommand) command);
-                } else if (command instanceof ExecutableCommand) {
-                    executeRequest((ExecutableCommand) command);
-                    if (!outputQueue.isEmpty()) {
-                        return;
-                    }
+                } else if (command instanceof RequestExecutionCommand) {
+                    executeRequest((RequestExecutionCommand) command);
+                }
+                if (!outputQueue.isEmpty()) {
+                    return;
                 }
             }
         }
@@ -507,8 +520,8 @@ public final void onOutput() throws HttpException, IOException {
             int liveStreams = 0;
             for (final Iterator<H2Stream> it = streams.iterator(); it.hasNext(); ) {
                 final H2Stream stream = it.next();
-                if (stream.isLocalClosed() && stream.isRemoteClosed()) {
-                    streams.release(stream);
+                if (stream.isClosedPastLingerDeadline()) {
+                    streams.dropStreamId(stream.getId());
                     it.remove();
                 } else {
                     if (streams.isSameSide(stream.getId()) || stream.getId() <= streams.getLastRemoteId()) {
@@ -555,10 +568,10 @@ public final void onTimeout(final Timeout timeout) throws HttpException, IOExcep
         final RawFrame goAway;
         if (localSettingState != SettingsHandshake.ACKED) {
             goAway = frameFactory.createGoAway(streams.getLastRemoteId(), H2Error.SETTINGS_TIMEOUT,
-                            "Setting timeout (" + timeout + ")");
+                    "Setting timeout (" + timeout + ")");
         } else {
             goAway = frameFactory.createGoAway(streams.getLastRemoteId(), H2Error.NO_ERROR,
-                            "Timeout due to inactivity (" + timeout + ")");
+                    "Timeout due to inactivity (" + timeout + ")");
         }
         commitFrame(goAway);
         for (final Iterator<H2Stream> it = streams.iterator(); it.hasNext(); ) {
@@ -601,14 +614,13 @@ private void executePing(final PingCommand pingCommand) throws IOException {
         commitFrame(ping);
     }
 
-    private void executeRequest(final ExecutableCommand executableCommand) throws IOException, HttpException {
+    private void executeRequest(final RequestExecutionCommand requestExecutionCommand) throws IOException, HttpException {
         final int streamId = streams.generateStreamId();
-        final H2StreamChannel channel = createChannel(streamId, true);
-        final H2StreamHandler streamHandler = createLocallyInitiatedStream(
-                executableCommand, channel, httpProcessor, connMetrics);
-
-        final H2Stream stream = new H2Stream(channel, streamHandler);
-        streams.addLocallyInitiated(stream);
+        final H2StreamChannel channel = createChannel(streamId);
+        final H2Stream stream = streams.createActive(channel, outgoingRequest(channel,
+                requestExecutionCommand.getExchangeHandler(),
+                requestExecutionCommand.getPushHandlerFactory(),
+                requestExecutionCommand.getContext()));
 
         if (streamListener != null) {
             final int initInputWindow = stream.getInputWindow().get();
@@ -620,23 +632,12 @@ private void executeRequest(final ExecutableCommand executableCommand) throws IO
         if (stream.isOutputReady()) {
             stream.produceOutput();
         }
-        final CancellableDependency cancellableDependency = executableCommand.getCancellableDependency();
+        final CancellableDependency cancellableDependency = requestExecutionCommand.getCancellableDependency();
         if (cancellableDependency != null) {
             cancellableDependency.setDependency(stream::abort);
         }
     }
 
-    public void executePush(final int promisedStreamId, final AsyncPushProducer pushProducer) {
-        final H2StreamChannel channel = createChannel(promisedStreamId, true);
-        final HttpCoreContext context = HttpCoreContext.create();
-        context.setSSLSession(getSSLSession());
-        context.setEndpointDetails(getEndpointDetails());
-        final H2StreamHandler streamHandler = new ServerPushH2StreamHandler(
-                channel, httpProcessor, connMetrics, pushProducer, context);
-        final H2Stream stream = new H2Stream(channel, streamHandler);
-        streams.addLocallyInitiated(stream);
-    }
-
     public final void onException(final Exception cause) {
         try {
             for (;;) {
@@ -702,8 +703,8 @@ private void consumeFrame(final RawFrame frame) throws HttpException, IOExceptio
                     stream.localReset(ex, ex.getCause() != null ? H2Error.INTERNAL_ERROR : H2Error.CANCEL);
                 }
 
-                if (stream.isTerminated()) {
-                    streams.release(stream);
+                if (stream.isClosed()) {
+                    stream.releaseResources();
                     requestSessionOutput();
                 }
             }
@@ -722,26 +723,20 @@ private void consumeFrame(final RawFrame frame) throws HttpException, IOExceptio
                         throw new H2ConnectionException(H2Error.PROTOCOL_ERROR, "GOAWAY received");
                     }
 
-                    final H2StreamChannel channel = createChannel(streamId, false);
-                    final H2StreamHandler streamHandler;
+                    final H2StreamChannel channel = createChannel(streamId);
                     if (connState.compareTo(ConnectionHandshake.ACTIVE) <= 0) {
-                        streamHandler = createRemotelyInitiatedStream(channel, httpProcessor, connMetrics, null);
+                        stream = streams.createActive(channel, incomingRequest(channel));
                     } else {
-                        streamHandler = NoopH2StreamHandler.INSTANCE;
-                        channel.markLocalClosed();
-                    }
-
-                    stream = new H2Stream(channel, streamHandler);
-                    if (stream.isOutputReady()) {
-                        stream.produceOutput();
+                        channel.localReset(H2Error.REFUSED_STREAM);
+                        stream = streams.createActive(channel, NoopH2StreamHandler.INSTANCE);
                     }
-                    streams.addRemotelyInitiated(stream);
                 } else if (stream.isLocalClosed() && stream.isRemoteClosed()) {
                     throw new H2ConnectionException(H2Error.STREAM_CLOSED, "Stream closed");
+                } else if (stream.isReserved()) {
+                    stream.activate();
                 }
                 try {
                     consumeHeaderFrame(frame, stream);
-
                     if (stream.isOutputReady()) {
                         stream.produceOutput();
                     }
@@ -753,8 +748,8 @@ private void consumeFrame(final RawFrame frame) throws HttpException, IOExceptio
                     stream.handle(ex);
                 }
 
-                if (stream.isTerminated()) {
-                    streams.release(stream);
+                if (stream.isClosed()) {
+                    stream.releaseResources();
                     requestSessionOutput();
                 }
             }
@@ -779,8 +774,8 @@ private void consumeFrame(final RawFrame frame) throws HttpException, IOExceptio
                     stream.localReset(ex, ex.getCause() != null ? H2Error.INTERNAL_ERROR : H2Error.CANCEL);
                 }
 
-                if (stream.isTerminated()) {
-                    streams.release(stream);
+                if (stream.isClosed()) {
+                    stream.releaseResources();
                     requestSessionOutput();
                 }
             }
@@ -829,7 +824,6 @@ private void consumeFrame(final RawFrame frame) throws HttpException, IOExceptio
                         requestSessionOutput();
                     } else {
                         stream.fail(new H2StreamResetException(errorCode, "Stream reset (" + errorCode + ")"));
-                        streams.release(stream);
                         requestSessionOutput();
                     }
                 }
@@ -900,7 +894,7 @@ private void consumeFrame(final RawFrame frame) throws HttpException, IOExceptio
                     throw new H2ConnectionException(H2Error.PROTOCOL_ERROR, "Push is disabled");
                 }
 
-                final H2Stream stream = streams.lookup(streamId);
+                final H2Stream stream = streams.lookupValid(streamId);
                 if (stream.isRemoteClosed()) {
                     stream.localReset(new H2StreamResetException(H2Error.STREAM_CLOSED, "Stream closed"));
                     break;
@@ -918,19 +912,14 @@ private void consumeFrame(final RawFrame frame) throws HttpException, IOExceptio
                     throw new H2ConnectionException(H2Error.PROTOCOL_ERROR, "Stream already open: " + promisedStreamId);
                 }
 
-                final H2StreamChannel channel = createChannel(promisedStreamId, false);
-                final H2StreamHandler streamHandler;
+                final H2StreamChannel channel = createChannel(promisedStreamId);
+                final H2Stream promisedStream;
                 if (connState.compareTo(ConnectionHandshake.ACTIVE) <= 0) {
-                    streamHandler = createRemotelyInitiatedStream(channel, httpProcessor, connMetrics,
-                            stream.getPushHandlerFactory());
+                    promisedStream = streams.createReserved(channel, incomingPushPromise(channel, stream.getPushHandlerFactory()));
                 } else {
-                    streamHandler = NoopH2StreamHandler.INSTANCE;
-                    channel.markLocalClosed();
+                    channel.localReset(H2Error.REFUSED_STREAM);
+                    promisedStream = streams.createActive(channel, NoopH2StreamHandler.INSTANCE);
                 }
-
-                final H2Stream promisedStream = new H2Stream(channel, streamHandler);
-                streams.addRemotelyInitiated(promisedStream);
-
                 try {
                     consumePushPromiseFrame(frame, payload, promisedStream);
                 } catch (final H2StreamResetException ex) {
@@ -1133,9 +1122,10 @@ private void produceOutput() throws HttpException, IOException {
             if (!stream.isLocalClosed() && stream.getOutputWindow().get() > 0) {
                 stream.produceOutput();
             }
-            if (stream.isTerminated()) {
+            if (stream.isClosedPastLingerDeadline()) {
+                streams.dropStreamId(stream.getId());
+                stream.releaseResources();
                 it.remove();
-                streams.release(stream);
                 requestSessionOutput();
             }
             if (!outputQueue.isEmpty()) {
@@ -1303,12 +1293,12 @@ ByteBuffer getContent() {
 
     }
 
-    H2StreamChannel createChannel(final int streamId, final boolean idle) {
-        return new H2StreamChannelImpl(streamId, idle, initInputWinSize, initOutputWinSize);
+    H2StreamChannel createChannel(final int streamId) {
+        return new H2StreamChannelImpl(streamId, initInputWinSize, initOutputWinSize);
     }
 
-    void addStream(final H2Stream stream) throws H2ConnectionException {
-        streams.addLocallyInitiated(stream);
+    H2Stream createStream(final H2StreamChannel channel, final H2StreamHandler streamHandler) throws H2ConnectionException {
+        return streams.createActive(channel, streamHandler);
     }
 
     class H2StreamChannelImpl implements H2StreamChannel {
@@ -1317,14 +1307,11 @@ class H2StreamChannelImpl implements H2StreamChannel {
         private final AtomicInteger inputWindow;
         private final AtomicInteger outputWindow;
 
-        private volatile boolean idle;
         private volatile boolean localClosed;
-
         private volatile long localResetTime;
 
-        H2StreamChannelImpl(final int id, final boolean idle, final int initialInputWindowSize, final int initialOutputWindowSize) {
+        H2StreamChannelImpl(final int id, final int initialInputWindowSize, final int initialOutputWindowSize) {
             this.id = id;
-            this.idle = idle;
             this.inputWindow = new AtomicInteger(initialInputWindowSize);
             this.outputWindow = new AtomicInteger(initialOutputWindowSize);
         }
@@ -1358,7 +1345,6 @@ public void submit(final List<Header> headers, final boolean endStream) throws I
                     throw new H2ConnectionException(H2Error.INTERNAL_ERROR, "Message headers are missing");
                 }
                 ensureNotClosed();
-                idle = false;
                 commitHeaders(id, headers, endStream);
                 if (endStream) {
                     localClosed = true;
@@ -1371,15 +1357,14 @@ public void submit(final List<Header> headers, final boolean endStream) throws I
         @Override
         public void push(final List<Header> headers, final AsyncPushProducer pushProducer) throws HttpException, IOException {
             acceptPushRequest();
-
             ioSession.getLock().lock();
             try {
                 ensureNotClosed();
-
                 final int promisedStreamId = streams.generateStreamId();
-                executePush(promisedStreamId, pushProducer);
+                final H2StreamChannel channel = createChannel(promisedStreamId);
+                streams.createReserved(channel, outgoingPushPromise(channel, pushProducer));
+
                 commitPushPromise(id, promisedStreamId, headers);
-                idle = false;
             } finally {
                 ioSession.getLock().unlock();
             }
@@ -1450,12 +1435,9 @@ public boolean localReset(final int code) throws IOException {
                 localClosed = true;
                 localResetTime = System.currentTimeMillis();
 
-                if (!idle) {
-                    final RawFrame resetStream = frameFactory.createResetStream(id, code);
-                    commitFrameInternal(resetStream);
-                    return true;
-                }
-                return false;
+                final RawFrame resetStream = frameFactory.createResetStream(id, code);
+                commitFrameInternal(resetStream);
+                return true;
             } finally {
                 ioSession.getLock().unlock();
             }
@@ -1484,8 +1466,7 @@ public String toString() {
                     .append(", inputWindow=").append(inputWindow)
                     .append(", outputWindow=").append(outputWindow)
                     .append(", localClosed=").append(localClosed)
-                    .append(", idle=").append(idle);
-            buf.append("]");
+                    .append("]");
             return buf.toString();
         }
 
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ClientH2StreamMultiplexer.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ClientH2StreamMultiplexer.java
index 9102dd1d8..24ecd70df 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ClientH2StreamMultiplexer.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ClientH2StreamMultiplexer.java
@@ -30,12 +30,11 @@
 
 import org.apache.hc.core5.annotation.Internal;
 import org.apache.hc.core5.http.config.CharCodingConfig;
-import org.apache.hc.core5.http.impl.BasicHttpConnectionMetrics;
 import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
 import org.apache.hc.core5.http.nio.AsyncPushConsumer;
+import org.apache.hc.core5.http.nio.AsyncPushProducer;
 import org.apache.hc.core5.http.nio.HandlerFactory;
-import org.apache.hc.core5.http.nio.command.ExecutableCommand;
-import org.apache.hc.core5.http.nio.command.RequestExecutionCommand;
+import org.apache.hc.core5.http.protocol.HttpContext;
 import org.apache.hc.core5.http.protocol.HttpCoreContext;
 import org.apache.hc.core5.http.protocol.HttpProcessor;
 import org.apache.hc.core5.http2.H2ConnectionException;
@@ -114,7 +113,7 @@ void acceptHeaderFrame() throws H2ConnectionException {
     }
 
     @Override
-    void acceptPushFrame() throws H2ConnectionException {
+    void acceptPushFrame() {
     }
 
     @Override
@@ -123,35 +122,36 @@ void acceptPushRequest() throws H2ConnectionException {
     }
 
     @Override
-    H2StreamHandler createLocallyInitiatedStream(
-            final ExecutableCommand command,
+    H2StreamHandler outgoingRequest(
             final H2StreamChannel channel,
-            final HttpProcessor httpProcessor,
-            final BasicHttpConnectionMetrics connMetrics) throws IOException {
-        if (command instanceof RequestExecutionCommand) {
-            final RequestExecutionCommand executionCommand = (RequestExecutionCommand) command;
-            final AsyncClientExchangeHandler exchangeHandler = executionCommand.getExchangeHandler();
-            final HandlerFactory<AsyncPushConsumer> pushHandlerFactory = executionCommand.getPushHandlerFactory();
-            final HttpCoreContext context = HttpCoreContext.castOrCreate(executionCommand.getContext());
-            context.setSSLSession(getSSLSession());
-            context.setEndpointDetails(getEndpointDetails());
-            return new ClientH2StreamHandler(channel, httpProcessor, connMetrics, exchangeHandler,
-                    pushHandlerFactory != null ? pushHandlerFactory : this.pushHandlerFactory,
-                    context);
-        }
-        throw new H2ConnectionException(H2Error.INTERNAL_ERROR, "Unexpected executable command");
+            final AsyncClientExchangeHandler exchangeHandler,
+            final HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
+            final HttpContext context) {
+        final HttpCoreContext coreContext = HttpCoreContext.castOrCreate(context);
+        coreContext.setSSLSession(getSSLSession());
+        coreContext.setEndpointDetails(getEndpointDetails());
+        return new ClientH2StreamHandler(channel, getHttpProcessor(), getConnMetrics(), exchangeHandler,
+                pushHandlerFactory != null ? pushHandlerFactory : this.pushHandlerFactory,
+                coreContext);
     }
 
     @Override
-    H2StreamHandler createRemotelyInitiatedStream(
-            final H2StreamChannel channel,
-            final HttpProcessor httpProcessor,
-            final BasicHttpConnectionMetrics connMetrics,
-            final HandlerFactory<AsyncPushConsumer> pushHandlerFactory) throws IOException {
+    H2StreamHandler incomingRequest(final H2StreamChannel channel) throws IOException {
+        throw new H2ConnectionException(H2Error.PROTOCOL_ERROR, "Illegal incoming request");
+    }
+
+    @Override
+    H2StreamHandler outgoingPushPromise(final H2StreamChannel channel, final AsyncPushProducer pushProducer) throws IOException {
+        throw new H2ConnectionException(H2Error.PROTOCOL_ERROR, "Illegal attempt to send push promise");
+    }
+
+    @Override
+    H2StreamHandler incomingPushPromise(final H2StreamChannel channel,
+                                        final HandlerFactory<AsyncPushConsumer> pushHandlerFactory) {
         final HttpCoreContext context = HttpCoreContext.create();
         context.setSSLSession(getSSLSession());
         context.setEndpointDetails(getEndpointDetails());
-        return new ClientPushH2StreamHandler(channel, httpProcessor, connMetrics,
+        return new ClientPushH2StreamHandler(channel, getHttpProcessor(), getConnMetrics(),
                 pushHandlerFactory != null ? pushHandlerFactory : this.pushHandlerFactory,
                 context);
     }
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Stream.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Stream.java
index 90e69f68d..7ec30a035 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Stream.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Stream.java
@@ -33,6 +33,7 @@
 import java.util.List;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Consumer;
 
 import org.apache.hc.core5.http.Header;
 import org.apache.hc.core5.http.HttpException;
@@ -45,17 +46,23 @@
 
 class H2Stream {
 
+    enum State { RESERVED, OPEN, CLOSED }
+
     private static final long LINGER_TIME = 1000; // 1 second
 
     private final H2StreamChannel channel;
     private final H2StreamHandler handler;
+    private final Consumer<State> stateChangeCallback;
     private final AtomicBoolean released;
 
+    private volatile boolean reserved;
     private volatile boolean remoteClosed;
 
-    H2Stream(final H2StreamChannel channel, final H2StreamHandler handler) {
+    H2Stream(final H2StreamChannel channel, final H2StreamHandler handler, final Consumer<State> stateChangeCallback) {
         this.channel = channel;
         this.handler = handler;
+        this.stateChangeCallback = stateChangeCallback;
+        this.reserved = true;
         this.released = new AtomicBoolean();
     }
 
@@ -63,6 +70,27 @@ int getId() {
         return channel.getId();
     }
 
+    boolean isReserved() {
+        return reserved;
+    }
+
+    private void triggerOpen() {
+        if (stateChangeCallback != null) {
+            stateChangeCallback.accept(State.OPEN);
+        }
+    }
+
+    private void triggerClosed() {
+        if (stateChangeCallback != null) {
+            stateChangeCallback.accept(State.CLOSED);
+        }
+    }
+
+    void activate() {
+        reserved = false;
+        triggerOpen();
+    }
+
     AtomicInteger getOutputWindow() {
         return channel.getOutputWindow();
     }
@@ -71,13 +99,21 @@ AtomicInteger getInputWindow() {
         return channel.getInputWindow();
     }
 
-    private boolean isPastResetDeadline() {
+    private boolean isPastLingerDeadline() {
         final long localResetTime = channel.getLocalResetTime();
         return localResetTime > 0 && localResetTime + LINGER_TIME < System.currentTimeMillis();
     }
 
-    boolean isTerminated() {
-        return channel.isLocalClosed() && (remoteClosed || isPastResetDeadline());
+    boolean isClosedPastLingerDeadline() {
+        return channel.isLocalClosed() && (remoteClosed || isPastLingerDeadline());
+    }
+
+    boolean isClosed() {
+        return channel.isLocalClosed() && (remoteClosed || channel.isLocalReset());
+    }
+
+    boolean isActive() {
+        return !reserved && !isClosed();
     }
 
     boolean isRemoteClosed() {
@@ -134,7 +170,7 @@ void consumeData(final ByteBuffer src, final boolean endOfStream) throws HttpExc
     }
 
     boolean isOutputReady() {
-        return handler.isOutputReady();
+        return !channel.isLocalClosed() && handler.isOutputReady();
     }
 
     void produceOutput() throws HttpException, IOException {
@@ -153,16 +189,24 @@ void fail(final Exception cause) {
         remoteClosed = true;
         channel.markLocalClosed();
         if (released.compareAndSet(false, true)) {
-            handler.failed(cause);
-            handler.releaseResources();
+            try {
+                handler.failed(cause);
+                handler.releaseResources();
+            } finally {
+                triggerClosed();
+            }
         }
     }
 
     void localReset(final Exception cause, final int code) throws IOException {
         channel.localReset(code);
         if (released.compareAndSet(false, true)) {
-            handler.failed(cause);
-            handler.releaseResources();
+            try {
+                handler.failed(cause);
+                handler.releaseResources();
+            } finally {
+                triggerClosed();
+            }
         }
     }
 
@@ -185,8 +229,12 @@ HandlerFactory<AsyncPushConsumer> getPushHandlerFactory() {
     boolean abort() {
         final boolean cancelled = channel.cancel();
         if (released.compareAndSet(false, true)) {
-            handler.failed(new StreamClosedException());
-            handler.releaseResources();
+            try {
+                handler.failed(new StreamClosedException());
+                handler.releaseResources();
+            } finally {
+                triggerClosed();
+            }
         }
         return cancelled;
     }
@@ -194,7 +242,7 @@ boolean abort() {
     boolean abortGracefully() throws IOException {
         if (!isLocalClosed() && isRemoteClosed()) {
             channel.endStream();
-            handler.releaseResources();
+            releaseResources();
             return true;
         } else {
             return abort();
@@ -203,7 +251,11 @@ boolean abortGracefully() throws IOException {
 
     void releaseResources() {
         if (released.compareAndSet(false, true)) {
-            handler.releaseResources();
+            try {
+                handler.releaseResources();
+            } finally {
+                triggerClosed();
+            }
         }
     }
 
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Streams.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Streams.java
index 53b3de5d7..62b348e4c 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Streams.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Streams.java
@@ -47,6 +47,8 @@ class H2Streams {
     private final Queue<H2Stream> streams;
     private final AtomicInteger lastLocalId;
     private final AtomicInteger lastRemoteId;
+    private final AtomicInteger localCount;
+    private final AtomicInteger remoteCount;
 
     public H2Streams(final StreamIdGenerator idGenerator) {
         this.idGenerator = Args.notNull(idGenerator, "Stream id generator");
@@ -54,6 +56,8 @@ public H2Streams(final StreamIdGenerator idGenerator) {
         this.streams = new ConcurrentLinkedQueue<>();
         this.lastLocalId = new AtomicInteger(0);
         this.lastRemoteId = new AtomicInteger(0);
+        this.localCount = new AtomicInteger(0);
+        this.remoteCount = new AtomicInteger(0);
     }
 
     public int size() {
@@ -76,37 +80,50 @@ public int getLastRemoteId() {
         return lastRemoteId.get();
     }
 
-    public void addLocallyInitiated(final H2Stream stream) throws H2ConnectionException {
-        final int streamId = stream.getId();
-        if (isOtherSide(streamId)) {
-            throw new H2ConnectionException(H2Error.PROTOCOL_ERROR, "Illegal stream id");
+    private H2Stream createStream(final H2StreamChannel channel, final H2StreamHandler streamHandler) {
+        final int streamId = channel.getId();
+        final boolean remoteStream = isOtherSide(streamId);
+        final H2Stream stream = new H2Stream(channel, streamHandler, state -> {
+            final AtomicInteger count = remoteStream ? remoteCount : localCount;
+            switch (state) {
+                case OPEN:
+                    count.incrementAndGet();
+                    break;
+                case CLOSED:
+                    count.decrementAndGet();
+            }
+        });
+        if (remoteStream) {
+            final int currentId = lastRemoteId.get();
+            if (streamId > currentId) {
+                lastRemoteId.compareAndSet(currentId, streamId);
+            }
         }
         streamMap.put(streamId, stream);
         streams.add(stream);
+        return stream;
     }
 
-    public void addRemotelyInitiated(final H2Stream stream) throws H2ConnectionException {
-        final int streamId = stream.getId();
-        if (isSameSide(streamId)) {
-            throw new H2ConnectionException(H2Error.PROTOCOL_ERROR, "Illegal stream id");
+    public H2Stream createActive(final H2StreamChannel channel, final H2StreamHandler streamHandler) {
+        final H2Stream stream = createStream(channel, streamHandler);
+        if (!stream.isClosed()) {
+            stream.activate();
         }
-        final int currentId = lastRemoteId.get();
-        if (streamId > currentId) {
-            lastRemoteId.compareAndSet(currentId, streamId);
-        }
-        streamMap.put(streamId, stream);
-        streams.add(stream);
+        return stream;
+    }
+
+    public H2Stream createReserved(final H2StreamChannel channel, final H2StreamHandler streamHandler) {
+        return createStream(channel, streamHandler);
     }
 
-    public void release(final H2Stream stream) {
-        streamMap.remove(stream.getId());
-        stream.releaseResources();
+    public void dropStreamId(final int streamId) {
+        streamMap.remove(streamId);
     }
 
     public void shutdownAndReleaseAll() {
         for (final Iterator<H2Stream> it = streams.iterator(); it.hasNext(); ) {
             final H2Stream stream = it.next();
-            if (stream.isLocalClosed() && stream.isRemoteClosed()) {
+            if (stream.isClosed()) {
                 stream.releaseResources();
             } else {
                 stream.fail(new ConnectionClosedException());
@@ -120,22 +137,26 @@ public H2Stream lookup(final int streamId) {
         return streamMap.get(streamId);
     }
 
+    boolean hasBeenSeen(final int streamId) {
+        return streamId <= (isSameSide(streamId) ? lastLocalId : lastRemoteId).get();
+    }
+
+    boolean isClosed(final H2Stream stream, final int streamId) {
+        return stream != null ? stream.isLocalClosed() && stream.isRemoteClosed() : hasBeenSeen(streamId);
+    }
+
     public H2Stream lookupValidOrNull(final int streamId) throws H2ConnectionException {
         final H2Stream stream = streamMap.get(streamId);
-        if (stream == null) {
-            if (idGenerator.isSameSide(streamId)) {
-                if (streamId <= lastLocalId.get()) {
-                    throw new H2ConnectionException(H2Error.STREAM_CLOSED, "Stream closed");
-                }
-            } else {
-                if (streamId <= lastRemoteId.get()) {
-                    throw new H2ConnectionException(H2Error.STREAM_CLOSED, "Stream closed");
-                }
-            }
-        } else {
-            if (stream.isLocalClosed() && stream.isRemoteClosed()) {
-                throw new H2ConnectionException(H2Error.STREAM_CLOSED, "Stream closed");
-            }
+        if (isClosed(stream, streamId)) {
+            throw new H2ConnectionException(H2Error.STREAM_CLOSED, "Stream closed");
+        }
+        return stream;
+    }
+
+    public H2Stream lookupSeen(final int streamId) throws H2ConnectionException {
+        final H2Stream stream = streamMap.get(streamId);
+        if (stream == null && !hasBeenSeen(streamId)) {
+            throw new H2ConnectionException(H2Error.PROTOCOL_ERROR, "Unexpected stream id: " + streamId);
         }
         return stream;
     }
@@ -166,5 +187,4 @@ public int generateStreamId() {
         }
     }
 
-
 }
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerH2StreamMultiplexer.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerH2StreamMultiplexer.java
index 94669275f..046047398 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerH2StreamMultiplexer.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerH2StreamMultiplexer.java
@@ -35,11 +35,12 @@
 import org.apache.hc.core5.http.HttpException;
 import org.apache.hc.core5.http.RequestHeaderFieldsTooLargeException;
 import org.apache.hc.core5.http.config.CharCodingConfig;
-import org.apache.hc.core5.http.impl.BasicHttpConnectionMetrics;
+import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
 import org.apache.hc.core5.http.nio.AsyncPushConsumer;
+import org.apache.hc.core5.http.nio.AsyncPushProducer;
 import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
 import org.apache.hc.core5.http.nio.HandlerFactory;
-import org.apache.hc.core5.http.nio.command.ExecutableCommand;
+import org.apache.hc.core5.http.protocol.HttpContext;
 import org.apache.hc.core5.http.protocol.HttpCoreContext;
 import org.apache.hc.core5.http.protocol.HttpProcessor;
 import org.apache.hc.core5.http2.H2ConnectionException;
@@ -103,11 +104,11 @@ H2Setting[] generateSettings(final H2Config localConfig) {
     }
 
     @Override
-    void acceptHeaderFrame() throws H2ConnectionException {
+    void acceptHeaderFrame() {
     }
 
     @Override
-    void acceptPushRequest() throws H2ConnectionException {
+    void acceptPushRequest() {
     }
 
     @Override
@@ -116,24 +117,35 @@ void acceptPushFrame() throws H2ConnectionException {
     }
 
     @Override
-    H2StreamHandler createRemotelyInitiatedStream(
-            final H2StreamChannel channel,
-            final HttpProcessor httpProcessor,
-            final BasicHttpConnectionMetrics connMetrics,
-            final HandlerFactory<AsyncPushConsumer> pushHandlerFactory) throws IOException {
+    H2StreamHandler incomingRequest(final H2StreamChannel channel) {
         final HttpCoreContext context = HttpCoreContext.create();
         context.setSSLSession(getSSLSession());
         context.setEndpointDetails(getEndpointDetails());
-        return new ServerH2StreamHandler(channel, httpProcessor, connMetrics, exchangeHandlerFactory, context);
+        return new ServerH2StreamHandler(channel, getHttpProcessor(), getConnMetrics(), exchangeHandlerFactory, context);
     }
 
     @Override
-    H2StreamHandler createLocallyInitiatedStream(
-            final ExecutableCommand command,
+    H2StreamHandler outgoingRequest(
             final H2StreamChannel channel,
-            final HttpProcessor httpProcessor,
-            final BasicHttpConnectionMetrics connMetrics) throws IOException {
-        throw new H2ConnectionException(H2Error.INTERNAL_ERROR, "Illegal attempt to execute a request");
+            final AsyncClientExchangeHandler exchangeHandler,
+            final HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
+            final HttpContext context) throws IOException {
+        throw new H2ConnectionException(H2Error.INTERNAL_ERROR, "Illegal attempt to send a request");
+    }
+
+    @Override
+    H2StreamHandler incomingPushPromise(final H2StreamChannel channel,
+                                        final HandlerFactory<AsyncPushConsumer> pushHandlerFactory) throws IOException {
+        throw new H2ConnectionException(H2Error.PROTOCOL_ERROR, "Illegal incoming push promise");
+    }
+
+    @Override
+    H2StreamHandler outgoingPushPromise(final H2StreamChannel channel,
+                                        final AsyncPushProducer pushProducer) throws IOException {
+        final HttpCoreContext context = HttpCoreContext.create();
+        context.setSSLSession(getSSLSession());
+        context.setEndpointDetails(getEndpointDetails());
+        return new ServerPushH2StreamHandler(channel, getHttpProcessor(), getConnMetrics(), pushProducer, context);
     }
 
     @Override
diff --git a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestAbstractH2StreamMultiplexer.java b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestAbstractH2StreamMultiplexer.java
index 7c7208b48..4025ae28e 100644
--- a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestAbstractH2StreamMultiplexer.java
+++ b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestAbstractH2StreamMultiplexer.java
@@ -27,7 +27,6 @@
 
 package org.apache.hc.core5.http2.impl.nio;
 
-import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -37,12 +36,13 @@
 import org.apache.hc.core5.function.Supplier;
 import org.apache.hc.core5.http.Header;
 import org.apache.hc.core5.http.config.CharCodingConfig;
-import org.apache.hc.core5.http.impl.BasicHttpConnectionMetrics;
 import org.apache.hc.core5.http.impl.CharCodingSupport;
 import org.apache.hc.core5.http.message.BasicHeader;
+import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
 import org.apache.hc.core5.http.nio.AsyncPushConsumer;
+import org.apache.hc.core5.http.nio.AsyncPushProducer;
 import org.apache.hc.core5.http.nio.HandlerFactory;
-import org.apache.hc.core5.http.nio.command.ExecutableCommand;
+import org.apache.hc.core5.http.protocol.HttpContext;
 import org.apache.hc.core5.http.protocol.HttpProcessor;
 import org.apache.hc.core5.http2.H2ConnectionException;
 import org.apache.hc.core5.http2.H2Error;
@@ -141,20 +141,27 @@ void acceptPushFrame() throws H2ConnectionException {
         }
 
         @Override
-        H2StreamHandler createRemotelyInitiatedStream(
-                final H2StreamChannel channel,
-                final HttpProcessor httpProcessor,
-                final BasicHttpConnectionMetrics connMetrics,
-                final HandlerFactory<AsyncPushConsumer> pushHandlerFactory) throws IOException {
+        H2StreamHandler incomingRequest(final H2StreamChannel channel) {
             return streamHandlerSupplier.get();
         }
 
         @Override
-        H2StreamHandler createLocallyInitiatedStream(
-                final ExecutableCommand command,
-                final H2StreamChannel channel,
-                final HttpProcessor httpProcessor,
-                final BasicHttpConnectionMetrics connMetrics) throws IOException {
+        H2StreamHandler outgoingRequest(final H2StreamChannel channel,
+                                        final AsyncClientExchangeHandler exchangeHandler,
+                                        final HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
+                                        final HttpContext context) {
+            return null;
+        }
+
+        @Override
+        H2StreamHandler incomingPushPromise(final H2StreamChannel channel,
+                                            final HandlerFactory<AsyncPushConsumer> pushHandlerFactory) {
+            return streamHandlerSupplier.get();
+        }
+
+        @Override
+        H2StreamHandler outgoingPushPromise(final H2StreamChannel channel,
+                                            final AsyncPushProducer pushProducer) {
             return null;
         }
 
@@ -684,9 +691,8 @@ void testStreamRemoteReset() throws Exception {
                 h2StreamListener,
                 () -> streamHandler);
 
-        final H2StreamChannel channel = streamMultiplexer.createChannel(1, false);
-        final H2Stream stream = new H2Stream(channel, streamHandler);
-        streamMultiplexer.addStream(stream);
+        final H2StreamChannel channel = streamMultiplexer.createChannel(1);
+        final H2Stream stream = streamMultiplexer.createStream(channel, streamHandler);
 
         final ByteArrayBuffer buf = new ByteArrayBuffer(19);
         final HPackEncoder encoder = new HPackEncoder(H2Config.INIT.getHeaderTableSize(), CharCodingSupport.createEncoder(CharCodingConfig.DEFAULT));
@@ -731,9 +737,8 @@ void testStreamRemoteResetNoErrorRemoteAlreadyClosed() throws Exception {
                 h2StreamListener,
                 () -> streamHandler);
 
-        final H2StreamChannel channel = streamMultiplexer.createChannel(1, false);
-        final H2Stream stream = new H2Stream(channel, streamHandler);
-        streamMultiplexer.addStream(stream);
+        final H2StreamChannel channel = streamMultiplexer.createChannel(1);
+        final H2Stream stream = streamMultiplexer.createStream(channel, streamHandler);
 
         final ByteArrayBuffer buf = new ByteArrayBuffer(19);
         final HPackEncoder encoder = new HPackEncoder(H2Config.INIT.getHeaderTableSize(), CharCodingSupport.createEncoder(CharCodingConfig.DEFAULT));