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: crash about persistent websocket being started from background [WPB-6551] #2745

Merged
merged 6 commits into from Feb 28, 2024

Conversation

saleniuk
Copy link
Contributor

@saleniuk saleniuk commented Feb 28, 2024

BugWPB-6551 [Android] Crash caused by Foreground services not allowed


PR Submission Checklist for internal contributors

  • The PR Title

    • conforms to the style of semantic commits messages¹ supported in Wire's Github Workflow²
    • contains a reference JIRA issue number like SQPIT-764
    • answers the question: If merged, this PR will: ... ³
  • The PR Description

    • is free of optional paragraphs and you have filled the relevant parts to the best of your ability

What's new in this PR?

Issues

Sometimes startForeground for PersistentWebSocketService results in ForegroundServiceStartNotAllowedException.

Causes (Optional)

This exception means that startForeground was executed when it's not allowed to, there are multiple restrictions on when foreground service can be started - more here: https://developer.android.com/develop/background-work/services/foreground-services#background-start-restriction-exemptions
For us, we are allowed to start foreground service from Activity when the app is in the foreground and when the device is booted or app is updated (probably within 20 seconds) and we use StartServiceReceiver for that. The problem is probably that we collect in a coroutine, so it's just left hanging and receiving new values even in the background.

Solutions

When StartServiceReceiver is called, get the list of persistent websocket statuses only once to make sure that we open the service within the permitted time frame. This persistent websocket flag can be changed only in settings by the user when the app is in the foreground so no need to observe for changes in this receiver - if changed then the service will be started from WireActivityViewModel.
The logic of deciding whether the service should be started or not is extracted to dedicated use case and tested.

Testing

Test Coverage (Optional)

  • I have added automated test to this contribution

How to Test

There's no specific reproduction path, from the logs it looks like it could happen when the app was updated in the background and the sync was started so that the user's values in database are updated and persistentWebSocketStatusListFlow emits new list.


PR Post Submission Checklist for internal contributors (Optional)

  • Wire's Github Workflow has automatically linked the PR to a JIRA issue

PR Post Merge Checklist for internal contributors

  • If any soft of configuration variable was introduced by this PR, it has been added to the relevant documents and the CI jobs have been updated.

References
  1. https://sparkbox.com/foundry/semantic_commit_messages
  2. https://github.com/wireapp/.github#usage
  3. E.g. feat(conversation-list): Sort conversations by most emojis in the title #SQPIT-764.

Copy link
Contributor

github-actions bot commented Feb 28, 2024

Test Results

809 tests   809 ✅  9m 10s ⏱️
109 suites    0 💤
109 files      0 ❌

Results for commit 98c23e3.

♻️ This comment has been updated with latest results.

@AndroidBob
Copy link
Collaborator

Build 3355 failed.

Copy link
Contributor

APKs built during tests are available here. Scroll down to Artifacts!

@AndroidBob
Copy link
Collaborator

Build 3356 succeeded.

The build produced the following APK's:

@saleniuk saleniuk requested review from a team, alexandreferris, borichellow, MohamadJaara, vitorhugods and mchenani and removed request for a team February 28, 2024 12:01
Copy link
Contributor

APKs built during tests are available here. Scroll down to Artifacts!

@AndroidBob
Copy link
Collaborator

Build 3364 succeeded.

The build produced the following APK's:

Copy link
Contributor

@vitorhugods vitorhugods left a comment

Choose a reason for hiding this comment

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

🙇🏼‍♂️

Copy link
Contributor

APKs built during tests are available here. Scroll down to Artifacts!

@AndroidBob
Copy link
Collaborator

Build 3366 succeeded.

The build produced the following APK's:

Copy link
Contributor

@alexandreferris alexandreferris left a comment

Choose a reason for hiding this comment

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

👌🏻

@saleniuk saleniuk merged commit 85593a6 into release/candidate Feb 28, 2024
14 checks passed
@saleniuk saleniuk deleted the fix/start-persistent-socket-service-crash branch February 28, 2024 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants