Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly handle async cancellation of call context in OkHttp engine #1063

Merged
merged 5 commits into from
Apr 17, 2024

Conversation

ianbotsf
Copy link
Contributor

@ianbotsf ianbotsf commented Apr 8, 2024

Issue #

Resolves #1061

Description of changes

Update OkHttp engine call context cancellation to first cancel the call before attempting to close any response body.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@ianbotsf ianbotsf requested a review from aajtodd April 8, 2024 22:07
@ianbotsf ianbotsf requested a review from a team as a code owner April 8, 2024 22:07
@@ -51,6 +51,7 @@ public class OkHttpEngine(
val engineResponse = mapOkHttpExceptions { engineCall.executeAsync() }

callContext.job.invokeOnCompletion {
engineCall.cancel()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correctness: I'm not sure we want to trigger a cancellation on every invocation including successful ones. This may trigger other behaviors/paths in okhttp (including lifecycle methods on interceptors).

What we probably want is to know if there was a response body and if it was consumed or not. If not then cancel and close otherwise we don't need to do anything.

Alternatively we have the right hooks in place on HttpCall. We could add an internal open fun cleanup() { } that we override to invoke engineResponse.body.close() on the same thread that the execution is happening on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that makes sense. Let me try modifying HttpCall first and see how that works. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline and decided to try more intelligent invokeOnCompletion logic instead.

val response = engineResponse.toSdkResponse()
val requestTime = Instant.fromEpochMilliseconds(engineResponse.sentRequestAtMillis)
val responseTime = Instant.fromEpochMilliseconds(engineResponse.receivedResponseAtMillis)

return OkHttpCall(request, response, requestTime, responseTime, callContext, engineCall)
return OkHttpCall(request, response, requestTime, responseTime, callContext, engineCall).also { call ->
callContext.job.invokeOnCompletion { error ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit(naming): error -> cause to match documentation?

return OkHttpCall(request, response, requestTime, responseTime, callContext, engineCall)
return OkHttpCall(request, response, requestTime, responseTime, callContext, engineCall).also { call ->
callContext.job.invokeOnCompletion { error ->
if (error != null) call.cancelInFlight()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correctness: you can receive a non-null CancellationException which is not meant to be treated as an error. do we still want to cancel the call in this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we definitely want to cancel when we get a CancellationException. We're bridging two contexts here so we want to ensure resources belonging to the engine are cleaned up when the call context is cancelled. I'll leave a comment to that effect for future readers.

coroutineScope {
val parentScope = this

println("Invoking call on ctx $coroutineContext")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove println

@ianbotsf ianbotsf merged commit 28a28f5 into main Apr 17, 2024
13 checks passed
@ianbotsf ianbotsf deleted the fix-okhttp-cancellation-timing branch April 17, 2024 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IllegalStateException "Unbalanced enter/exit" in okio AsyncTimeout
3 participants