Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android Crash - Tried to finish non-existent task with id 2 #202

Closed
cristianoccazinsp opened this issue Dec 23, 2019 · 10 comments
Closed

Android Crash - Tried to finish non-existent task with id 2 #202

cristianoccazinsp opened this issue Dec 23, 2019 · 10 comments
Labels

Comments

@cristianoccazinsp
Copy link

cristianoccazinsp commented Dec 23, 2019

Just received a random Android crash from the crash reporting tool. I know the app's only headless task with a timeout value comes from this library, but I haven't been able to reproduce the scenario where such a weird crash would happen. I'm also aware that Samsung devices are a piece of crap and will kill your app as soon as possible, and is most likely the reason for the crash

The crash happened from a headless task that didn't run with the app previously initialized (with an UI / main activity). See crash logs at the end.

After tracing the error, I ended up here: https://github.com/facebook/react-native/blob/0.61-stable/ReactAndroid/src/main/java/com/facebook/react/jstasks/HeadlessJsTaskContext.java#L202 , here https://github.com/facebook/react-native/blob/0.61-stable/ReactAndroid/src/main/java/com/facebook/react/jstasks/HeadlessJsTaskContext.java#L111 and here https://github.com/facebook/react-native/blob/0.61-stable/ReactAndroid/src/main/java/com/facebook/react/jstasks/HeadlessJsTaskContext.java#L165

The above lines hint me that some headless task with a timeout failed to be stopped with the above exception. So far the only headless task with timeout comes from BackgroundFetch.registerHeadlessTask(headlessTask), so I wonder if this crash has been seen already.

Also wonder if this is more of a react native bug itself, looks like a bit aggressive to kill the app just because the task no longer exists. Thoughts?

Your Environment

  • Plugin version: 2.7.1
  • Platform: Android
  • OS version: 9
  • Device manufacturer / model: Samsung SM-G965U1
  • React Native version (react-native -v): 0.61.5
  • Plugin config
 BackgroundFetch.configure({
      minimumFetchInterval: 60,
      stopOnTerminate: false,
      startOnBoot: true, 
      enableHeadless: true
    });

Expected Behavior

No crash if the task times out.

Actual Behavior

Looks like the task timed out and the app crashed when attempting to stop it.

Steps to Reproduce

Run some network request code in the task that might end up timing out.

Context

Background fetch task that executes every 60 minutes, and tries to run in under 20 seconds, stopping itself afterwards to prevent the task from timing out.

Debug logs

java.lang.AssertionError Tried to finish non-existent task with id 2. 
    Assertions.java:72 com.facebook.infer.annotation.Assertions.assertCondition
    HeadlessJsTaskContext.java:164 com.facebook.react.jstasks.HeadlessJsTaskContext.finishTask
    HeadlessJsTaskContext.java:202 com.facebook.react.jstasks.HeadlessJsTaskContext$3.run
    Handler.java:873 android.os.Handler.handleCallback
    Handler.java:99 android.os.Handler.dispatchMessage
    Looper.java:214 android.os.Looper.loop
    ActivityThread.java:6986 android.app.ActivityThread.main
    Method.java:-2 java.lang.reflect.Method.invoke
    RuntimeInit.java:494 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run
    ZygoteInit.java:1445 com.android.internal.os.ZygoteInit.main
@stale
Copy link

stale bot commented Feb 22, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You may also mark this issue as a "discussion" and I will leave this open.

@stale stale bot added the stale label Feb 22, 2020
@stale
Copy link

stale bot commented Feb 29, 2020

Closing this issue after a prolonged period of inactivity. Fell free to reopen this issue, if this still affecting you.

@stale stale bot closed this as completed Feb 29, 2020
@vtoupet
Copy link

vtoupet commented Mar 26, 2021

@cristianoccazinsp did you figure out why this was happening. Any way to handle this exception gracefully?

@cristianoccazinsp
Copy link
Author

No, we pretty much ignored it since it was very rare. However, looks more like a RN bug than this library itself. Although only this library can probably handle the crash gracefully.

@RodolfoGS
Copy link

I have the same issue:

