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

Fatal Exception: Tried to finish non-existent task with id 1 #369

Closed
RodolfoGS opened this issue Nov 24, 2021 · 8 comments
Closed

Fatal Exception: Tried to finish non-existent task with id 1 #369

RodolfoGS opened this issue Nov 24, 2021 · 8 comments
Labels

Comments

@RodolfoGS
Copy link

I have a lot of crashes on Crashlytics related to headless. Says that can't stop a non-existent task.
I don't know how to reproduce the crash. I'm seeing them on Crashlytics.

My headless task is this:

BackgroundFetch.configure({
  minimumFetchInterval: 15,     // <-- minutes (15 is minimum allowed)
  forceAlarmManager: false,     // <-- Set true to bypass JobScheduler.
  stopOnTerminate: false,
  startOnBoot: true,
  requiredNetworkType: BackgroundFetch.NETWORK_TYPE_NONE, // Default
  requiresCharging: false,      // Default
  requiresDeviceIdle: false,    // Default
  requiresBatteryNotLow: false, // Default
  requiresStorageNotLow: false, // Default
  enableHeadless: true,
}, async (taskId) => {
  // event callback

  // ... my task is here

  BackgroundFetch.finish(taskId);
}, async (taskId) => {
  // task timeout callback
  console.log(`react-native-background-fetch timeout task: ${taskId}`);
  BackgroundFetch.finish(taskId);
});

BackgroundFetch.registerHeadlessTask(async (event) => {
  if (event.timeout) {
    BackgroundFetch.finish(event.taskId);
    return;
  }

  // ... my task is here

  BackgroundFetch.finish(event.taskId);
});

Your Environment

  • Plugin version: 4.0.3
  • Platform: Android
  • OS version: 8, 9, 10, 11
  • Device manufacturer / model: Samsung, Umx, Tinno, LGE, Motorola, Google, etc
  • React Native version (react-native -v): 0.63.4

Expected Behavior

No crash

Actual Behavior

Crash

Steps to Reproduce

I don't know how to reproduce the crash, I have them on Crashlytics

Context

Run a headless task

Debug logs

Fatal Exception: java.lang.AssertionError: Tried to finish non-existent task with id 1.
       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:193)
       at android.app.ActivityThread.main(ActivityThread.java:6746)
       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:858)
@christocracy
Copy link
Member

Show me your entire index.js without redaction.

@RodolfoGS
Copy link
Author

RodolfoGS commented Nov 24, 2021

@christocracy this is my index.js and a helper that I'm using to register the task

index.js

import 'react-native-gesture-handler'; 
import { Platform } from 'react-native';
import { registerRootComponent } from 'expo';
import App from './App';
import BackgroundFetch from './helpers/background-fetch';

if (Platform.OS === 'android') {
  BackgroundFetch.registerHeadless();
}

// registerRootComponent calls AppRegistry.registerComponent('main', () => App);
// It also ensures that whether you load the app in Expo Go or in a native build,
// the environment is set up appropriately
registerRootComponent(App);

helpers/background-fetch.js

import BackgroundFetch from 'react-native-background-fetch';

async function run() {
  // my task
}

function register() {
  BackgroundFetch.configure({
    minimumFetchInterval: 15,     // <-- minutes (15 is minimum allowed)
    forceAlarmManager: false,     // <-- Set true to bypass JobScheduler.
    stopOnTerminate: false,
    startOnBoot: true,
    requiredNetworkType: BackgroundFetch.NETWORK_TYPE_NONE, // Default
    requiresCharging: false,      // Default
    requiresDeviceIdle: false,    // Default
    requiresBatteryNotLow: false, // Default
    requiresStorageNotLow: false, // Default
    enableHeadless: true,
  }, async (taskId) => {
    // event callback
    await run();
    BackgroundFetch.finish(taskId);
  }, async (taskId) => {
    // task timeout callback
    console.log(`react-native-background-fetch timeout task: ${taskId}`);
    BackgroundFetch.finish(taskId);
  });

  // Query the authorization status
  BackgroundFetch.status((status) => {
    switch(status) {
      case BackgroundFetch.STATUS_RESTRICTED:
        console.log("react-native-background-fetch: BackgroundFetch restricted");
        break;
      case BackgroundFetch.STATUS_DENIED:
        console.log("react-native-background-fetch: BackgroundFetch denied");
        break;
      case BackgroundFetch.STATUS_AVAILABLE:
        console.log("react-native-background-fetch: BackgroundFetch is enabled");
        break;
    }
  });
}

function registerHeadless() {
  BackgroundFetch.registerHeadlessTask(async (event) => {
    if (event.timeout) {
      BackgroundFetch.finish(event.taskId);
      return;
    }

    await run();
    BackgroundFetch.finish(event.taskId);
  });
}

export default {
  register,
  registerHeadless,
}

@christocracy
Copy link
Member

This error has been reported by others not using my plugins.

The stacktrace does not reference com.transistorsoft so I don't think my plugin is involved with the error. I don't see how my code could possibly catch that exception. The plugin is not involved with calling this RN finishTask method. This is purely RN plumbing at fault here.

@RodolfoGS
Copy link
Author

RodolfoGS commented Nov 24, 2021

The exception is fired by react-native in this line, when try to finish the task.

Maybe could be fixed adding an if using the method isTaskRunning to verify if the task is running before call finishTask?

Could be a race condition with event.timeout?

@christocracy
Copy link
Member

to verify if the task is running before call finishTask?

My code does not call finishTask. RN calls that upon itself.

@stale
Copy link

stale bot commented Apr 16, 2022

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 Apr 16, 2022
@stale
Copy link

stale bot commented Apr 27, 2022

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 Apr 27, 2022
@Saad-Bashar
Copy link

@RodolfoGS Just trying out my luck here. Apologies for posting a comment here. Did any of you figure out a solution on this? We are using react-native-background-fetch as well and recently getting a lot of crash with this message. The crash mainly happens on Samsung android. I know this library is not responsible for that but hoping to get some hints. Thanks!

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

3 participants