okhttp icon indicating copy to clipboard operation
okhttp copied to clipboard

IOExceptions can cause infinite retries over HTTP/2

Open dlew opened this issue 9 years ago • 9 comments

Stick the following test into HttpOverHttp2Test (which I did since it's hard to get tests to do HTTP2 otherwise):

@Test
public void errorInterceptorDoesNotInfinitelyRetry() throws Exception {
  OkHttpClient client = this.client.newBuilder()
      .addNetworkInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
          throw new IOException("Ouch");
        }
      })
      .build();

  Call call = client.newCall(new Request.Builder()
      .url(server.url("/foo"))
      .build());

  call.execute();
}

The test will never finish execution due to infinite retries.

The infinite retry happens in RetryAndFollowUpInterceptor. It constantly thinks it's got a recoverable error so it never quits.

As far as I can tell, the issue lies in StreamAllocation.streamFailed(). Without HTTP2 it sets route = null, which means that when you retry the request, you eventually run out of routes to try. Once StreamAllocation.hasMoreRoutes() returns false, the request is done.

However, with HTTP2 this never gets set to null, thus it always thinks it has more routes. As a result, an IOException can cause infinite retries.

dlew avatar Apr 04 '17 20:04 dlew

I’d like to somehow differentiate exceptions thrown by OkHttp from exceptions thrown by interceptors and use that to enable the retry.

swankjesse avatar Apr 05 '17 12:04 swankjesse

I face the same problem now,404 also cause IOException which will cause infinite retries, i don't think it should retry infinitely in this situation. This bug only occurred when protocol is http2.

dreamtalee avatar Jun 13 '17 14:06 dreamtalee

Hello, I think I have a potential solution to this issue. Would it be best to discuss here or should I just submit a pull request? Thanks in advance.

I am thinking of making the following change in okhttp/src/main/java/okhttp3/internal/http/RetryAndFollowupInterceptor.java

- while ( true ) {
+ while ( followupCount++ < MAX_FOLLOW_UPS ) {

mdsiebler avatar Sep 25 '17 18:09 mdsiebler

@mdsiebler +1 to PR even if discussion continues here, would help show which other tests break (if any)

yschimke avatar Sep 25 '17 18:09 yschimke

Is my problem is some reason? I use interceptor to retry http 2.0 request ,and server find more than 100 request at the same time ?

WoodJim avatar Oct 11 '17 09:10 WoodJim

@swankjesse @yschimke we met this issue too. Has it been fixed? Or any suggested temporary walk around?

NormanOu avatar Oct 31 '17 04:10 NormanOu

Will implement with this: https://github.com/square/okhttp/issues/3268

swankjesse avatar Feb 18 '18 20:02 swankjesse

@swankjesse The issue linked in your last comment for this fix to be implemented with is the same issue number as this issue. Is there a different issue/PR resolving this one?

cassgenerator avatar Feb 18 '18 21:02 cassgenerator

Whoops! Will implement with this: https://github.com/square/okhttp/issues/3261

swankjesse avatar Feb 19 '18 02:02 swankjesse