Fatal Exception: java.lang.AssertionError: Tried to finish non-existent task with id 2.
       at com.facebook.infer.annotation.Assertions.assertCondition(Assertions.java:72)
       at com.facebook.react.jstasks.HeadlessJsTaskContext.finishTask(HeadlessJsTaskContext.java:173)
       at com.facebook.react.jstasks.HeadlessJsTaskContext$3.run(HeadlessJsTaskContext.java:211)
       at android.os.Handler.handleCallback(Handler.java:873)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:205)
       at android.app.ActivityThread.main(ActivityThread.java:6991)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:884)

@pedrolopes10
Copy link

@cristianoccazinsp @RodolfoGS did you find a way to fix this issue? I've been receiving more and more crashes about this on Android 12

@cristianoccazinsp
Copy link
Author

Haven't found a fix yet, se see it randomly but not as often as to worry about it. Perhaps @christocracy can comment on this.

@pedrolopes10
Copy link

Thanks @cristianoccazinsp. I also think it's not related to this library as I've done a deploy without it and the issue persisted. I also use react native firebase messaging that uses HeadlessTasks but couldn't reach any conclusion. So any suggestion is welcome!

@tsachit
Copy link

tsachit commented Oct 2, 2023

@RodolfoGS

any update on this fix?

@RodolfoGS
Copy link

@tsachit no updates, I continue having this issue

facebook-github-bot pushed a commit to facebook/react-native that referenced this issue Sep 16, 2024
Summary:
Sometimes a headless task tries to finish, but it doesn’t exist, which causes an exception.
No one knows how to reliably reproduce it, as it could be a race condition. However, if you attempt to remove a task that has already been removed, it shouldn’t cause an issue since you're trying to remove something that’s already gone (which is exactly what you want).

Fixes:
 - #46496
 - #33883
 - #27597
 - transistorsoft/react-native-background-fetch#202
 - transistorsoft/react-native-background-fetch#369
 - transistorsoft/react-native-background-geolocation#2096
 - jpush/jpush-react-native#78

## Stacktrace:
```
Fatal Exception: java.lang.AssertionError: Tried to finish non-existent task with id 28.
  at com.facebook.infer.annotation.Assertions.assertCondition(Assertions.java:88)
  at com.facebook.react.jstasks.HeadlessJsTaskContext.finishTask(HeadlessJsTaskContext.java:179)
  at com.facebook.react.jstasks.HeadlessJsTaskContext$3.run(HeadlessJsTaskContext.java:217)
  at android.os.Handler.handleCallback(Handler.java:958)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loopOnce(Looper.java:257)
  at android.os.Looper.loop(Looper.java:368)
  at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:233)
  at java.lang.Thread.run(Thread.java:1012)
```

## Screenshot

https://github.com/user-attachments/assets/101f0f53-95c9-40ec-a59d-22d6d474b457

## Changelog:

[ANDROID] [FIXED] - Fix Headless Crash `Tried to finish non-existent task with id`

Pull Request resolved: #46497

Test Plan:
I created an example where I attempt to remove a task that doesn’t exist.

Example: https://github.com/RodolfoGS/react-native-fix-non-existent-task

### How to reproduce using the example above:
1. `git clone git@github.com:RodolfoGS/react-native-fix-non-existent-task.git`
2. `cd react-native-fix-non-existent-task`
3. `npm install`
4. `npm run android`
5. Notice the crash

### Steps to create the example from scratch and reproduce the crash:
1. `npx react-native-community/cli@latest init AwesomeProject`
2. `cd AwesomeProject`
3. Add call to finishTask to reproduce the crash (RodolfoGS/react-native-fix-non-existent-task@6fe3c13)
4. `npm run android`
5. Notice the crash

Reviewed By: javache

Differential Revision: D62738059

Pulled By: rshest

fbshipit-source-id: 3232dc76ba8a069279c2b741d62372537a3f9140
cipolleschi pushed a commit to facebook/react-native that referenced this issue Sep 16, 2024
Summary:
Sometimes a headless task tries to finish, but it doesn’t exist, which causes an exception.
No one knows how to reliably reproduce it, as it could be a race condition. However, if you attempt to remove a task that has already been removed, it shouldn’t cause an issue since you're trying to remove something that’s already gone (which is exactly what you want).

Fixes:
 - #46496
 - #33883
 - #27597
 - transistorsoft/react-native-background-fetch#202
 - transistorsoft/react-native-background-fetch#369
 - transistorsoft/react-native-background-geolocation#2096
 - jpush/jpush-react-native#78

