PostHog / posthog-android

PostHog Android SDK
https://posthog.com/docs/libraries/android
MIT License
43 stars 23 forks source link

Exception java.lang.IllegalStateException: This ViewTreeObserver is not alive, call getViewTreeObserver() again #133

Closed wangjingyang closed 5 months ago

wangjingyang commented 5 months ago

Description

Exception java.lang.IllegalStateException: This ViewTreeObserver is not alive, call getViewTreeObserver() again at android.view.ViewTreeObserver.checkIsAlive (ViewTreeObserver.java:984) at android.view.ViewTreeObserver.removeOnDrawListener (ViewTreeObserver.java:759) at com.posthog.android.replay.PostHogReplayIntegration.cleanSessionState$lambda$6$lambda$5 (PostHogReplayIntegration.kt:253) at com.posthog.android.replay.PostHogReplayIntegration.$r8$lambda$Fo0KroR9KoezO0ojDFIMQW4ICps at com.posthog.android.replay.PostHogReplayIntegration$$ExternalSyntheticLambda2.run at android.os.Handler.handleCallback (Handler.java:938) at android.os.Handler.dispatchMessage (Handler.java:99) at android.os.Looper.loopOnce (Looper.java:226) at android.os.Looper.loop (Looper.java:313) at android.app.ActivityThread.main (ActivityThread.java:8663) at java.lang.reflect.Method.invoke at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:571) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1135)

marandaneto commented 5 months ago

@wangjingyang which version of the SDK are you using? is it crashing the app or just logging on to Logcat?

wangjingyang commented 5 months ago

v3.2.2 Yes, it crashed and was reported to firebase。 I think the code should be like this:

 private fun cleanSessionState(
        view: View,
        status: ViewTreeSnapshotStatus,
    ) {
        view.viewTreeObserver?.let { viewTreeObserver ->
            mainHandler.handler.post {
                if (viewTreeObserver.isAlive) { // Move this line of code inside the post code block
                    viewTreeObserver.removeOnDrawListener(status.listener)
                }
            }
        }
        view.phoneWindow?.let { window ->
            window.touchEventInterceptors -= onTouchEventListener
        }

        decorViews.remove(view)
    }
marandaneto commented 5 months ago

@wangjingyang I have a draft PR already https://github.com/PostHog/posthog-android/pull/134/files Thanks for reporting it, sorry about that. Not sure if it crashed tho, Since cleanSessionState is called by uninstall and the uninstall already swallows exceptions. Same for onRootViewsChangedListener. Maybe it'd crash because when we do mainHandler.handler.post, it's in a different scope so I have added another try catch anyway.

wangjingyang commented 5 months ago

When will the new version be updated?

marandaneto commented 5 months ago

Once https://github.com/PostHog/posthog-android/actions/runs/9219736666 finishes running it'll be published, usually maven central has a few minutes delay as well