IOExceptions can cause infinite retries over HTTP/2
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.
I’d like to somehow differentiate exceptions thrown by OkHttp from exceptions thrown by interceptors and use that to enable the retry.
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.
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 +1 to PR even if discussion continues here, would help show which other tests break (if any)
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 ?
@swankjesse @yschimke we met this issue too. Has it been fixed? Or any suggested temporary walk around?
Will implement with this: https://github.com/square/okhttp/issues/3268
@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?
Whoops! Will implement with this: https://github.com/square/okhttp/issues/3261