Permalink
Browse files

fix notification task timeout crashing

Reviewed By: hedgerwang

Differential Revision: D4088570

fbshipit-source-id: e2a217564d9325815e396daafbef2b7f06e84b33
  • Loading branch information...
1 parent 285786a commit 3580de541db600d39d4182b59a536c69477e46d3 Emily Janzer committed with Facebook Github Bot Oct 28, 2016
@@ -93,12 +93,12 @@ public synchronized int startTask(final HeadlessJsTaskConfig taskConfig) {
" while in foreground, but this is not allowed.");
}
final int taskId = mLastTaskId.incrementAndGet();
+ mActiveTasks.add(taskId);
reactContext.getJSModule(AppRegistry.class)
.startHeadlessTask(taskId, taskConfig.getTaskKey(), taskConfig.getData());
if (taskConfig.getTimeout() > 0) {
scheduleTaskTimeout(taskId, taskConfig.getTimeout());
}
- mActiveTasks.add(taskId);
for (HeadlessJsTaskEventListener listener : mHeadlessJsTaskEventListeners) {
listener.onHeadlessJsTaskStart(taskId);
}
@@ -107,7 +107,7 @@ public synchronized int startTask(final HeadlessJsTaskConfig taskConfig) {
/**
* Finish a JS task. Doesn't actually stop the task on the JS side, only removes it from the list
- * of active tasks and notifies listeners.
+ * of active tasks and notifies listeners. A task can only be finished once.
*
* @param taskId the unique id returned by {@link #startTask}.
*/
@@ -130,6 +130,14 @@ public void run() {
});
}
+ /**
+ * Check if a given task is currently running. A task is stopped if either {@link #finishTask} is
+ * called or it times out.
+ */
+ public synchronized boolean isTaskRunning(final int taskId) {
+ return mActiveTasks.contains(taskId);
+ }
+
private void scheduleTaskTimeout(final int taskId, long timeout) {
Runnable runnable = new Runnable() {
@Override
@@ -9,6 +9,7 @@
package com.facebook.react.modules.core;
+import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
@@ -37,6 +38,13 @@ public String getName() {
public void notifyTaskFinished(int taskId) {
HeadlessJsTaskContext headlessJsTaskContext =
HeadlessJsTaskContext.getInstance(getReactApplicationContext());
- headlessJsTaskContext.finishTask(taskId);
+ if (headlessJsTaskContext.isTaskRunning(taskId)) {
+ headlessJsTaskContext.finishTask(taskId);
+ } else {
+ FLog.w(
+ HeadlessJsTaskSupportModule.class,
+ "Tried to finish non-active task with id %d. Did it time out?",
+ taskId);
+ }
}
}

0 comments on commit 3580de5

Please sign in to comment.