Skip to content

Commit a202086

Browse files
committed
Bug-fix: corrected abnormal termination of H2 streams
1 parent faf44e8 commit a202086

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import org.apache.hc.core5.http.ProtocolException;
5858
import org.apache.hc.core5.http.ProtocolVersion;
5959
import org.apache.hc.core5.http.RequestNotExecutedException;
60+
import org.apache.hc.core5.http.StreamClosedException;
6061
import org.apache.hc.core5.http.config.CharCodingConfig;
6162
import org.apache.hc.core5.http.impl.BasicEndpointDetails;
6263
import org.apache.hc.core5.http.impl.BasicHttpConnectionMetrics;
@@ -528,7 +529,11 @@ public final void onOutput() throws HttpException, IOException {
528529
if (connState.compareTo(ConnectionHandshake.SHUTDOWN) >= 0) {
529530
if (!streamMap.isEmpty()) {
530531
for (final H2Stream stream : streamMap.values()) {
531-
stream.releaseResources();
532+
if (stream.isLocalClosed() && stream.isRemoteClosed()) {
533+
stream.releaseResources();
534+
} else {
535+
stream.fail(new ConnectionClosedException());
536+
}
532537
}
533538
streamMap.clear();
534539
}
@@ -575,7 +580,11 @@ public final void onDisconnect() {
575580
for (final Iterator<Map.Entry<Integer, H2Stream>> it = streamMap.entrySet().iterator(); it.hasNext(); ) {
576581
final Map.Entry<Integer, H2Stream> entry = it.next();
577582
final H2Stream stream = entry.getValue();
578-
stream.cancel();
583+
if (stream.isLocalClosed() && stream.isRemoteClosed()) {
584+
stream.releaseResources();
585+
} else {
586+
stream.fail(new ConnectionClosedException());
587+
}
579588
}
580589
CommandSupport.cancelCommands(ioSession);
581590
}
@@ -592,7 +601,11 @@ private void processPendingCommands() throws IOException, HttpException {
592601
for (final Iterator<Map.Entry<Integer, H2Stream>> it = streamMap.entrySet().iterator(); it.hasNext(); ) {
593602
final Map.Entry<Integer, H2Stream> entry = it.next();
594603
final H2Stream stream = entry.getValue();
595-
stream.cancel();
604+
if (stream.isLocalClosed() && stream.isRemoteClosed()) {
605+
stream.releaseResources();
606+
} else {
607+
stream.fail(new ConnectionClosedException());
608+
}
596609
}
597610
streamMap.clear();
598611
connState = ConnectionHandshake.SHUTDOWN;
@@ -982,7 +995,7 @@ private void consumeFrame(final RawFrame frame) throws HttpException, IOExceptio
982995
final int activeStreamId = entry.getKey();
983996
if (!idGenerator.isSameSide(activeStreamId) && activeStreamId > processedLocalStreamId) {
984997
final H2Stream stream = entry.getValue();
985-
stream.cancel();
998+
stream.fail(new RequestNotExecutedException());
986999
it.remove();
9871000
}
9881001
}
@@ -1702,14 +1715,10 @@ HandlerFactory<AsyncPushConsumer> getPushHandlerFactory() {
17021715
return handler.getPushHandlerFactory();
17031716
}
17041717

1705-
void cancel() {
1706-
fail(new RequestNotExecutedException());
1707-
}
1708-
17091718
boolean abort() {
17101719
final boolean cancelled = channel.cancel();
17111720
if (released.compareAndSet(false, true)) {
1712-
handler.failed(new RequestNotExecutedException());
1721+
handler.failed(new StreamClosedException());
17131722
handler.releaseResources();
17141723
}
17151724
return cancelled;

0 commit comments

Comments
 (0)