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

Why Task Stop Working When App Terminate In Android #451

Closed
xts-bit opened this issue May 27, 2023 · 31 comments
Closed

Why Task Stop Working When App Terminate In Android #451

xts-bit opened this issue May 27, 2023 · 31 comments
Labels

Comments

@xts-bit
Copy link

xts-bit commented May 27, 2023

Your Environment

  • Plugin version: 4.1.10
  • Platform: Android
  • OS version: Android 11
  • Device: Google Pixel 4 & Real Samsung Device (Android 12)
  • React Native version: 0.71.7
  • Plugin config

Expected Behavior

It should upload the file to the cloud when i Terminate the app on Android when i call the uploadHandler function and. the data.message from the API is "successfully" it should call the ImageHandler function and run it in the background

Actual Behavior

It's not uploading files to the cloud and not hitting the API tried this thing many times and also check my server logs

Steps to Reproduce

  1. Call The uploadHandler
  2. Swipe up
  3. Swipe up the app
  4. App is closed

Context


const ImageHandler = async () => {
    return new Promise(async (resolve, reject) => {
        try {
            // Uploading Image to Cloud
            if (data.message === 'done successfully') {
                resolve();
            } else {
                reject(new Error('Something went wrong, please try again'));
            }
        } catch (error) {
            reject(error);
        }
    });
};

export default function App() {

    useEffect(() => {
        BackgroundFetch.configure(
            {
                minimumFetchInterval: 15,
                stopOnTerminate: false,
                startOnBoot: true,
                enableHeadless: true,
                requiredNetworkType: BackgroundFetch.NETWORK_TYPE_ANY
            },
            async (taskId) => {
                console.log('[BackgroundFetch HeadlessTask] start');
                await uploadHandler(taskId);
            },
            (error) => {
                console.log('[BackgroundFetch] ERROR: ', error);
            }
        );
    }, []);

    const uploadHandler = async (_taskId) => {
            try {
               // Upload File Function
               if (data.message === 'successfully') {
              ImageHandler(image)
            }
            } catch (error) {
                console.log(error);
            }
        } else {
            alert('Please wait its processing');
        }
        BackgroundFetch.finish(taskId);
    };


    return (
       // JSX
    )
};

Debug logs

