OkHttp throws IllegalStateException: previous response is still open
java.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.
Fix it fast
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/java2Match what you see, then jump to its fix
- 1
The 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
The 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
A leak warning, or slowness after many requests, even when no interceptor retriesA response in your own code is never closedFix
- 1
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/java2Fix
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:testDebugUnitTestConfirmed by raw.githubusercontent.com
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/java2Fix
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:testDebugUnitTestConfirmed by raw.githubusercontent.com
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/java2Fix
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:testDebugUnitTestConfirmed by raw.githubusercontent.com
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:
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- Nuclear Home VideoA video-streaming Android app built from scratch, engineered for smooth playback.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
Related fixes
Mobile apps
AGP 9 marks the android block as deprecated or fails on BaseExtension
java.lang.ClassCastException: class com.android.build.gradle.internal.dsl.ApplicationExtensionImpl$AgpDecorated_Decorated cannot be cast to class com.android.build.gradle.BaseExtension
AGP 9.0 uses only the new public DSL interfaces.
3 causes, each with a fixMobile apps
The editor cannot find R, so every R.string and R.id turns red
Unresolved reference: R
R is a class that the build generates from your res folder, in the package named by the module's namespace.
4 causes, each with a fixMobile apps
Android Studio says it could not identify the launch activity when you press Run
Run > Edit Configurations > Launch Options is set to Nothing or to a class that no longer exists
Android Studio launches the activity marked with a MAIN action and a LAUNCHER category in the merged manifest.
3 causes, each with a fixMobile apps
FirebaseInstanceIdService and FirebaseInstanceId.getToken() no longer work
Deprecation warning on FirebaseInstanceId.getInstance().getToken()
Move token handling into a class that extends FirebaseMessagingService and override onNewToken, which runs when a token is first created and whenever it changes.
2 causes, each with a fixMobile apps
Java and Kotlin compile to different JVM targets in an Android module
Inconsistent JVM-target compatibility detected for tasks 'compileDebugJavaWithJavac' (1.8) and 'kaptGenerateStubsDebugKotlin' (17)
Java and Kotlin tasks in the same module are compiling for different JVM targets, for example 1.8 and 17.
2 causes, each with a fixMobile apps
Manifest.permission.POST_NOTIFICATIONS shows red or does not resolve
Cannot resolve symbol 'POST_NOTIFICATIONS'
POST_NOTIFICATIONS exists only from API level 33.
4 causes, each with a fix