## Stacktrace:
```
Fatal Exception: java.lang.AssertionError: Tried to finish non-existent task with id 28.
  at com.facebook.infer.annotation.Assertions.assertCondition(Assertions.java:88)
  at com.facebook.react.jstasks.HeadlessJsTaskContext.finishTask(HeadlessJsTaskContext.java:179)
  at com.facebook.react.jstasks.HeadlessJsTaskContext$3.run(HeadlessJsTaskContext.java:217)
  at android.os.Handler.handleCallback(Handler.java:958)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loopOnce(Looper.java:257)
  at android.os.Looper.loop(Looper.java:368)
  at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:233)
  at java.lang.Thread.run(Thread.java:1012)
```

## Screenshot

https://github.com/user-attachments/assets/101f0f53-95c9-40ec-a59d-22d6d474b457

## Changelog:

[ANDROID] [FIXED] - Fix Headless Crash `Tried to finish non-existent task with id`

Pull Request resolved: #46497

Test Plan:
I created an example where I attempt to remove a task that doesn’t exist.

Example: https://github.com/RodolfoGS/react-native-fix-non-existent-task

### How to reproduce using the example above:
1. `git clone git@github.com:RodolfoGS/react-native-fix-non-existent-task.git`
2. `cd react-native-fix-non-existent-task`
3. `npm install`
4. `npm run android`
5. Notice the crash

### Steps to create the example from scratch and reproduce the crash:
1. `npx react-native-community/cli@latest init AwesomeProject`
2. `cd AwesomeProject`
3. Add call to finishTask to reproduce the crash (RodolfoGS/react-native-fix-non-existent-task@6fe3c13)
4. `npm run android`
5. Notice the crash

Reviewed By: javache

Differential Revision: D62738059

Pulled By: rshest

fbshipit-source-id: 3232dc76ba8a069279c2b741d62372537a3f9140
blakef pushed a commit to facebook/react-native that referenced this issue Sep 30, 2024
Summary:
Sometimes a headless task tries to finish, but it doesn’t exist, which causes an exception.
No one knows how to reliably reproduce it, as it could be a race condition. However, if you attempt to remove a task that has already been removed, it shouldn’t cause an issue since you're trying to remove something that’s already gone (which is exactly what you want).

Fixes:
 - #46496
 - #33883
 - #27597
 - transistorsoft/react-native-background-fetch#202
 - transistorsoft/react-native-background-fetch#369
 - transistorsoft/react-native-background-geolocation#2096
 - jpush/jpush-react-native#78

## Stacktrace:
```
Fatal Exception: java.lang.AssertionError: Tried to finish non-existent task with id 28.
  at com.facebook.infer.annotation.Assertions.assertCondition(Assertions.java:88)
  at com.facebook.react.jstasks.HeadlessJsTaskContext.finishTask(HeadlessJsTaskContext.java:179)
  at com.facebook.react.jstasks.HeadlessJsTaskContext$3.run(HeadlessJsTaskContext.java:217)
  at android.os.Handler.handleCallback(Handler.java:958)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loopOnce(Looper.java:257)
  at android.os.Looper.loop(Looper.java:368)
  at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:233)
  at java.lang.Thread.run(Thread.java:1012)
```

## Screenshot

https://github.com/user-attachments/assets/101f0f53-95c9-40ec-a59d-22d6d474b457

## Changelog:

[ANDROID] [FIXED] - Fix Headless Crash `Tried to finish non-existent task with id`

Pull Request resolved: #46497

Test Plan:
I created an example where I attempt to remove a task that doesn’t exist.

Example: https://github.com/RodolfoGS/react-native-fix-non-existent-task

### How to reproduce using the example above:
1. `git clone git@github.com:RodolfoGS/react-native-fix-non-existent-task.git`
2. `cd react-native-fix-non-existent-task`
3. `npm install`
4. `npm run android`
5. Notice the crash

### Steps to create the example from scratch and reproduce the crash:
1. `npx react-native-community/cli@latest init AwesomeProject`
2. `cd AwesomeProject`
3. Add call to finishTask to reproduce the crash (RodolfoGS/react-native-fix-non-existent-task@6fe3c13)
4. `npm run android`
5. Notice the crash

Reviewed By: javache

Differential Revision: D62738059

Pulled By: rshest

fbshipit-source-id: 3232dc76ba8a069279c2b741d62372537a3f9140
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants