Closed lauzadis closed 12 months ago
Trying to use both engineCall.cancel()
and engineResponse.body.close()
results in the same issue. The coroutine which is blocked on read()
needs some time to unblock itself and attach the job.
callContext.job.invokeOnCompletion {
engineCall.cancel()
// without this delay, the same `IllegalStateException` is thrown. note: in practice, this delay will be more than 1 nanosecond
runBlocking { delay(1.nanoseconds) }
engineResponse.body.close()
}
This is caused by a blocked
read()
call inSdkSource.toSdkByteReadChannel()
.The call is blocked on
read()
(waiting for a new event stream update) while the HTTP call is finishing. This causes two threads to touch the same OkHttp Call which is forbidden.The problem is solved by replacing engineResponse.body.close() with engineCall.cancel() as suggested by OkHttp devs, but according to the documentation for
.cancel()
, this may leak resources: