Skip to content

Commit a56e845

Browse files
fix(http): better handling of active requests and shutting down gracefully
Co-authored-by: jcesarmobile <[email protected]>
1 parent af97904 commit a56e845

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

android/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorHttp.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
)
2525
public class CapacitorHttp extends Plugin {
2626

27-
private Map<Runnable, PluginCall> activeRequests = new HashMap<>();
27+
private final Map<Runnable, PluginCall> activeRequests = new HashMap<>();
2828
private final ExecutorService executor = Executors.newCachedThreadPool();
2929

3030
@Override
@@ -59,17 +59,26 @@ protected void handleOnDestroy() {
5959
}
6060

6161
private void http(final PluginCall call, final String httpMethod) {
62-
Runnable asyncHttpCall = () -> {
63-
try {
64-
JSObject response = HttpRequestHandler.request(call, httpMethod, getBridge());
65-
call.resolve(response);
66-
} catch (Exception e) {
67-
call.reject(e.getLocalizedMessage(), e.getClass().getSimpleName(), e);
62+
Runnable asyncHttpCall = new Runnable() {
63+
@Override
64+
public void run() {
65+
try {
66+
JSObject response = HttpRequestHandler.request(call, httpMethod, getBridge());
67+
call.resolve(response);
68+
} catch (Exception e) {
69+
call.reject(e.getLocalizedMessage(), e.getClass().getSimpleName(), e);
70+
} finally {
71+
activeRequests.remove(this);
72+
}
6873
}
6974
};
7075

71-
activeRequests.put(asyncHttpCall, call);
72-
executor.submit(asyncHttpCall);
76+
if (!executor.isShutdown()) {
77+
activeRequests.put(asyncHttpCall, call);
78+
executor.submit(asyncHttpCall);
79+
} else {
80+
call.reject("Failed to execute request - Http Plugin was shutdown");
81+
}
7382
}
7483

7584
@JavascriptInterface

0 commit comments

Comments
 (0)