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 in Messaging on Android when Terminate is called while tasks are still pending. #739

Merged
merged 3 commits into from
Nov 8, 2021

Conversation

TBarendt
Copy link
Contributor

@TBarendt TBarendt commented Nov 5, 2021

When firebase::messaging::Terminate is called while there are still pending tasks left a null pointer exception is causing an ANR on Andriod.

FutureData is destroyed before calling util::Terminate which in turn will try to cancel all pending callbacks. This will call cancel() in JniResultCallback.java which will lock a mutex and callback into C++ using the JniResultCallback_nativeResult function. Depending on the task canceled it will call into different callbacks in messaging.cc and some of them as for example CompleteStringCallback are using FutureData which has already been destroyed. This will cause the thread to stop and the mutex in JniResultCallbak.java to remain locked. The next message that either completes or fails will now end up in a dead lock causing an ANR.

The proposed fix is to not destroy FutureData until after the tasks have been canceled.

Description

Provide details of the change, and generalize the change in the PR title above.


Testing

Describe how you've tested these changes. Link any manually triggered Integration tests or CPP binary SDK Packaging Github Action workflows, if applicable.


Type of Change

Place an x the applicable box:

  • Bug fix. Add the issue # below if applicable.
  • New feature. A non-breaking change which adds functionality.
  • Other, such as a build process or documentation change.

Notes

  • Bug fixes and feature changes require an update to the Release Notes section of release_build_files/readme.md.
  • Read the contribution guidelines CONTRIBUTING.md.
  • Changes to the public API require an internal API review. If you'd like to help us make Firebase APIs better, please propose your change in a feature request so that we can discuss it together.
When firebase::messaging::Terminate is called while there are still pending tasks left a null pointer exception is causing an ANR on Andriod.

FutureData is destroyed before calling util::Terminate which in turn will try to cancel all pending callbacks. This will call cancel() in JniResultCallback.java which will lock a mutex and callback into C++ using the JniResultCallback_nativeResult function. Depending on the task canceled it will call into different callbacks in messaging.cc and some of them as for example CompleteStringCallback are using FutureData which has already been destroyed. This will cause the thread to stop and the mutex in JniResultCallbak.java to remain locked. The next message that either completes or fails will now end up in a dead lock causing an ANR.

The proposed fix is to not destroy FutureData until after the tasks have been canceled.
@google-cla google-cla bot added the cla: yes label Nov 5, 2021
ktaylor86
ktaylor86 previously approved these changes Nov 5, 2021
Copy link

@ktaylor86 ktaylor86 left a comment

Choose a reason for hiding this comment

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

Resolves a common ANR event in firebase when shutting down Messaging on Android

@jonsimantov jonsimantov self-assigned this Nov 5, 2021
@jonsimantov jonsimantov self-requested a review November 5, 2021 22:42
@github-actions github-actions bot added the tests: in-progress This PR's integration tests are in progress. label Nov 5, 2021
@github-actions
Copy link

github-actions bot commented Nov 5, 2021

❌  Integration test FAILED

Requested by @jonsimantov on commit refs/pull/739/merge
Last updated: Fri Nov 5 18:32 PDT 2021
View integration test log & download artifacts

Failures Configs
firestore [TEST] [ERROR] [Android] [ubuntu, windows] [android_target]
functions [TEST] [ERROR] [Android] [ubuntu] [android_target]
messaging [TEST] [ERROR] [Android] [All os] [android_target]

Add flaky tests to go/fpl-cpp-flake-tracker

@jonsimantov
Copy link
Contributor

jonsimantov commented Nov 5, 2021

Nice find! I kicked off an integration test run and will approve the PR after that finishes.

Because we currently have some flaky tests, don't worry if the integration test fails - I'll manually remove the failure tag as long as the failure isn't caused by this change.

Oh, also, could you please add a note to the "Next Release" section of release_build_files/readme.md? (You may need to merge from main if you don't see that section, I just added it in a recent PR.)

Thanks for your contribution!

@jonsimantov jonsimantov changed the title Andriod ANR Nov 5, 2021
@github-actions github-actions bot dismissed ktaylor86’s stale review November 5, 2021 22:59

🍞 Dismissed stale approval on external PR.

@jonsimantov jonsimantov added the skip-release-notes Skip release notes check label Nov 5, 2021
@github-actions github-actions bot added the tests: failed This PR's integration tests failed. label Nov 6, 2021
@firebase-workflow-trigger firebase-workflow-trigger bot removed the tests: in-progress This PR's integration tests are in progress. label Nov 6, 2021
@jonsimantov jonsimantov removed the tests: failed This PR's integration tests failed. label Nov 6, 2021
@jonsimantov jonsimantov merged commit e4d43e5 into firebase:main Nov 8, 2021
@firebase firebase locked and limited conversation to collaborators Dec 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes skip-release-notes Skip release notes check
3 participants