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

Don't leak response bodies in executeAsync #8330

Merged
merged 2 commits into from Apr 5, 2024
Merged

Conversation

swankjesse
Copy link
Member

@swankjesse swankjesse commented Apr 5, 2024

Also make callers opt in to an unstable coroutines API. If the resource
cleanup coroutines API changes, we'll have to change this API.

Remove the OkHttp experimental API. This is a good enough API as far
as OkHttp is concerned.

Also make callers opt in to an unstable coroutines API. If the resource
cleanup coroutines API changes, we'll have to change this API.

Remove the OkHttp experimental API. This is a good enough API as far
as OkHttp is concerned.
@@ -42,7 +42,9 @@ suspend fun Call.executeAsync(): Response =
call: Call,
response: Response,
) {
continuation.resume(value = response, onCancellation = { call.cancel() })
continuation.resume(response) {
response.closeQuietly()
Copy link
Member Author

Choose a reason for hiding this comment

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

I am naughty and did both a formatting change and a behavior change here.

Used to be cancel -- asynchronously interrupt a call without releasing any resources
Now it’s close -- synchronously release resources

Copy link
Contributor

Choose a reason for hiding this comment

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

Should Retrofit borrow this improvement in retrofit2/KotlinExtensions.kt? @JakeWharton

Copy link
Member

Choose a reason for hiding this comment

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

Retrofit doesn't have this problem. We never pass a live resource across the coroutine boundary. Our underlying request has already been completely consumed and closed before we resume.

Copy link
Contributor

Choose a reason for hiding this comment

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

Retrofit doesn't have this problem. We never pass a live resource across the coroutine boundary. Our underlying request has already been completely consumed and closed before we resume.

Got it, thanks

import okio.IOException

@OptIn(ExperimentalCoroutinesApi::class)
@ExperimentalOkHttpApi
@ExperimentalCoroutinesApi // resume with a resource cleanup.
Copy link
Member Author

Choose a reason for hiding this comment

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

@swankjesse swankjesse merged commit aef791a into master Apr 5, 2024
20 checks passed
@swankjesse swankjesse deleted the jwilson.0404.dont_leak branch April 5, 2024 11:25
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.

None yet

4 participants