Skip to content
This repository was archived by the owner on Jul 25, 2024. It is now read-only.

Commit 59ab30a

Browse files
koziodigitalincniftynei
authored andcommitted
Race condition prevention and attempting to catch thread violations for aborting requests.
1 parent d5f2ffc commit 59ab30a

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

app/src/main/java/com/zulip/android/networking/HTTPRequest.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class HTTPRequest {
3333
private OkHttpClient okHttpClient;
3434
private Response response = null;
3535
private String method, path;
36+
private Object synchronization = new Object();
3637

3738
public void setMethodAndUrl(String method, String URL) {
3839
this.method = method;
@@ -56,14 +57,22 @@ void clearProperties() {
5657

5758
void abort() {
5859
aborting = true;
59-
if (response != null) {
60-
(new AsyncTask<Void, Void, Void>() {
61-
@Override
62-
protected Void doInBackground(Void... voids) {
63-
response.body().close();
64-
return null;
65-
}
66-
}).execute();
60+
synchronized (synchronization) {
61+
if (response != null) {
62+
final Response finalResponse = response;
63+
response = null;
64+
(new AsyncTask<Void, Void, Void>() {
65+
@Override
66+
protected Void doInBackground(Void... voids) {
67+
try {
68+
finalResponse.body().close();
69+
} catch (IllegalStateException e) {
70+
//fail silently
71+
}
72+
return null;
73+
}
74+
}).execute();
75+
}
6776
}
6877
}
6978

0 commit comments

Comments
 (0)