From bdb1d4377e47c6cd49ff619134d4860519a3cb0c Mon Sep 17 00:00:00 2001 From: Tim Wang Date: Wed, 1 May 2019 02:17:11 -0700 Subject: [PATCH] Do not throw on empty registered headless task (#24671) Summary: Start a `HeadlessJsTaskService` on Android without registered is dangerous on apps because `HeadlessJsTaskService` will acquire a [`PARTIAL_WAKE_LOCK`](https://developer.android.com/topic/performance/vitals/wakelock), without calling `onHeadlessJsTaskFinish` this lock won't release until timeout(if exist). This lock will prevent the android device from sleeping. Although on JS will throw an error if no headless tasks registered, but it's hard to notice while app in the background. No visual information is displayed. This PR will log a warning instead of Error, and just mark the task to finished on native if nothing registered in order to release the wake lock. [Android] [Fixed] - Fix unexpected PARTIAL_WAKE_LOCK when no headless tasks registered. Pull Request resolved: https://github.com/facebook/react-native/pull/24671 Differential Revision: D15164310 Pulled By: cpojer fbshipit-source-id: 05b62017ba094d0faabc2848dc8bb6c26101321b --- Libraries/ReactNative/AppRegistry.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Libraries/ReactNative/AppRegistry.js b/Libraries/ReactNative/AppRegistry.js index 5e10af2f6..e4b846863 100644 --- a/Libraries/ReactNative/AppRegistry.js +++ b/Libraries/ReactNative/AppRegistry.js @@ -260,7 +260,9 @@ const AppRegistry = { startHeadlessTask(taskId: number, taskKey: string, data: any): void { const taskProvider = taskProviders.get(taskKey); if (!taskProvider) { - throw new Error(`No task registered for key ${taskKey}`); + console.warn(`No task registered for key ${taskKey}`); + NativeModules.HeadlessJsTaskSupport.notifyTaskFinished(taskId); + return; } taskProvider()(data) .then(() =>