Skip to content

Commit

Permalink
Merge pull request #4492 from amirlivneh/fix-call-cancellation-while-…
Browse files Browse the repository at this point in the history
…sending-request-headers

Fix call cancellation while sending request headers
  • Loading branch information
swankjesse authored Dec 30, 2018
2 parents 0729c20 + 371b9f8 commit e9619ed
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 0 additions & 2 deletions okhttp-tests/src/test/java/okhttp3/CallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2119,8 +2119,6 @@ public void requestHeadersStart(Call call) {
}
}

@Ignore(
"Canceling an HTTP/2 request while request headers are sent has no effect. The request completes successfully and the response can be read.")
@Test
public void cancelWhileRequestHeadersAreSent_HTTP_2() throws Exception {
enableProtocol(Protocol.HTTP_2);
Expand Down
8 changes: 8 additions & 0 deletions okhttp/src/main/java/okhttp3/internal/http2/Http2Codec.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public final class Http2Codec implements HttpCodec {
private final Http2Connection connection;
private Http2Stream stream;
private final Protocol protocol;
private volatile boolean canceled;

public Http2Codec(OkHttpClient client, Interceptor.Chain chain, StreamAllocation streamAllocation,
Http2Connection connection) {
Expand All @@ -114,6 +115,12 @@ public Http2Codec(OkHttpClient client, Interceptor.Chain chain, StreamAllocation
boolean hasRequestBody = request.body() != null || Internal.instance.isDuplex(request);
List<Header> requestHeaders = http2HeadersList(request);
stream = connection.newStream(requestHeaders, hasRequestBody);
// We may have been asked to cancel while creating the new stream and sending the request
// headers, but there was still no stream to close.
if (canceled) {
stream.closeLater(ErrorCode.CANCEL);
throw new IOException("Canceled");
}
stream.readTimeout().timeout(chain.readTimeoutMillis(), TimeUnit.MILLISECONDS);
stream.writeTimeout().timeout(chain.writeTimeoutMillis(), TimeUnit.MILLISECONDS);
}
Expand Down Expand Up @@ -199,6 +206,7 @@ public static Response.Builder readHttp2HeadersList(Headers headerBlock,
}

@Override public void cancel() {
canceled = true;
if (stream != null) stream.closeLater(ErrorCode.CANCEL);
}

Expand Down

0 comments on commit e9619ed

Please sign in to comment.