watchOS 3 app gets seemingly randomly killed in Background

280 Views Asked by At

I upgraded my watchOS app to support the new watchOS 3 background modes. Most of the time that works totally fine. But more often that I like it gets killed by watchOS with a report like the following:

Incident Identifier: XXX
CrashReporter Key:   xxxx
Hardware Model:      Watch1,1
Process:             AppName App Extension [271]
Identifier:          com.example.app.appextension
Version:             171 (1.13)
Code Type:           ARM (Native)
Role:                Foreground
Parent Process:      launchd [1]
Coalition:           <none> [0]


Date/Time:           2016-09-16 11:09:47.5463 +0200
Launch Time:         2016-09-16 11:05:19.6587 +0200
OS Version:          Watch OS 3.0 (14S326)
Report Version:      104

Exception Type:  00000020
Exception Codes: 0x000000008badf00d
Exception Note:  SIMULATED (this is NOT a crash)
Highlighted by Thread:  0

Application Specific Information:
<BKNewProcess: 0x14540580; com.example.app.appextension; pid: 271; hostpid: 104> has active assertions beyond permitted time: 
{(
    <BKProcessAssertion: 0x14550e20> id: 104-85D4A371-50C7-46D1-8674-8F664FB17511 name: CUP-DCE601C5-C370-4098-AAD5-76CB68EA95E3-com.example.app.appextension process: <BKNewProcess: 0x14540580; com.example.app.appextension; pid: 271; hostpid: 104> permittedBackgroundDuration: 30.000000 reason: complicationUpdate owner pid:104 preventSuspend  preventThrottleDownCPU  preventIdleSleep  wantsForegroundResourcePriority  preventSuspendOnSleep 
)}

Elapsed total CPU time (seconds): 2.040 (user 2.040, system 0.000), 7% CPU 
Elapsed application CPU time (seconds): 0.002, 0% CPU

It often happens approx. 30 seconds after my last command runs (according to my logs), but sometimes also earlier, but almost always way after my tasks are finished. I also do finish the tasks with task.setTaskCompleted() correctly (or at least I think so).

It seems something is preventing for the app to be reported as "I'm finished", but I can't figure out what. Anyone any idea what it could be? I'd be happy to provide more info, but I don't know where to start, since the problem is not really reproducible. Just that happens too often.

If it's important, I use NSURLDownloadSessionTask with a background session to download data, but that also seems to run and finish fine most of the time (except network errors, of course, but there's no correlation between that and the killing of the app)

Edit:

After lots of debugging, I seem to know more, but still don't have a conclusion

What happens is that my app asks for new data with a NSURLDownloadSessionTask Background session when it becomes active. It also asks for the current location. All this can take quite some time (up to 20 seconds in all). So, when I leave now the app, this tasks may still run in the background. Seems to work also fine, since the background time I can use is approx. 30 seconds.

But when I close the app before those tasks are finished and then a WKSnapshotRefreshBackgroundTask is coming in, I wait until the tasks are finished and send the .setTaskCompleted(). But the app seems to never suspend itself in that case. Neither willDisappear nor didDeactivate of the ViewController are called in this case and the app gets killed after 30 seconds with the above report.

When just a regular WKSnapshotRefreshBackgroundTask comes in while nothing is running, all works fine and willDisappear is executed as soon as I call .setTaskCompleted

I have no idea, why in the first case willDisappear is not called. It looks like the app is waiting for something to be finished, but there's nothing, afaik. And I also don't update the interface with those tasks, just update the Model.

0

There are 0 best solutions below