Skip to content

Close BufferedWriter in upload - #1360

Open
tejas-ae wants to merge 1 commit into
jonasoreland:masterfrom
tejas-ae:fix/close-bufferedwriter-in-upload
Open

Close BufferedWriter in upload#1360
tejas-ae wants to merge 1 commit into
jonasoreland:masterfrom
tejas-ae:fix/close-bufferedwriter-in-upload

Conversation

@tejas-ae

@tejas-ae tejas-ae commented Jun 6, 2026

Copy link
Copy Markdown

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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;
}
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.

2 participants