Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions app/src/org/commcare/connect/network/ApiPersonalId.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,9 @@ public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<Respo
} else {
// Handle validation errors
logNetworkError(response);
if (response.errorBody() != null){
callback.processFailure(response.code(), response.errorBody().byteStream());
}
callback.processFailure(response.code(), null);
InputStream stream = response.errorBody() != null ?
response.errorBody().byteStream() : null;
callback.processFailure(response.code(), stream);
}
Comment on lines +236 to 239
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Resource leak: errorBody() stream is never closed

On failure you pass response.errorBody().byteStream() to processFailure without closing the ResponseBody.
Unlike the success branch, this path leaks the socket/okio resources.

Refactor to mirror the success handling:

-InputStream stream = response.errorBody() != null ?
-        response.errorBody().byteStream() : null;
-callback.processFailure(response.code(), stream);
+try (InputStream stream = response.errorBody() != null
+        ? response.errorBody().byteStream()
+        : null) {
+    callback.processFailure(response.code(), stream);
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
InputStream stream = response.errorBody() != null ?
response.errorBody().byteStream() : null;
callback.processFailure(response.code(), stream);
}
// … previous lines …
try (InputStream stream = response.errorBody() != null
? response.errorBody().byteStream()
: null) {
callback.processFailure(response.code(), stream);
}
// … following lines …
🤖 Prompt for AI Agents
In app/src/org/commcare/connect/network/ApiPersonalId.java around lines 236 to
239, the InputStream obtained from response.errorBody().byteStream() is passed
to callback.processFailure without closing the ResponseBody, causing a resource
leak. Refactor this block to obtain the ResponseBody once, use a
try-with-resources or ensure the ResponseBody is closed after processing the
stream, similar to the success branch, to properly release the socket and okio
resources.

}

Expand Down
Loading