Skip to content

Commit 78697ec

Browse files
committed
Backport 710653ce1856d13161ae1786d7c5f71997536e78
1 parent d215a1f commit 78697ec

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

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

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,45 @@ static final class ConnectionAborter {
150150
private volatile boolean closeRequested;
151151

152152
void connection(HttpConnection connection) {
153-
this.connection = connection;
154-
if (closeRequested) closeConnection();
153+
boolean closeRequested;
154+
synchronized (this) {
155+
// check whether this new connection should be
156+
// closed
157+
closeRequested = this.closeRequested;
158+
if (!closeRequested) {
159+
this.connection = connection;
160+
} else {
161+
// assert this.connection == null
162+
this.closeRequested = false;
163+
}
164+
}
165+
if (closeRequested) closeConnection(connection);
155166
}
156167

157168
void closeConnection() {
158-
closeRequested = true;
159-
HttpConnection connection = this.connection;
160-
this.connection = null;
169+
HttpConnection connection;
170+
synchronized (this) {
171+
connection = this.connection;
172+
if (connection == null) {
173+
closeRequested = true;
174+
} else {
175+
this.connection = null;
176+
}
177+
}
178+
closeConnection(connection);
179+
}
180+
181+
HttpConnection disable() {
182+
HttpConnection connection;
183+
synchronized (this) {
184+
connection = this.connection;
185+
this.connection = null;
186+
this.closeRequested = false;
187+
}
188+
return connection;
189+
}
190+
191+
private static void closeConnection(HttpConnection connection) {
161192
if (connection != null) {
162193
try {
163194
connection.close();
@@ -166,11 +197,6 @@ void closeConnection() {
166197
}
167198
}
168199
}
169-
170-
void disable() {
171-
connection = null;
172-
closeRequested = false;
173-
}
174200
}
175201

176202
// Called for 204 response - when no body is permitted
@@ -614,8 +640,11 @@ HttpResponse.BodySubscriber<T> ignoreBody(HttpResponse.ResponseInfo hdrs) {
614640
client.client2(),
615641
this, e::drainLeftOverBytes)
616642
.thenCompose((Http2Connection c) -> {
643+
HttpConnection connection = connectionAborter.disable();
617644
boolean cached = c.offerConnection();
618-
if (cached) connectionAborter.disable();
645+
if (!cached && connection != null) {
646+
connectionAborter.connection(connection);
647+
}
619648
Stream<T> s = c.getInitialStream();
620649

621650
if (s == null) {

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.io.EOFException;
2929
import java.io.IOException;
3030
import java.io.UncheckedIOException;
31-
import java.net.ConnectException;
3231
import java.net.InetSocketAddress;
3332
import java.net.URI;
3433
import java.util.Base64;
@@ -100,7 +99,7 @@ CompletableFuture<Http2Connection> getConnectionFor(HttpRequestImpl req,
10099
Http2Connection connection = connections.get(key);
101100
if (connection != null) {
102101
try {
103-
if (connection.closed
102+
if (!connection.isOpen()
104103
|| !connection.reserveStream(true, pushEnabled)) {
105104
if (debug.on())
106105
debug.log("removing connection from pool since " +
@@ -156,14 +155,19 @@ CompletableFuture<Http2Connection> getConnectionFor(HttpRequestImpl req,
156155
*/
157156
boolean offerConnection(Http2Connection c) {
158157
if (debug.on()) debug.log("offering to the connection pool: %s", c);
159-
if (c.closed || c.finalStream()) {
158+
if (!c.isOpen() || c.finalStream()) {
160159
if (debug.on())
161160
debug.log("skipping offered closed or closing connection: %s", c);
162161
return false;
163162
}
164163

165164
String key = c.key();
166165
synchronized(this) {
166+
if (!c.isOpen()) {
167+
if (debug.on())
168+
debug.log("skipping offered closed or closing connection: %s", c);
169+
return false;
170+
}
167171
Http2Connection c1 = connections.putIfAbsent(key, c);
168172
if (c1 != null) {
169173
if (c.serverPushEnabled() && !c1.serverPushEnabled()) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,10 @@ private void handleConnectionFrame(Http2Frame frame)
10731073
}
10741074
}
10751075

1076+
boolean isOpen() {
1077+
return !closed && connection.channel().isOpen();
1078+
}
1079+
10761080
void resetStream(int streamid, int code) {
10771081
try {
10781082
if (connection.channel().isOpen()) {

test/jdk/java/net/httpclient/CancelRequestTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8245462 8229822
26+
* @bug 8245462 8229822 8254786
2727
* @summary Tests cancelling the request.
2828
* @library /test/lib /test/jdk/java/net/httpclient/lib
2929
* @key randomness

0 commit comments

Comments
 (0)