Close BufferedWriter in upload - #1360
Open
tejas-ae wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR aims to prevent a resource leak during RunKeeper uploads by ensuring the request BufferedWriter is always closed, even on error paths.
Changes:
- Wrap the upload request body writer in a try-with-resources to guarantee cleanup.
- Refactor the upload flow around the writer lifecycle (write request body, then read response).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+465
to
488
| try (BufferedWriter w = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()))) { | ||
| rk.export(mID, w); | ||
| w.flush(); | ||
|
|
||
| int responseCode = conn.getResponseCode(); | ||
| String amsg = conn.getResponseMessage(); | ||
| String externalId = noNullStr(conn.getHeaderField("Location")); | ||
| conn.disconnect(); | ||
| conn = null; | ||
|
|
||
| if (responseCode >= HttpURLConnection.HTTP_OK | ||
| && responseCode < HttpURLConnection.HTTP_MULT_CHOICE) { | ||
| s.activityId = mID; | ||
| if (!TextUtils.isEmpty(externalId)) { | ||
| s.externalId = externalId; | ||
| s.externalIdStatus = ExternalIdStatus.OK; | ||
| } | ||
| return s; | ||
| } | ||
| Log.e(getName(), "Error code: " + responseCode + ", amsg: " + amsg); | ||
| ex = new Exception(amsg); | ||
| } catch (Exception e) { | ||
| ex = e; | ||
| } |
| ex = new Exception(amsg); | ||
| } catch (Exception e) { | ||
| ex = e; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The path through upload looks mostly fine, but The resource opened there can leak if upload exits on an error path. this patch moves the allocation into try-with-resources so cleanup happens on every exit path. Happy to adjust if you’d prefer a different approach.
Happy to revise the approach or close this if it doesn’t fit — you know the codebase far better than I do.