Fix incorrectly terminated background task#21254
Conversation
Generated by 🚫 dangerJS |
|
It's unlikely it's the reason for terminations, but better safe than sorry. |
| if let task = self?.bgTask, task != .invalid { | ||
| DDLogInfo("BackgroundTask: executing expirationHandler for bgTask = \(task.rawValue)") | ||
| app.endBackgroundTask(task) | ||
| self?.bgTask = .invalid |
There was a problem hiding this comment.
❓ I agree with the removal of DispatchQueue.main.async, but do you have an idea why it was added in the first place? Maybe it was added to fix some bug. But even in that case, the ideal solution shouldn't be DispatchQueue.main.async.
There was a problem hiding this comment.
I tried to check the history, but this code has been moved around a bit.
Based on the comment ("synchronize on the main thread"), this async is unnecessary because the expiration handler is already called on the main thread:
The system calls the handler synchronously on the main thread, blocking the app’s suspension momentarily.
From https://developer.apple.com/documentation/uikit/uiapplication/1623031-beginbackgroundtaskwithexpiratio
|
| App Name | WordPress Alpha |
|
| Configuration | Release-Alpha | |
| Build Number | pr21254-bc73fc4 | |
| Version | 22.9 | |
| Bundle ID | org.wordpress.alpha | |
| Commit | bc73fc4 | |
| App Center Build | WPiOS - One-Offs #6617 |
|
| App Name | Jetpack Alpha |
|
| Configuration | Release-Alpha | |
| Build Number | pr21254-bc73fc4 | |
| Version | 22.9 | |
| Bundle ID | com.jetpack.alpha | |
| Commit | bc73fc4 | |
| App Center Build | jetpack-installable-builds #5652 |


The app currently prints the following error message when the background task expiration handler is called:
The reason is that the app doesn't end the task immediately on termination. The expiration handler is already called on the main queue, so there is no need to dispatch it.
This may be one of the reasons of WatchdogTermination events.
To test:
Regression Notes
PR submission checklist:
RELEASE-NOTES.txtif necessary.UI Changes testing checklist: