Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

OkHttp throws IllegalStateException: previous response is still open

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

Short answer

OkHttp throws this when one call sends a second request while the first response body is still open. It happens in an interceptor that calls chain.proceed() more than once. Close the first response before proceeding again, or move token refresh to an Authenticator, which OkHttp built for 401 replies.

  • Applies to OkHttp 4.x and 5.x, Retrofit 2.x, Kotlin, Android Studio current stable
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the stack trace to find which interceptor is on it, then list every interceptor you add to the client. The failing one calls chain.proceed() twice.

    grep -rnE "addInterceptor|addNetworkInterceptor|chain.proceed|authenticator" app/src/main/java
  2. 2Match what you see, then jump to its fix

    1. 1The stack trace passes through your own Interceptor.intercept, and the code calls chain.proceed(request) again after checking the first responseA retry interceptor proceeds a second time without closing the first responseFix
    2. 2The interceptor sees a 401, fetches a new token and proceeds again with the first response still openToken refresh on 401 is done inside an interceptorFix
    3. 3A leak warning, or slowness after many requests, even when no interceptor retriesA response in your own code is never closedFix

What does the error look like?

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

Cause 1 of 3

A retry interceptor proceeds a second time without closing the first response

1Check

OkHttp's source throws this check when the call's previous response body is still open. Its interceptor guide says application interceptors may retry by calling chain.proceed() more than once, and that previous response bodies must then be closed.

grep -rn -B2 -A8 "chain.proceed" app/src/main/java

2Fix

Close the first response before the second proceed. In Kotlin, response.close() is enough, because closing a response closes its body.

class RetryInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        var response = chain.proceed(chain.request())
        if (response.code == 503) {
            response.close()
            response = chain.proceed(chain.request())
        }
        return response
    }
}

3Verify

Trigger the failing case again. The retry goes through and no IllegalStateException is thrown.

./gradlew :app:testDebugUnitTest

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

Token refresh on 401 is done inside an interceptor

1Check

Look for an interceptor that inspects response.code == 401. OkHttp has a dedicated hook for this: its Authenticator documentation says that when an origin server answers 401, the implementation should respond with a new request carrying the Authorization header.

grep -rn "401" app/src/main/java

2Fix

Move the refresh into an Authenticator on the client. OkHttp calls it for the 401 and sends the request you return, so you never run a second proceed by hand.

val client = OkHttpClient.Builder()
    .authenticator(Authenticator { _, response ->
        val token = refreshToken() // your own code
        response.request.newBuilder()
            .header("Authorization", "Bearer $token")
            .build()
    })
    .build()

3Verify

Call an endpoint with an expired token. The call succeeds after one refresh.

./gradlew :app:testDebugUnitTest

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

A response in your own code is never closed

1Check

OkHttp's ResponseBody documentation says failing to close the body leaks resources and may slow or crash the application, and that the body can be read only once. Look for response variables in error branches and early returns.

grep -rnE "\.execute\(\)|\.body\(\)|\.body\b" app/src/main/java

2Fix

Use the response in a use block so it closes on every path, or read the body with string() inside the block.

client.newCall(request).execute().use { response ->
    val text = response.body.string()
    // handle text
}

3Verify

Run the flow repeatedly. No leak warnings appear and the call time stays flat.

./gradlew :app:testDebugUnitTest

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Treat every extra chain.proceed() as a reason to close the previous response first.
  • Use an Authenticator for 401 token refresh rather than an interceptor.
  • Wrap manual execute() calls in use { } so each path closes the response.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.