okhttp: okhttp3.internal.http2.StreamResetException: stream was reset: PROTOCOL_ERROR/ CANCEL

I am connecting to web service with last version retrofit but get me bellow error :

okhttp3.internal.http2.StreamResetException: stream was reset: PROTOCOL_ERROR

My code is like bellow :

try {
                    // Create a trust manager that does not validate certificate chains
                    final TrustManager[] trustAllCerts = new TrustManager[]{
                            new X509TrustManager() {
                                @Override
                                public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
                                }

                                @Override
                                public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
                                }

                                @Override
                                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                                    return new java.security.cert.X509Certificate[]{};
                                }
                            }
                    };

                    // Install the all-trusting trust manager
                    final SSLContext sslContext = SSLContext.getInstance("SSL");
                    sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
                    // Create an ssl socket factory with our all-trusting manager
                    final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
                    OkHttpClient.Builder builder = new OkHttpClient.Builder();
                    builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
                    builder.hostnameVerifier(new HostnameVerifier() {
                        @Override
                        public boolean verify(String hostname, SSLSession session) {
                            return true;
                        }
                    });

                    OkHttpClient okHttpClient = builder.
                            build();
                    Retrofit retrofit = new Retrofit.Builder()
                            .baseUrl("https://xxx")
                            .client(okHttpClient)
                            .addConverterFactory(GsonConverterFactory.create())
                            .addConverterFactory(ScalarsConverterFactory.create())
                            .build();
                    final PublicApi request = retrofit.create(PublicApi.class);
                    Call<GetStatusSaveContactListModel> call = request.sendContactLists("saveContactList", obj.toString());
                    call.enqueue(new Callback<GetStatusSaveContactListModel>() {
                        @Override
                        public void onResponse(@NonNull Call<GetStatusSaveContactListModel> call, @NonNull Response<GetStatusSaveContactListModel> response) {
                        }

                        @Override
                        public void onFailure(@NonNull Call<GetStatusSaveContactListModel> call, Throwable t) {
                        }
                    });

                } catch (Exception e) {
                    throw new RuntimeException(e);
                }

And

@POST("/web_service/mobile/rest")
Call<GetStatusSaveContactListModel> sendContactLists(@Query("function") String function,
                                                     @Query("data") String data);

And

implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.3.0'

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 6
  • Comments: 54 (3 by maintainers)

Commits related to this issue

Most upvoted comments

httpClient.protocols( Collections.singletonList(Protocol.HTTP_1_1) ); This what worked for me. https://stackoverflow.com/questions/53648852/how-to-solve-okhttp3-internal-http2-streamresetexception-stream-was-reset-refu

This is happening to me in production, but I am not able to reproduce the problem when debugging

Non-fatal Exception: okhttp3.internal.http2.StreamResetException: stream was reset: CANCEL
       at okhttp3.internal.http2.Http2Stream.takeHeaders(Http2Stream.java:158)
       at okhttp3.internal.http2.Http2Codec.readResponseHeaders(Http2Codec.java:131)
       at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
       at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
       at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
       at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
       at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
       at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
       at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
       at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
       at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
       at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
       at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
       at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
       at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
       at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:254)
       at okhttp3.RealCall.execute(RealCall.java:92)
       at com.google.firebase.perf.network.FirebasePerfOkHttpClient.execute(FirebasePerfOkHttpClient.java:43)
       at retrofit2.OkHttpCall.execute(OkHttpCall.java:186)
       at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:45)
       at io.reactivex.Observable.subscribe(Observable.java:12090)
       at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
       at io.reactivex.Observable.subscribe(Observable.java:12090)
       at io.reactivex.internal.operators.observable.ObservableSingleSingle.subscribeActual(ObservableSingleSingle.java:35)
       at io.reactivex.Single.subscribe(Single.java:3438)
       at io.reactivex.internal.operators.single.SingleToObservable.subscribeActual(SingleToObservable.java:35)
       at io.reactivex.Observable.subscribe(Observable.java:12090)
       at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
       at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:578)
       at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
       at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
       at java.util.concurrent.FutureTask.run(FutureTask.java:266)
       at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
       at java.lang.Thread.run(Thread.java:919)

Have we got any update on this? I get this when i trigger a specific api in quick succession for example: API called API called again (before i could get response) throws StreamResetException

but if i call the API wait for some time i get the response API called . . . waiting . . after some waiting finally i get the response

@shrutigupta23 In theory now, all that should be required to enable framelogging via adb is the following

