Skip to content

Commit dcac630

Browse files
authored
Fixing issue #669 - RejectedExecutionException from idsAvailable (#936)
* Added try-catch to manually run the task a `RejectedExecutionException` from the already shutdown `pendingTaskExecutor` * No need to redesign or over complicated this fix since we will be moving to Kotlina nd there might be better patterns for managing the task thread queue
1 parent aff3fd0 commit dcac630

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,6 @@
2727

2828
package com.onesignal;
2929

30-
import java.io.PrintWriter;
31-
import java.io.StringWriter;
32-
import java.util.ArrayList;
33-
import java.util.Calendar;
34-
import java.util.Collection;
35-
import java.util.Date;
36-
import java.util.HashMap;
37-
import java.util.HashSet;
38-
import java.util.Iterator;
39-
import java.util.Map;
40-
import java.util.TimeZone;
41-
import java.util.concurrent.ConcurrentLinkedQueue;
42-
import java.util.concurrent.ExecutorService;
43-
import java.util.concurrent.Executors;
44-
import java.util.concurrent.ThreadFactory;
45-
import java.util.concurrent.atomic.AtomicLong;
46-
4730
import android.app.Activity;
4831
import android.app.AlertDialog;
4932
import android.app.Application;
@@ -68,6 +51,24 @@
6851
import org.json.JSONException;
6952
import org.json.JSONObject;
7053

54+
import java.io.PrintWriter;
55+
import java.io.StringWriter;
56+
import java.util.ArrayList;
57+
import java.util.Calendar;
58+
import java.util.Collection;
59+
import java.util.Date;
60+
import java.util.HashMap;
61+
import java.util.HashSet;
62+
import java.util.Iterator;
63+
import java.util.Map;
64+
import java.util.TimeZone;
65+
import java.util.concurrent.ConcurrentLinkedQueue;
66+
import java.util.concurrent.ExecutorService;
67+
import java.util.concurrent.Executors;
68+
import java.util.concurrent.RejectedExecutionException;
69+
import java.util.concurrent.ThreadFactory;
70+
import java.util.concurrent.atomic.AtomicLong;
71+
7172
/**
7273
* The main OneSignal class - this is where you will interface with the OneSignal SDK
7374
* <br/><br/>
@@ -847,7 +848,7 @@ public void run() {
847848
}
848849

849850
private static void startPendingTasks() {
850-
if(!taskQueueWaitingForInit.isEmpty()) {
851+
if (!taskQueueWaitingForInit.isEmpty()) {
851852
pendingTaskExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() {
852853
@Override
853854
public Thread newThread(@NonNull Runnable runnable) {
@@ -857,7 +858,7 @@ public Thread newThread(@NonNull Runnable runnable) {
857858
}
858859
});
859860

860-
while(!taskQueueWaitingForInit.isEmpty()) {
861+
while (!taskQueueWaitingForInit.isEmpty()) {
861862
pendingTaskExecutor.submit(taskQueueWaitingForInit.poll());
862863
}
863864
}
@@ -866,29 +867,39 @@ public Thread newThread(@NonNull Runnable runnable) {
866867
private static void addTaskToQueue(PendingTaskRunnable task) {
867868
task.taskId = lastTaskId.incrementAndGet();
868869

869-
if(pendingTaskExecutor == null) {
870+
if (pendingTaskExecutor == null) {
870871
OneSignal.Log(LOG_LEVEL.INFO,"Adding a task to the pending queue with ID: " + task.taskId);
871872
//the tasks haven't been executed yet...add them to the waiting queue
872873
taskQueueWaitingForInit.add(task);
873874
}
874-
else if(!pendingTaskExecutor.isShutdown()) {
875+
else if (!pendingTaskExecutor.isShutdown()) {
875876
OneSignal.Log(LOG_LEVEL.INFO,"Executor is still running, add to the executor with ID: " + task.taskId);
876-
//if the executor isn't done with tasks, submit the task to the executor
877-
pendingTaskExecutor.submit(task);
877+
try {
878+
//if the executor isn't done with tasks, submit the task to the executor
879+
pendingTaskExecutor.submit(task);
880+
} catch (RejectedExecutionException e) {
881+
OneSignal.Log(LOG_LEVEL.INFO,"Executor is shutdown, running task manually with ID: " + task.taskId);
882+
// Run task manually when RejectedExecutionException occurs due to the ThreadPoolExecutor.AbortPolicy
883+
// The pendingTaskExecutor is already shutdown by the time it tries to run the task
884+
// Issue #669
885+
// https://github.com/OneSignal/OneSignal-Android-SDK/issues/669
886+
task.run();
887+
e.printStackTrace();
888+
}
878889
}
879890

880891
}
881892

882893
private static boolean shouldRunTaskThroughQueue() {
883-
if(initDone && pendingTaskExecutor == null) // there never were any waiting tasks
894+
if (initDone && pendingTaskExecutor == null) // there never were any waiting tasks
884895
return false;
885896

886897
//if init isn't finished and the pending executor hasn't been defined yet...
887-
if(!initDone && pendingTaskExecutor == null)
898+
if (!initDone && pendingTaskExecutor == null)
888899
return true;
889900

890901
//or if the pending executor is alive and hasn't been shutdown yet...
891-
if(pendingTaskExecutor != null && !pendingTaskExecutor.isShutdown())
902+
if (pendingTaskExecutor != null && !pendingTaskExecutor.isShutdown())
892903
return true;
893904

894905
return false;

0 commit comments

Comments
 (0)