retrofit: cannot make a new request because the previous response is still open: please call response.close()

I am using retrofit2 and tried to hit the call.

But I am getting the error as cannot make a new request because the previous response is still open: please call response.close().

My request is like


serviceRequestTypesResponseCall.enqueue(new RetrofitCallback<ServiceRequestTypesResponse>(retrofit) {
        @Override
        public void onResponse(RetrofitResult<ServiceRequestTypesResponse> result) {

            // get url from response and enqueue nested call
            detailedServiceRequestTypeCall.enqueue(new RetrofitCallback<DetailedServiceRequestType>(retrofit) {
                @Override
                public void onResponse(RetrofitResult<DetailedServiceRequestType> result) {
                    // this is being reached when cancelled
                }

                @Override
                public void onFailure(RetrofitResult<DetailedServiceRequestType> error) {
                    // do something
                }

            });
        }

        @Override
        public void onFailure(RetrofitResult<ServiceRequestTypesResponse> error) {
            // do something
        }
    });

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 3
  • Comments: 15 (2 by maintainers)

Most upvoted comments

I don’t have a solution but an interesting hint to this issue. We’ve got that error as well but it came after a retrofit update.

We updated from 2.5.0 to 2.9.0. With 2.9.0 we have this exception, with 2.5.0 everything works fine. Can you acknowledge that its exception does not occur with this version?

Found a solution from okhttp source itself. It works fine. Users aren’t facing the crash after adding this line before calling chain.proceed again. response.body?.closeQuietly()

https://github.com/square/okhttp/blob/b9267b0ce7a11440ccdd6b3e7d70c4ab9710a144/okhttp/src/jvmMain/kotlin/okhttp3/internal/http/RetryAndFollowUpInterceptor.kt#L101

close the previous response like they have closed it in RetryAndFollowUpInterceptor.

Note: don’t use close(), instead use closeQuietly. the close() method might throw exception. IllegalStateException, other IOException which can be safely ignored.

We are using retrofit2 2.9.0v. We are facing this issue suddenly but not able to reproduce. we found this crash in Crashlytics. should we close the response manually?

Or any other solution for this issue?

Fatal Exception: java.lang.IllegalStateException cannot make a new request because the previous response is still open: please call response.close()

okhttp3.internal.connection.Transmitter.newExchange (Transmitter.java:164) okhttp3.internal.connection.ConnectInterceptor.intercept (ConnectInterceptor.java:41) okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:142) okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:117) okhttp3.internal.cache.CacheInterceptor.intercept (CacheInterceptor.java:94) okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:142) okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:117) okhttp3.internal.http.BridgeInterceptor.intercept (BridgeInterceptor.java:93) okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:142) okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept (RetryAndFollowUpInterceptor.java:88) okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:142) okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:117) ai.haptik.android.sdk.common.OkHttpClientFactory$1.intercept (OkHttpClientFactory.java:101) okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:142) okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:117) ai.haptik.android.sdk.common.a.intercept (a.java:30) okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:142) okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:117) okhttp3.RealCall.getResponseWithInterceptorChain (RealCall.java:229) okhttp3.RealCall$AsyncCall.execute (RealCall.java:172) okhttp3.internal.NamedRunnable.run (NamedRunnable.java:32) java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1167) java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:641) java.lang.Thread.run (Thread.java:923)

After some code iterations, we were able to fix this bug with retrofitVersion = 2.9.0

Hope it helps

override fun intercept(chain: Interceptor.Chain): Response {
        return if (currentToken.isEmpty()) {
            getTokenAndRetry(chain)
        } else {
           /** some code **/

            val isFailed = response.isSuccessful.not()
            if (isFailed) {
                // call was not successful --> current token was deleted -> get token and try again
                response.close() // <---- CLOSE HERE!!
                return getTokenAndRetry(chain) // <-- see next code snippet
            }

            return response
        }
    }

private fun getTokenAndRetry(chain: Interceptor.Chain): Response {

    /** SOME CODE **/

    val refreshResponse = chain.proceedDeletingTokenOnError(refreshTokenRequest)
        return when (refreshResponse.isSuccessful) {
            true -> {
               /** some code in good case **/ 
            }
            false -> {
                // close is error case
                refreshResponse.close() // <-- CLOSE HERE
                chain.proceedDeletingTokenOnError(chain.request())
            }
    }
}

Today got the same error “cannot make a new request because the previous response is still open” in my TokenRefresher class.

People have said that on the latest Retrofit versions we should close initial response with initialResponse.close() method. https://medium.com/@113408/this-is-a-nice-implementation-of-the-authinterceptor-however-on-newer-version-of-okhttp-retrofit-9cf20477e2af

But don’t use the token refresh approach in that article, it’s extremely bad!

I had the same problem while implementing refreshToken as per this article https://medium.com/@emmanuelguther/android-refresh-token-with-multiple-calls-with-retrofit-babe5d1023a1 and update retrofit from 2.5.0 -> 2.9.0

Note that I only get the error when I build in the release environment

No. Just call close, or use use.