Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit 552a098

Browse files
author
Yuri Nesterenko
committed
8238270: java.net HTTP/2 client does not decrease stream count when receives 204 response
The HTTP/2 Stream is updated to register a trivial data subscriber in case of 204 so that the END_STREAM is correctly processed. Backport-of: da1abd1
1 parent 43e72d2 commit 552a098

File tree

4 files changed

+342
-6
lines changed

4 files changed

+342
-6
lines changed

src/java.net.http/share/classes/jdk/internal/net/http/ExchangeImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -166,8 +166,8 @@ HttpClient client() {
166166

167167
// Called for 204 response - when no body is permitted
168168
void nullBody(HttpResponse<T> resp, Throwable t) {
169-
// only needed for HTTP/1.1 to close the connection
170-
// or return it to the pool
169+
// Needed for HTTP/1.1 to close the connection or return it to the pool
170+
// Needed for HTTP/2 to subscribe a dummy subscriber and close the stream
171171
}
172172

173173
/* The following methods have separate HTTP/1.1 and HTTP/2 implementations */

src/java.net.http/share/classes/jdk/internal/net/http/Stream.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -259,6 +259,14 @@ private void drainInputQueue() {
259259
}
260260
}
261261

262+
@Override
263+
void nullBody(HttpResponse<T> resp, Throwable t) {
264+
if (debug.on()) debug.log("nullBody: streamid=%d", streamid);
265+
// We should have an END_STREAM data frame waiting in the inputQ.
266+
// We need a subscriber to force the scheduler to process it.
267+
pendingResponseSubscriber = HttpResponse.BodySubscribers.replacing(null);
268+
sched.runOrSchedule();
269+
}
262270

263271
// Callback invoked after the Response BodySubscriber has consumed the
264272
// buffers contained in a DataFrame.
@@ -382,6 +390,7 @@ void incoming(Http2Frame frame) throws IOException {
382390
Log.logTrace("handling response (streamid={0})", streamid);
383391
handleResponse();
384392
if (hframe.getFlag(HeaderFrame.END_STREAM)) {
393+
if (debug.on()) debug.log("handling END_STREAM: %d", streamid);
385394
receiveDataFrame(new DataFrame(streamid, DataFrame.END_STREAM, List.of()));
386395
}
387396
}
@@ -688,6 +697,7 @@ HttpHeaders getRequestPseudoHeaders() {
688697

689698
/** Sets endStreamReceived. Should be called only once. */
690699
void setEndStreamReceived() {
700+
if (debug.on()) debug.log("setEndStreamReceived: streamid=%d", streamid);
691701
assert remotelyClosed == false: "Unexpected endStream already set";
692702
remotelyClosed = true;
693703
responseReceived();
@@ -1032,14 +1042,24 @@ void completeResponse(Response resp) {
10321042
synchronized void requestSent() {
10331043
requestSent = true;
10341044
if (responseReceived) {
1045+
if (debug.on()) debug.log("requestSent: streamid=%d", streamid);
10351046
close();
1047+
} else {
1048+
if (debug.on()) {
1049+
debug.log("requestSent: streamid=%d but response not received", streamid);
1050+
}
10361051
}
10371052
}
10381053

10391054
synchronized void responseReceived() {
10401055
responseReceived = true;
10411056
if (requestSent) {
1057+
if (debug.on()) debug.log("responseReceived: streamid=%d", streamid);
10421058
close();
1059+
} else {
1060+
if (debug.on()) {
1061+
debug.log("responseReceived: streamid=%d but request not sent", streamid);
1062+
}
10431063
}
10441064
}
10451065

@@ -1162,6 +1182,7 @@ void close() {
11621182
if (closed) return;
11631183
closed = true;
11641184
}
1185+
if (debug.on()) debug.log("close stream %d", streamid);
11651186
Log.logTrace("Closing stream {0}", streamid);
11661187
connection.closeStream(streamid);
11671188
Log.logTrace("Stream {0} closed", streamid);

0 commit comments

Comments
 (0)