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

Close okhttp response in case of error #13246

Closed
wants to merge 1 commit into from

Conversation

AsamK
Copy link
Contributor

@AsamK AsamK commented Nov 9, 2023

Contributor checklist

  • OnePlus 8T, Android 13.1
  • My contribution is fully baked and ready to be merged as is
  • I ensure that all the open issues my contribution fixes are mentioned in the commit message of my first commit using the Fixes #1234 syntax

Description

In the makeServiceRequest method, that returns a Response, the response body is not closed if there is an error.
If an exception is thrown in the responseCodeHandler or in validateServiceResponse the response is not returned, and it needs to be closed directly.

WARN  OkHttpClient - A connection to https://chat.staging.signal.org/ was leaked. Did you forget to close a response body?
java.lang.Throwable: response.body().close()
        at okhttp3.internal.platform.Platform.getStackTraceForCloseable(Platform.kt:145)
        at okhttp3.internal.connection.RealCall.callStart(RealCall.kt:170)
        at okhttp3.internal.connection.RealCall.execute(RealCall.kt:151)
        at org.whispersystems.signalservice.internal.push.PushServiceSocket.getServiceConnection(PushServiceSocket.java:2054)
        at org.whispersystems.signalservice.internal.push.PushServiceSocket.makeServiceRequest(PushServiceSocket.java:1965)
        at org.whispersystems.signalservice.internal.push.PushServiceSocket.makeServiceRequest(PushServiceSocket.java:1904)
        at org.whispersystems.signalservice.internal.push.PushServiceSocket.makeServiceRequest(PushServiceSocket.java:1898)
        at org.whispersystems.signalservice.internal.push.PushServiceSocket.setAccountAttributes(PushServiceSocket.java:493)
        at org.whispersystems.signalservice.api.SignalServiceAccountManager.setAccountAttributes(SignalServiceAccountManager.java:327)

In the makeServiceRequest method, that returns a Response, the response body
is not closed if there is an error.
If an exception is thrown in the responseCodeHandler or in validateServiceResponse
the response is not returned, and it needs to be closed directly.
@cody-signal
Copy link
Contributor

Are you able to do this with a try-with-resource instead?

@AsamK
Copy link
Contributor Author

AsamK commented Nov 9, 2023

No, I don't think so.
In the success case, the response is still required by the callers of this method, try-with-resource would close the Response in the success and error case.
The callers of this method already use a try-with-resource to close the response, but this only works in the success case ... in the error case this method is responsible for closing the response.

@AsamK AsamK deleted the close_okhttp_response branch November 11, 2023 07:00
TwoLeaves pushed a commit to TwoLeaves/Signal-antisocial that referenced this pull request Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants