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

ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. #20739

Open
sentry-io bot opened this issue Apr 30, 2024 · 5 comments
Assignees
Labels
Gravatar Gravatar integration [Type] Crash

Comments

@sentry-io
Copy link

sentry-io bot commented Apr 30, 2024

Sentry Issue: WORDPRESS-ANDROID-2Y5B

ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
    at org.wordpress.android.login.LoginBaseFormFragment.endProgress(LoginBaseFormFragment.java:273)
    at org.wordpress.android.ui.accounts.signup.SignupEpilogueFragment.access$200(SignupEpilogueFragment.java:103)
    public class SignupEpilogueFragment extends LoginBaseFormFragment<SignupEpilogueListener>
    at org.wordpress.android.ui.accounts.signup.SignupEpilogueFragment$13.onError(SignupEpilogueFragment.java:752)
    endProgress();
...
(20 additional frame(s) were not displayed)
@sentry-io sentry-io bot added [Type] Crash Gravatar Gravatar integration labels Apr 30, 2024
@dangermattic
Copy link
Collaborator

Thanks for reporting! 👍

@thomashorta
Copy link
Contributor

@maxme, I did a quick investigation about this issue and it looks like the new Gravatar-related code, introduced by you in #20590, is causing this crash in WordPress 24.7, which currently seems to have affected only a few users.

I am adding you as an assignee but feel free to assign someone else if it makes sense.

What I believe to be the root cause of the issue is that the AvatarService.upload method runs inside a coroutine in a background thread and also calls the callbacks from GravatarListener from within that background thread, but endProgress called by both callbacks needs to run on the UI thread, and the thread is not being switched anywhere.

public void onSuccess(@NonNull Unit response) {
endProgress();
AnalyticsTracker.track(Stat.ME_GRAVATAR_UPLOADED);
mPhotoUrl = WPAvatarUtils.rewriteAvatarUrl(mAccount.getAccount().getAvatarUrl(),
getResources().getDimensionPixelSize(R.dimen.avatar_sz_large));
loadAvatar(mPhotoUrl, filePath);
mHeaderAvatarAdd.setVisibility(View.GONE);
mIsAvatarAdded = true;
}
@Override
public void onError(@NonNull ErrorType errorType) {
endProgress();
showErrorDialogWithCloseButton(getString(R.string.signup_epilogue_error_avatar));
Map<String, Object> properties = new HashMap<>();
properties.put("error_type", errorType);
AnalyticsTracker.track(AnalyticsTracker.Stat.ME_GRAVATAR_UPLOAD_EXCEPTION, properties);
AppLog.e(T.NUX, "Uploading image to Gravatar failed");
}

A couple of quick fix suggestions I could think of are:

  1. Use getActivity().runOnUiThread in the WP-Android-side around any parts of callback code that interact with the UI (endProgress, setVisibility, etc)
  2. Make changes in the Gravatar-SDK-Android AvatarService to always call listener callbacks in the UI thread, which is usually the expected behavior in APIs than own their background execution (something like using withContext(Dispatchers.Main) around the code that calls onSuccess andonError).

Anyway, these are just suggestion I could come up with after doing a quick investigation, so feel free to ignore them if it doesn't make sense as well.

@maxme
Copy link
Contributor

maxme commented May 2, 2024

Thanks @thomashorta for investigating, yeah I think having onSuccess and onError running on the main Android thread is best, I'll fix it in a 0.2.1 release of gravatar.

Copy link
Author

sentry-io bot commented May 9, 2024

Sentry Issue: JETPACK-ANDROID-R1X

Copy link
Author

sentry-io bot commented May 14, 2024

Sentry Issue: JETPACK-ANDROID-R3V

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Gravatar Gravatar integration [Type] Crash
Projects
None yet
Development

No branches or pull requests

3 participants