HeadlessJsTaskService is expected to run on UI thread #15915
Copy link
Copy link
Closed
Labels
Resolution: LockedThis issue was locked by the bot.This issue was locked by the bot.
Description
Is this a bug report?
Yes
Have you read the Contributing Guidelines?
Yes
Environment
react-native -v: react-native-cli: 2.0.1 react-native: 0.48.1node -v: v7.5.0npm -v: 5.3.0
- Target Platform: Android
- Development Operating System: macOS
- Build tools:
Steps to Reproduce
- Received a push notification
- Starting a Service that extends HeadlessJsTaskService.java
- App throws exception
com.facebook.react.bridge.AssertionException: Expected to run on UI thread!
Expected Behavior
I expect no UI thread requirement for a background service (same as Android native SDK)
Actual Behavior
App throws an exception
Reacted by Elad Moshe, haimyyy, Yuval Carmel and Niklas von Hertzen
Metadata
Metadata
Assignees
Labels
Resolution: LockedThis issue was locked by the bot.This issue was locked by the bot.
Activity
chirag04 commentedon Sep 12, 2017
starting a headless task is expected to run on UI thread: https://github.com/facebook/react-native/blob/1e8f3b11027fe0a7514b4fc97d0798d3c64bc895/ReactAndroid/src/main/java/com/facebook/react/HeadlessJsTaskService.java#L89-L96
oriharel commentedon Sep 12, 2017
@chirag04 thanks for the note. However, as I understand, one of the common uses of this is performing tasks triggered by a push notification (or a job schedule). These are clearly not required to run in a UI thread as these are background activities.
cmdshepard commentedon Sep 12, 2017
@oriharel according to the documentation, HeadlessJS is meant for tasks that are needed to be ran when your app in the background. In which case there are no operations on the main thread.
oriharel commentedon Sep 13, 2017
Exactly. This is why I don't understand the "startTask must be triggered from the UI thread" requirement. Unless there is some limitation forcing the React context to be spawned from the UI thread.
D1no commentedon Sep 13, 2017
That means you can not trigger a headless task from another headless task (threads of threads).
oriharel commentedon Sep 13, 2017
In the documentation, it is clearly stated -
Which means, even if a BroadcastReceiver is triggered, say from a push notification, the Headless task should be able to perform.
But this is not the case, because there is an underline requirement that the onReactContextInitialized event handler will be called from the main thread (AKA UI thread). A requirement that most of the time not fulfilled (for example, when a user swiped out the app from the recent apps menu).
So in fact - I DO consider this a bug and updated the form.
haimyyy commentedon Sep 13, 2017
+1
ycarmel commentedon Sep 13, 2017
+1
blankg commentedon Sep 13, 2017
+1
foghina commentedon Sep 13, 2017
I think there's a bit of confusion here. The "UI" thread is simply the "main" thread of an Android application. It's the thread that
Services naturally run on. For various reasons, the React context is always initialized on this thread, from where it spins off two others: JS and modules (IIRC). This is whystartTaskneeds to be run on the UI/main thread, because it might need to initialize the context.foghina commentedon Sep 13, 2017
If you're on a background thread it should be fairly easy to post something to the main one and call
startTaskthere. Is there a reason why that wouldn't work?oriharel commentedon Sep 14, 2017
You're right, everything I said about the UI thread actually reffers to the main thread. The Service "onStartCommand" does indeed happen on that thread. However, inside startTask there is an asynchronic call to create the React context. The callback that is triggered asynchronically is no longer on that thread. And that is already a private area so we can't override this in our inheriting class.
oriharel commentedon Sep 14, 2017
An updated pull request here.
re #15915 HeadlessJsTaskService is expected to run on UI thread
narodejesus commentedon Sep 20, 2017
+1
oriharel commentedon Sep 20, 2017
Issue is now fixed and merged to React Native.
sparsh commentedon Sep 28, 2017
I have used latest version i.e 0.48.4 of react-native, still, I don't see your changes in File HeadlessJsTaskService.java.

oriharel commentedon Sep 28, 2017
I don't think it's released yet
artemukolov commentedon Oct 26, 2017
Hi. I tried to fix this file manually, but i got another error.

My code
oriharel commentedon Oct 26, 2017
Check your AppRegistry in js code. You need to register a js file before you call it from Java
oriharel commentedon Oct 26, 2017
Also check your HeadlessJsTaskService implementation in Java. Make sure you return new HeadlessJsTaskConfig with a name "exactJobKey".
artemukolov commentedon Oct 30, 2017
Thanks. Looks like this issue is fixed in 0.49.5.