$ adb shell setprop log.tag.okhttp.Http2 DEBUG
$ adb logcat '*:E' 'okhttp.Http2:D'
...
05-28 19:31:32.365  8472  8505 D okhttp.Http2: >> CONNECTION 505249202a20485454502f322e300d0a0d0a534d0d0a0d0a
05-28 19:31:32.366  8472  8505 D okhttp.Http2: >> 0x00000000     6 SETTINGS
05-28 19:31:32.368  8472  8505 D okhttp.Http2: >> 0x00000000     4 WINDOW_UPDATE
05-28 19:31:32.374  8472  8509 D okhttp.Http2: << 0x00000000    18 SETTINGS
05-28 19:31:32.374  8472  8509 D okhttp.Http2: << 0x00000000     4 WINDOW_UPDATE
05-28 19:31:32.379  8472  8505 D okhttp.Http2: >> 0x00000003    45 HEADERS       END_STREAM|END_HEADERS
05-28 19:31:32.382  8472  8510 D okhttp.Http2: >> 0x00000000     0 SETTINGS      ACK
05-28 19:31:32.389  8472  8509 D okhttp.Http2: << 0x00000000     0 SETTINGS      ACK
05-28 19:31:32.408  8472  8509 D okhttp.Http2: << 0x00000003   373 HEADERS       END_HEADERS
05-28 19:31:32.412  8472  8509 D okhttp.Http2: << 0x00000003   230 DATA          END_STREAM
05-28 19:31:32.412  8472  8509 D okhttp.Http2: << 0x00000000     8 PING
05-28 19:31:32.413  8472  8511 D okhttp.Http2: >> 0x00000000     8 PING          ACK
05-28 19:31:32.523  8472  8505 D okhttp.Http2: >> CONNECTION 505249202a20485454502f322e300d0a0d0a534d0d0a0d0a
05-28 19:31:32.524  8472  8505 D okhttp.Http2: >> 0x00000000     6 SETTINGS
05-28 19:31:32.525  8472  8505 D okhttp.Http2: >> 0x00000000     4 WINDOW_UPDATE
05-28 19:31:32.529  8472  8505 D okhttp.Http2: >> 0x00000003    49 HEADERS       END_STREAM|END_HEADERS
05-28 19:31:32.556  8472  8512 D okhttp.Http2: << 0x00000000    18 SETTINGS
05-28 19:31:32.556  8472  8512 D okhttp.Http2: << 0x00000000     4 WINDOW_UPDATE
05-28 19:31:32.557  8472  8512 D okhttp.Http2: << 0x00000000     0 SETTINGS      ACK
05-28 19:31:32.557  8472  8511 D okhttp.Http2: >> 0x00000000     0 SETTINGS      ACK
05-28 19:31:32.564  8472  8512 D okhttp.Http2: << 0x00000003   375 HEADERS       END_HEADERS
05-28 19:31:32.568  8472  8512 D okhttp.Http2: << 0x00000003  1841 DATA          PADDED
05-28 19:31:32.568  8472  8512 D okhttp.Http2: << 0x00000003    15 DATA          END_STREAM|PADDED
05-28 19:31:32.569  8472  8512 D okhttp.Http2: << 0x00000000     8 PING
05-28 19:31:32.569  8472  8510 D okhttp.Http2: >> 0x00000000     8 PING          ACK

we are having the same issue.

2021-09-17 16:04:26.979  W/System.err: okhttp3.internal.http2.StreamResetException: stream was reset: PROTOCOL_ERROR
2021-09-17 16:04:26.979  W/System.err:     at okhttp3.internal.http2.Http2Stream$FramingSource.read(Http2Stream.java:381)
2021-09-17 16:04:26.979  W/System.err:     at okhttp3.internal.http2.Http2Codec$StreamFinishingSource.read(Http2Codec.java:213)
2021-09-17 16:04:26.979  W/System.err:     at okio.RealBufferedSource$1.read(RealBufferedSource.java:447)
2021-09-17 16:04:26.979  W/System.err:     at java.io.InputStream.read(InputStream.java:101)
2021-09-17 16:04:26.979  W/System.err:     at com.bumptech.glide.load.model.ComicStreamEncoder.encode(ComicStreamEncoder.kt:46)
2021-09-17 16:04:26.979  W/System.err:     at com.bumptech.glide.load.model.ComicStreamEncoder.encode(ComicStreamEncoder.kt:21)
2021-09-17 16:04:26.979  W/System.err:     at com.bumptech.glide.load.engine.DataCacheWriter.write(DataCacheWriter.java:31)
2021-09-17 16:04:26.979  W/System.err:     at com.bumptech.glide.load.engine.cache.DiskLruCacheWrapper.put(DiskLruCacheWrapper.java:134)
2021-09-17 16:04:26.979  W/System.err:     at com.bumptech.glide.load.engine.SourceGenerator.cacheData(SourceGenerator.java:79)
2021-09-17 16:04:26.980  W/System.err:     at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:46)
2021-09-17 16:04:26.980  W/System.err:     at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:302)
2021-09-17 16:04:26.980  W/System.err:     at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:272)
2021-09-17 16:04:26.980  W/System.err:     at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:233)
2021-09-17 16:04:26.980  W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2021-09-17 16:04:26.980  W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2021-09-17 16:04:26.980  W/System.err:     at java.lang.Thread.run(Thread.java:929)

I’m guessing you mean something that’s described here: https://www.twilio.com/blog/2017/10/http2-issues.html?

@mlazowik Yep - I’m assuming your case may possibly be the one I mentioned above “Or head of line blocking causing a bunch of later requests to be delayed while HTTP/1.1 may allow small requests to succeed.”

You can check you are not calling cancel() too early.

IIRC, I had that issue because I made a mistake in an extension where I was calling cancel after a successful response, which would make reading it fail with this exception.

@LouisCAD Thanks, but I’m not using cacel() in my code, it happens always when there is bad connection.