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

Fix incorrectly terminated background task #21254

Merged
merged 2 commits into from
Aug 4, 2023

Conversation

kean
Copy link
Contributor

@kean kean commented Aug 4, 2023

The app currently prints the following error message when the background task expiration handler is called:

2023-08-04 10:50:17.838280-0400 Jetpack[60050:4787549] [BackgroundTask] Background task still not 
ended after expiration handlers were called: <_UIBackgroundTaskInfo: 0x15523b7e0>: taskID = 30, 
taskName = Called by Jetpack, from $s9WordPress0aB11AppDelegateC29applicationDidEnterBackgroundyySo13UIApplicationCFTo, 
creationTime = 401007 (elapsed = 26). This app will likely be terminated by the system. 
Call UIApplication.endBackgroundTask(_:) to avoid this.

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:

  • Put the app in the background
  • Verify that the error messages is no longer called

Regression Notes

  1. Potential unintended areas of impact: background tasks
  2. What I did to test those areas of impact (or what existing automated tests I relied on): manual test
  3. What automated tests I added (or what prevented me from doing so): n/a

PR submission checklist:

  • I have completed the Regression Notes.
  • I have considered adding unit tests for my changes.
  • I have considered adding accessibility improvements for my changes.
  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

UI Changes testing checklist:

  • Portrait and landscape orientations.
  • Light and dark modes.
  • Fonts: Larger, smaller and bold text.
  • High contrast.
  • VoiceOver.
  • Languages with large words or with letters/accents not frequently used in English.
  • Right-to-left languages. (Even if translation isn’t complete, formatting should still respect the right-to-left layout)
  • iPhone and iPad.
  • Multi-tasking: Split view and Slide over. (iPad)

@peril-wordpress-mobile
Copy link

Warnings
⚠️ PR is not assigned to a milestone.

Generated by 🚫 dangerJS

@kean
Copy link
Contributor Author

kean commented Aug 4, 2023

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
Copy link
Contributor

@salimbraksa salimbraksa Aug 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@wpmobilebot
Copy link
Contributor

wpmobilebot commented Aug 4, 2023

WordPress Alpha📲 You can test the changes from this Pull Request in WordPress Alpha by scanning the QR code below to install the corresponding build.
App NameWordPress Alpha WordPress Alpha
ConfigurationRelease-Alpha
Build Numberpr21254-bc73fc4
Version22.9
Bundle IDorg.wordpress.alpha
Commitbc73fc4
App Center BuildWPiOS - One-Offs #6617
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.

@wpmobilebot
Copy link
Contributor

wpmobilebot commented Aug 4, 2023

Jetpack Alpha📲 You can test the changes from this Pull Request in Jetpack Alpha by scanning the QR code below to install the corresponding build.
App NameJetpack Alpha Jetpack Alpha
ConfigurationRelease-Alpha
Build Numberpr21254-bc73fc4
Version22.9
Bundle IDcom.jetpack.alpha
Commitbc73fc4
App Center Buildjetpack-installable-builds #5652
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.

Copy link
Contributor

@salimbraksa salimbraksa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@kean kean enabled auto-merge August 4, 2023 17:14
@kean kean merged commit 0e4e761 into trunk Aug 4, 2023
@kean kean deleted the fix/potential-reason-for-watchdog-terminations branch August 4, 2023 17:41
@kean kean added this to the 23.0 milestone Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants