Skip to content

Commit 4e6699e

Browse files
authored
Merge pull request #5789 from square/jwilson.0216.give_up
Don't do infinite retries on server-canceled calls
2 parents 1c03efd + 134ac98 commit 4e6699e

3 files changed

Lines changed: 20 additions & 8 deletions

File tree

okhttp/src/main/java/okhttp3/internal/connection/Exchange.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class Exchange(
164164

165165
private fun trackFailure(e: IOException) {
166166
finder.trackFailure(e)
167-
codec.connection.trackFailure(call.client, e)
167+
codec.connection.trackFailure(call, e)
168168
}
169169

170170
fun <E : IOException?> bodyComplete(

okhttp/src/main/java/okhttp3/internal/connection/RealConnection.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -683,13 +683,13 @@ class RealConnection(
683683
* Track a failure using this connection. This may prevent both the connection and its route from
684684
* being used for future exchanges.
685685
*/
686-
internal fun trackFailure(client: OkHttpClient, e: IOException?) {
686+
internal fun trackFailure(call: RealCall, e: IOException?) {
687687
connectionPool.assertThreadDoesntHoldLock()
688688

689689
synchronized(connectionPool) {
690690
if (e is StreamResetException) {
691-
when (e.errorCode) {
692-
ErrorCode.REFUSED_STREAM -> {
691+
when {
692+
e.errorCode == ErrorCode.REFUSED_STREAM -> {
693693
// Stop using this connection on the 2nd REFUSED_STREAM error.
694694
refusedStreamCount++
695695
if (refusedStreamCount > 1) {
@@ -698,8 +698,8 @@ class RealConnection(
698698
}
699699
}
700700

701-
ErrorCode.CANCEL -> {
702-
// Permit any number of CANCEL errors on each connection.
701+
e.errorCode == ErrorCode.CANCEL && call.isCanceled() -> {
702+
// Permit any number of CANCEL errors on locally-canceled calls.
703703
}
704704

705705
else -> {
@@ -714,7 +714,7 @@ class RealConnection(
714714
// If this route hasn't completed a call, avoid it for new connections.
715715
if (successCount == 0) {
716716
if (e != null) {
717-
connectFailed(client, route, e)
717+
connectFailed(call.client, route, e)
718718
}
719719
routeFailureCount++
720720
}

okhttp/src/test/java/okhttp3/internal/http2/HttpOverHttp2Test.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,9 +881,17 @@ public void noRecoveryFromTwoRefusedStreams() throws Exception {
881881
}
882882

883883
@Test public void recoverFromOneInternalErrorRequiresNewConnection() throws Exception {
884+
recoverFromOneHttp2ErrorRequiresNewConnection(ErrorCode.INTERNAL_ERROR);
885+
}
886+
887+
@Test public void recoverFromOneCancelRequiresNewConnection() throws Exception {
888+
recoverFromOneHttp2ErrorRequiresNewConnection(ErrorCode.CANCEL);
889+
}
890+
891+
private void recoverFromOneHttp2ErrorRequiresNewConnection(ErrorCode errorCode) throws Exception {
884892
server.enqueue(new MockResponse()
885893
.setSocketPolicy(SocketPolicy.RESET_STREAM_AT_START)
886-
.setHttp2ErrorCode(ErrorCode.INTERNAL_ERROR.getHttpCode()));
894+
.setHttp2ErrorCode(errorCode.getHttpCode()));
887895
server.enqueue(new MockResponse()
888896
.setBody("abc"));
889897

@@ -1064,6 +1072,10 @@ private void callAndCancel(int expectedSequenceNumber, CountDownLatch responseDe
10641072
noRecoveryFromErrorWithRetryDisabled(ErrorCode.INTERNAL_ERROR);
10651073
}
10661074

1075+
@Test public void noRecoveryFromCancelWithRetryDisabled() throws Exception {
1076+
noRecoveryFromErrorWithRetryDisabled(ErrorCode.CANCEL);
1077+
}
1078+
10671079
private void noRecoveryFromErrorWithRetryDisabled(ErrorCode errorCode) throws Exception {
10681080
server.enqueue(new MockResponse()
10691081
.setSocketPolicy(SocketPolicy.RESET_STREAM_AT_START)

0 commit comments

Comments
 (0)