[:180 call()
05-27 22:13:30.698  1289  1340 I PeriodicStatsRunner: PeriodicStatsRunner.call():184 No submit PeriodicStats since input started.
05-27 22:13:34.488   320   320 I android.hardware.vibrator-service.example: Vibrator reporting capabilities
05-27 22:13:34.489   320   320 I android.hardware.vibrator-service.example: Vibrator off
05-27 22:13:34.489   320   320 I android.hardware.vibrator-service.example: Vibrator perform
05-27 22:13:34.490   320 29824 I android.hardware.vibrator-service.example: Starting perform on another thread
05-27 22:13:34.500   494  3453 I system_server: oneway function results will be dropped but finished with status OK and parcel size 4
05-27 22:13:34.500   494  3453 I system_server: oneway function results will be dropped but finished with status OK and parcel size 4
05-27 22:13:34.591   320 29824 I android.hardware.vibrator-service.example: Notifying perform complete
05-27 22:13:34.624   320   320 I android.hardware.vibrator-service.example: Vibrator off
05-27 22:13:34.636   494  2318 I system_server: oneway function results will be dropped but finished with status OK and parcel size 4
05-27 22:13:34.649   668   668 V ShadeControllerImpl: NotificationShadeWindow: com.android.systemui.statusbar.phone.NotificationShadeWindowView{e3a3082 I.E...... ......ID 0,0-1080,2280} canPanelBeCollapsed(): false
05-27 22:13:37.627   494  2318 I system_server: oneway function results will be dropped but finished with status OK and parcel size 4
05-27 22:13:40.580  1134 29731 I AlarmManager: setExactAndAllowWhileIdle [name: GCM_HB_ALARM type: 2 triggerAtMillis: 7076945]
05-27 22:13:40.590  1134 29731 E WakeLock: GCM_HB_ALARM release without a matched acquire!
05-27 22:13:45.637  1380  1410 W hbox:interacto: Reducing the number of considered missed Gc histogram windows from 137 to 100
05-27 22:13:51.110 26292 26333 W earchbox:searc: Profile data size exceeds 500000 bytes. It has 552474 bytes.
^[[A05-27 22:13:54.304 27053 27109 I Finsky  : [1325] ivz.run(33): AIM: AppInfoManager-Perf > Destroying AppInfoManager ...
05-27 22:13:55.671   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup4: Permission denied
05-27 22:13:55.668   211   211 W Binder:211_2: type=1400 audit(0.0:1846): avc: denied { read } for name="wakeup4" dev="sysfs" ino=16464 scontext=u:r:system_suspend:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0
05-27 22:13:55.671   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup2: Permission denied
05-27 22:13:55.668   211   211 W Binder:211_2: type=1400 audit(0.0:1848): avc: denied { read } for name="wakeup0" dev="sysfs" ino=13341 scontext=u:r:system_suspend:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0
05-27 22:13:55.672   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup0: Permission denied
05-27 22:13:55.672   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup5: Permission denied
05-27 22:13:55.673   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup3: Permission denied
05-27 22:13:55.673   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup1: Permission denied
05-27 22:13:56.085   467   467 E netmgr  : qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:network' service: Invalid argument
05-27 22:13:56.085   467   467 E netmgr  : Failed to open QEMU pipe 'qemud:network': Invalid argument
05-27 22:13:57.073   471   471 E wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument
05-27 22:13:57.073   471   471 E wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe
05-27 22:13:58.230 27053 27098 I Finsky  : [1316] ivz.run(33): AIM: AppInfoManager-Perf > Destroying AppInfoManager ...
05-27 22:14:06.600 26292 29828 I earchbox:searc: oneway function results will be dropped but finished with status UNKNOWN_TRANSACTION and parcel size 0
05-27 22:14:10.034  1007  1027 W System  : A resource failed to call release. 
05-27 22:14:10.035  1007  1027 I chatty  : uid=10153(com.google.android.apps.nexuslauncher) FinalizerDaemon identical 1 line
05-27 22:14:10.035  1007  1027 W System  : A resource failed to call release. 
05-27 22:14:11.796   211   211 W Binder:211_2: type=1400 audit(0.0:1852): avc: denied { read } for name="wakeup4" dev="sysfs" ino=16464 scontext=u:r:system_suspend:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0
05-27 22:14:11.796   211   211 W Binder:211_2: type=1400 audit(0.0:1853): avc: denied { read } for name="wakeup2" dev="sysfs" ino=15902 scontext=u:r:system_suspend:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0
05-27 22:14:11.796   211   211 W Binder:211_2: type=1400 audit(0.0:1854): avc: denied { read } for name="wakeup0" dev="sysfs" ino=13341 scontext=u:r:system_suspend:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0
05-27 22:14:11.800   211   211 W Binder:211_2: type=1400 audit(0.0:1855): avc: denied { read } for name="wakeup5" dev="sysfs" ino=16527 scontext=u:r:system_suspend:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0
05-27 22:14:11.801   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup4: Permission denied
05-27 22:14:11.802   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup2: Permission denied
05-27 22:14:11.802   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup0: Permission denied
05-27 22:14:11.803   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup5: Permission denied
05-27 22:14:11.803   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup3: Permission denied
05-27 22:14:11.804   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup1: Permission denied
05-27 22:14:21.188   494  4816 W ActivityManager: Backgrou](url)
@christocracy
Copy link
Member

Where’s your Debug Logs from $ adb logcat?

@xts-bit
Copy link
Author

xts-bit commented May 27, 2023

@christocracy i updated please check again i see there is a error "05-27 22:14:11.802 211 215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup0: Permission denied"

@christocracy
Copy link
Member

None of that references anything from the plug-in. See https://dontkillmyapp.com

@xts-bit
Copy link
Author

xts-bit commented May 27, 2023

@christocracy How to debug it? will it cause this problem in production ?

@christocracy
Copy link
Member

I have no idea what those logs mean. I suggest you modify device settings according to https://dontkillmyapp.com.

@xts-bit
Copy link
Author

xts-bit commented May 27, 2023

@christocracy Also after running the app for 10min it gave this warning " WARN No task registered for key BackgroundFetch" any idea about can be the issue? is it common?

@christocracy
Copy link
Member

I suggest you simplify your code. Comment-out all your “uploading” stuff and simply do a log statement.

them simulate tasks according to the Debugging section in README

@xts-bit
Copy link
Author

xts-bit commented May 27, 2023

@christocracy i tried remove all the upload stuff and just hit a api after 3 seconds it does any hit the api

@christocracy
Copy link
Member

Your api calls don’t concern me.

I’m only interested in seeing your callback function print a message to the logs.

@xts-bit
Copy link
Author

xts-bit commented May 28, 2023

@christocracy it found this error for just console.log("Hello World") in background "05-28 15:32:40.212 3098 7224 E BgTaskExecutorImpl: EXCLUSIVE background task FETCH_CONFIGS_FROM_PHENOTYPE was cancelled.
05-28 15:32:40.212 3098 7224 E BgTaskExecutorImpl: java.util.concurrent.CancellationException: Task was cancelled."

Can i give a github repo of the project?

@christocracy
Copy link
Member

christocracy commented May 28, 2023

Comment-out your entire BackgroundFetch.configure block and replace it with this:

BackgroundFetch.configure({
  minimumFetchInterval: 15,
  stopOnTerminate: false,
  startOnBoot: true,
  enableHeadless: true,
  requiredNetworkType: BackgroundFetch.NETWORK_TYPE_ANY
}, async (taskId:string) => {
  console.log('[BackgroundFetch] taskId', taskId);                    
   BackgroundFetch.finish(taskId);
}, (taskId:string) => {  
  console.log('[Fetch] TIMEOUT taskId:', taskId);
  BackgroundFetch.finish(taskId);
});    

While simulating events according to the README, observe and provide the logcat results here:

$ adb logcat *:S ReactNative:V ReactNativeJS:V TSBackgroundFetch:V

@xts-bit
Copy link
Author

xts-bit commented May 29, 2023

@christocracy i cant see any message related to "Register Task" however i found out a log that says

 05-29 13:04:15.946  7778  7778 D TSBackgroundFetch: ☯️  onPause
05-29 13:04:15.946  7778  7778 D TSBackgroundFetch: ☯️  onStop

@christocracy
Copy link
Member

What exactly are you executing to simulate the task?

@xts-bit
Copy link
Author

xts-bit commented May 29, 2023

@christocracy I am just uploading image to cloud for test purpose I am logging hello world in background

@christocracy
Copy link
Member

I’m not talking about your “upload” code.

what is the adb command you’re using to simulate a fetch event. As documented in the readme.

@xts-bit
Copy link
Author

xts-bit commented May 29, 2023

@christocracy Chris it logs this:

05-29 20:40:26.666 11384 11447 D TSBackgroundFetch: - configure
05-29 20:40:26.666 11384 11447 D TSBackgroundFetch: - start
05-29 20:40:26.669 11384 11384 D TSBackgroundFetch: ☯️  onCreate
05-29 20:40:26.669 11384 11384 D TSBackgroundFetch: ☯️  onStart
05-29 20:40:26.669 11384 11384 D TSBackgroundFetch: ☯️  onResume
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch: - registerTask: react-native-background-fetch (jobId: 999)
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch: {
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "taskId": "react-native-background-fetch",
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "isFetchTask": true,
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "minimumFetchInterval": 15,
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "stopOnTerminate": false,
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "requiredNetworkType": 1,
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "requiresBatteryNotLow": false,
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "requiresCharging": false,
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "requiresDeviceIdle": false,
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "requiresStorageNotLow": false,
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "startOnBoot": true,
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "jobService": "com.transistorsoft.rnbackgroundfetch.HeadlessTask",
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "forceAlarmManager": false,
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "periodic": true,
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "delay": -1
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch: }
05-29 20:40:28.867 11384 11384 D TSBackgroundFetch: ☯️  onPause
05-29 20:40:28.867 11384 11384 D TSBackgroundFetch: ☯️  onStop

@christocracy
Copy link
Member

Are you not aware you can SIMULATE fetch events?

read the README to learn how to simulate events.

IMG_0630

@xts-bit
Copy link
Author

xts-bit commented May 29, 2023

@christocracy yep it logs this "Running job [FORCED]"

@christocracy
Copy link
Member

Print the logs after simulating an event.

@xts-bit
Copy link
Author

xts-bit commented May 29, 2023

@christocracy Can you please tell me what if mean about "Print the logs after simulating an event"

i run this "adb shell cmd jobscheduler run -f <your.application.id> 999"

@christocracy
Copy link
Member

“Print the logs” = adb logcat *:S ReactNative:V ReactNativeJS:V TSBackgroundFetch:V

@xts-bit
Copy link
Author

xts-bit commented May 29, 2023

@christocracy When i run adb logcat '*:S ReactNative:V ReactNativeJS:V TSBackgroundFetch:V' it log these messages related to TSBackgroundFetch:

05-29 21:26:58.960 14251 14251 D TSBackgroundFetch: ☯️  onCreate
05-29 21:26:58.960 14251 14251 D TSBackgroundFetch: ☯️  onStart
05-29 21:26:58.960 14251 14251 D TSBackgroundFetch: ☯️  onResume
05-29 21:26:58.960 14251 14315 D TSBackgroundFetch: - configure
05-29 21:26:58.960 14251 14315 D TSBackgroundFetch: - start
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch: - registerTask: react-native-background-fetch (jobId: 999)
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch: {
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "taskId": "react-native-background-fetch",
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "isFetchTask": true,
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "minimumFetchInterval": 15,
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "stopOnTerminate": false,
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "requiredNetworkType": 1,
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "requiresBatteryNotLow": false,
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "requiresCharging": false,
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "requiresDeviceIdle": false,
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "requiresStorageNotLow": false,
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "startOnBoot": true,
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "jobService": "com.transistorsoft.rnbackgroundfetch.HeadlessTask",
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "forceAlarmManager": false,
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "periodic": true,
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "delay": -1
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch: }

@christocracy
Copy link
Member

i run this "adb shell cmd jobscheduler run -f <your.application.id> 999"

Are you replacing <your.application.id> with your app’s actual application id?

@xts-bit
Copy link
Author

xts-bit commented May 29, 2023

@christocracy maybe i am doing wrong is this correct?

james@Jamess-MacBook-Air FoodApp % adb shell cmd jobscheduler run -f com.foodapp  999
Running job [FORCED]
james@Jamess-MacBook-Air FoodApp % 

@xts-bit
Copy link
Author

xts-bit commented May 30, 2023

@christocracy Can you give me a simple rn component code snippet that i can copy test and check for any problem with my Android emulator?

@christocracy
Copy link
Member

Can you give me a simple rn component code snippet that i can copy test

That’s exactly what I posted above.

@xts-bit
Copy link
Author

xts-bit commented May 30, 2023

@christocracy I already tried that code can you tell what is a positive log that I can see on console to assure that things are correct

@christocracy
Copy link
Member

See https://dontkillmyapp.com

@christocracy
Copy link
Member

what is a positive log that I can see on console to assure that things are correct

TSBackgroundFetch: - Background Fetch event received: react-native-background-fetch
ReactNativeJS: '[BackgroundFetch]', 'react-native-background-fetch' <------------------- this is a console.log
TSBackgroundFetch: - finish: react-native-background-fetch
TSBackgroundFetch: - jobFinished

Copy link

This issue is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale label Apr 17, 2024
Copy link

github-actions bot commented May 1, 2024

This issue was closed because it has been inactive for 14 days since being marked as stale.

@github-actions github-actions bot closed this as completed May 1, 2024
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

2 participants