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

Prevents the app from crashing when the foreground service fails to start #19987

Merged
merged 1 commit into from
Jan 22, 2024

Conversation

antonis
Copy link
Contributor

@antonis antonis commented Jan 19, 2024

Partially/Temporarily #18714

Description

This PR catches and logs the Runtime exception (ForegroundServiceStartNotAllowedException) thrown at the PostUploadNotifier when the service fails to start on Android 12+

Internal ref: pcdRpT-5hm-p2#comment-8537

Note: This is a temporary solution till we upgrade to WorkManager as part of the Android 14 Target SDK Update work (internal ref: pcdRpT-5fy-p2#comment-8380)


To Test:

  • Try to reproduce the crash by starting an upload of a post/media on an Android 13/14 device and putting the app on the background 🤞
  • Verify that the error is logged.

Regression Notes

  1. Potential unintended areas of impact

    • N/A
  2. What I did to test those areas of impact (or what existing automated tests I relied on)

    • N/A
  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 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.
  • Talkback.
  • 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)
  • Large and small screen sizes. (Tablet and smaller phones)
  • Multi-tasking: Split screen and Pop-up view. (Android 10 or higher)

@antonis antonis changed the title Catches and logs and Runtime exception thrown when the foreground ser… Prevents the app from crashing when the foreground service fails to start Jan 19, 2024
@antonis antonis added this to the 24.1 milestone Jan 19, 2024
@wpmobilebot
Copy link
Contributor

WordPress📲 You can test the changes from this Pull Request in WordPress by scanning the QR code below to install the corresponding build.
App NameWordPress WordPress
FlavorJalapeno
Build TypeDebug
Versionpr19987-861cdb4
Commit861cdb4
Direct Downloadwordpress-prototype-build-pr19987-861cdb4.apk
Note: Google Login is not supported on these builds.

@wpmobilebot
Copy link
Contributor

Jetpack📲 You can test the changes from this Pull Request in Jetpack by scanning the QR code below to install the corresponding build.
App NameJetpack Jetpack
FlavorJalapeno
Build TypeDebug
Versionpr19987-861cdb4
Commit861cdb4
Direct Downloadjetpack-prototype-build-pr19987-861cdb4.apk
Note: Google Login is not supported on these builds.

Copy link

codecov bot commented Jan 19, 2024

Codecov Report

Attention: 4 lines in your changes are missing coverage. Please review.

Comparison is base (e450383) 40.45% compared to head (861cdb4) 40.45%.

Files Patch % Lines
...rdpress/android/ui/uploads/PostUploadNotifier.java 0.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            trunk   #19987      +/-   ##
==========================================
- Coverage   40.45%   40.45%   -0.01%     
==========================================
  Files        1440     1440              
  Lines       66518    66521       +3     
  Branches    10959    10959              
==========================================
  Hits        26910    26910              
- Misses      37121    37124       +3     
  Partials     2487     2487              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@antonis antonis marked this pull request as ready for review January 19, 2024 11:00
@dangermattic
Copy link
Collaborator

1 Warning
⚠️ This PR is assigned to the milestone 24.1. This milestone is due in less than 4 days.
Please make sure to get it merged by then or assign it to a milestone with a later deadline.

Generated by 🚫 Danger

@peril-wordpress-mobile
Copy link

Warnings
⚠️ This PR is assigned to a milestone which is closing in less than 4 days Please, make sure to get it merged by then or assign it to a later expiring milestone

Generated by 🚫 dangerJS

Comment on lines +138 to +142
try {
mService.startForeground(sNotificationData.mNotificationId, mNotificationBuilder.build());
} catch (RuntimeException exception) {
AppLog.e(T.POSTS, "startOrUpdateForegroundNotification failed; See issue #18714", exception);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I won't be able to test this today, but wanted to ask if you were able to consistently reproduce the issue and test this workaround since I am not 100% sure what happens now when we start the services from the background.

Something else I noticed is that we never explicitly call startForegoundService for starting services that are promoted to the foreground, but I believe that's required (we simply call startService everywhere). Not sure if that could also be related to the crashes we see.

Anyway, my point is that I don't know if the app will simply crash at some other point if the service starts but fail to call startForeground. Maybe we might also need to stop the service on the catch clause here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your feedback @thomashorta 🙇

I won't be able to test this today, but wanted to ask if you were able to consistently reproduce the issue and test this workaround since I am not 100% sure what happens now when we start the services from the background.

I was able to reproduce the crash a couple of times and verified that it was user facing (crash popup). I wasn’t able to find specific consistent steps though (maybe due to the many exemptions) :(
Most related crash stack traces seem to go through the specific line.
Screenshot 2024-01-22 at 11 03 28 AM
I’ll have a 2nd look at this and iterate back if I manage to reproduce consistently.

Something else I noticed is that we never explicitly call startForegoundService for starting services that are promoted to the foreground, but I believe that's required (we simply call startService everywhere). Not sure if that could also be related to the crashes we see.

True. I think in all cases we use Context#startService(Intent). I'm not sure if explicitly calling startForegoundService would make a difference tbh.

Anyway, my point is that I don't know if the app will simply crash at some other point if the service starts but fail to call startForeground. Maybe we might also need to stop the service on the catch clause here.

That's indeed a valid concern. My reasoning behind this (temporary) workaround was to catch just the exception thrown from the startForeground assuming that running background service could continue.

Copy link
Contributor

Choose a reason for hiding this comment

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

Most related crash stack traces seem to go through the specific line.
Screenshot 2024-01-22 at 11 03 28 AM I’ll have a 2nd look at this and iterate back if I manage to reproduce consistently.

Yeah, that's indeed the case, so I think that will get rid of the crash we see. My concern is that another crash might pop up anyway at other service-related place.

True. I think in all cases we use Context#startService(Intent). I'm not sure if explicitly calling startForegoundService would make a difference tbh.

I agree! I'm not entirely sure what startForegroundService does TBH, I think it might just set some flag in the system saying that we will call startForeground soon and if we don't it throws an Exception, but maybe just using startService works as well 🤷🏼‍♂️

That's indeed a valid concern. My reasoning behind this (temporary) workaround was to catch just the exception thrown from the startForeground assuming that running background service could continue.

I don't think the service will be able to run properly in the background since background services are very limited in regards to execution time. I guess that some other crash might pop up, but to be honest, I think this fix is a start towards a solution, and if another crash pops up we will have more details about what could be going on exactly.

That said, I will go ahead and approve this PR!

Copy link
Contributor

@thomashorta thomashorta left a comment

Choose a reason for hiding this comment

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

As mentioned here: #19987 (comment) I still have some concerns about this not getting completely rid of crashes in the background/foreground service flow, but this might be a start towards a solution, or at least a better understanding of the problem based on possible follow up issues/crashes in this flow.

That said, I will approve it. Thanks for the changes.

@antonis
Copy link
Contributor Author

antonis commented Jan 22, 2024

Thank you for reviewing and your feedback @thomashorta 🙇

I still have some concerns about this not getting completely rid of crashes in the background/foreground service flow, but this might be a start towards a solution, or at least a better understanding of the problem based on possible follow up issues/crashes in this flow.

This makes sense. I'll keep an eye on this during the beta period to catch any related crashes.

@antonis antonis merged commit 585f83e into trunk Jan 22, 2024
24 of 29 checks passed
@antonis antonis deleted the fix/18714-prevent-background-upload-crash branch January 22, 2024 15:38
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.

4 participants