facebook / facebook-android-sdk

Used to integrate Android apps with Facebook Platform.
https://developers.facebook.com/docs/android
Other
6.16k stars 3.65k forks source link

Deadlock in FB SDK code #789

Closed grebulon closed 3 years ago

grebulon commented 4 years ago

Checklist

Environment

Goals

Just running my app

Expected Results

No ANRs

Actual Results

We get occasional ANR which I traced to the described deadlock

Steps to Reproduce

Just use Facebook SDK with enough users and you'll get to this deadlock

Code Samples & Details

Here are the stack traces that describe the deadlock. These are the only two threads in the process that run Facebook code. You'll notice that AsyncTask #4 is waiting, probably for main thread which is holding mutator lock mutex.

"main" prio=5 tid=1 Runnable
  | group="main" sCount=0 dsCount=0 flags=0 obj=0x7672c810 self=0xe3b25000
  | sysTid=25977 nice=-1 cgrp=default sched=0/0 handle=0xe820d4a8
  | state=R schedstat=( 1008373120 305279791 1640 ) utm=37 stm=63 core=0 HZ=100
  | stack=0xff563000-0xff565000 stackSize=8MB
  | held mutexes= "mutator lock"(shared held)
  at com.facebook.appevents.internal.ActivityLifecycleTracker$1.onCompleted (ActivityLifecycleTracker.java:82)
  at com.facebook.internal.FeatureManager$1.onCompleted (FeatureManager.java:47)
  at com.facebook.internal.FetchedAppGateKeepersManager$2.run (FetchedAppGateKeepersManager.java:156)
  at android.os.Handler.handleCallback (Handler.java:873)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loop (Looper.java:214)
  at android.app.ActivityThread.main (ActivityThread.java:7124)
  at java.lang.reflect.Method.invoke (Native method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:494)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:975)
"AsyncTask #4" prio=5 tid=17 Waiting
  | group="main" sCount=1 dsCount=0 flags=1 obj=0x12c40088 self=0xdbe21000
  | sysTid=26036 nice=10 cgrp=default sched=0/0 handle=0xc9484970
  | state=S schedstat=( 52004839 56563498 131 ) utm=1 stm=4 core=6 HZ=100
  | stack=0xc9381000-0xc9383000 stackSize=1042KB
  | held mutexes=
  at java.lang.Object.wait (Native method)
- waiting on <0x0c978aff> (a java.lang.Object)
  at java.lang.Thread.parkFor$ (Thread.java:2137)
- locked <0x0c978aff> (a java.lang.Object)
  at sun.misc.Unsafe.park (Unsafe.java:358)
  at java.util.concurrent.locks.LockSupport.park (LockSupport.java:190)
  at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await (AbstractQueuedSynchronizer.java:2059)
  at java.util.concurrent.LinkedBlockingDeque.takeFirst (LinkedBlockingDeque.java:492)
  at java.util.concurrent.LinkedBlockingDeque.take (LinkedBlockingDeque.java:680)
  at com.facebook.internal.AttributionIdentifiers$GoogleAdServiceConnection.getBinder (AttributionIdentifiers.java:315)
  at com.facebook.internal.AttributionIdentifiers.getAndroidIdViaService (AttributionIdentifiers.java:165)
  at com.facebook.internal.AttributionIdentifiers.getAndroidId (AttributionIdentifiers.java:81)
  at com.facebook.internal.AttributionIdentifiers.getAttributionIdentifiers (AttributionIdentifiers.java:180)
  at com.facebook.FacebookSdk.publishInstallAndWaitForResponse (FacebookSdk.java:697)
  at com.facebook.FacebookSdk$7.run (FacebookSdk.java:685)
  at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1167)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:641)
  at java.lang.Thread.run (Thread.java:764)
battulasaivinesh commented 4 years ago

Facing the same issue after releasing the latest SDK. ANRs are increased by more than 30% due to this deadlock. Had to revert back to old sdk.

battulasaivinesh commented 4 years ago

Happening specifically in Huawai Devices

gubatron commented 4 years ago
private static AttributionIdentifiers getAndroidIdViaService(Context context) {
    GoogleAdServiceConnection connection = new GoogleAdServiceConnection();
    Intent intent = new Intent("com.google.android.gms.ads.identifier.service.START");
    intent.setPackage("com.google.android.gms");
    if (context.bindService(intent, connection, Context.BIND_AUTO_CREATE)) {
      try {
        GoogleAdInfo adInfo = new GoogleAdInfo(connection.getBinder()); // THIS IS BLOCKING
        AttributionIdentifiers identifiers = new AttributionIdentifiers();
        identifiers.androidAdvertiserId = adInfo.getAdvertiserId();
        identifiers.limitTracking = adInfo.isTrackingLimited();
        return identifiers;
      } catch (Exception exception) {
        Utility.logd("android_id", exception);
      } finally {
        context.unbindService(connection);
      }
    }
    return null;
  }

That getBinder() method on the GoogleAdServiceConnection inner class uses a blocking queue queue.take().

It seems that queue contains references to IBinder services which get added onServiceConnected, my guess is that there's nothing on that queue by the time getAndroidIdViaService is called and it just lingers there, while at the same time a service is connecting and it cannot add elements to the queue because it's blocked trying to read.

The solution is probably in the time in which Facebook's SDK calls getAndroidIdViaService it might be doing so prematurely.

didixyy commented 4 years ago

i also have this problem,please fix it

stale[bot] commented 3 years ago

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

grebulon commented 3 years ago

No activity doesn't mean that the problem is solved...

vkreal commented 3 years ago

Please fix the ANRs issue.

00 pc 0000000000018cc4 /system/lib/libc.so (syscall+28)

00 pc 00000000000b71cd /system/lib/libart.so (art::ConditionVariable::WaitHoldingLocks(art::Thread*)+80)

00 pc 00000000003e0911 /system/lib/libart.so (art::GoToRunnable(art::Thread*)+300)

00 pc 00000000003e07b5 /system/lib/libart.so (art::JniMethodEnd(unsigned int, art::Thread*)+8)

00 pc 0000000000773b01 /system/framework/arm/boot-framework.oat (Java_android_os_BinderProxy_transactNative__ILandroid_os_Parcel_2Landroid_os_Parcel_2I+144)

at android.os.BinderProxy.transactNative (Native method) at android.os.BinderProxy.transact (Binder.java:778) at android.app.IActivityManager$Stub$Proxy.bindService (IActivityManager.java:4979) at android.app.ContextImpl.bindServiceCommon (ContextImpl.java:1622) at android.app.ContextImpl.bindService (ContextImpl.java:1561) at android.content.ContextWrapper.bindService (ContextWrapper.java:684) at com.facebook.internal.AttributionIdentifiers.getAndroidIdViaService (AttributionIdentifiers.java:163) at com.facebook.internal.AttributionIdentifiers.getAndroidId (AttributionIdentifiers.java:81) at com.facebook.internal.AttributionIdentifiers.getAttributionIdentifiers (AttributionIdentifiers.java:180) at com.facebook.FacebookSdk.publishInstallAndWaitForResponse (FacebookSdk.java:660) at com.facebook.FacebookSdk$5.run (FacebookSdk.java:648) at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:636) at java.lang.Thread.run (Thread.java:780)

1wayticket commented 3 years ago

The same problem !!!

stale[bot] commented 3 years ago

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] commented 3 years ago

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.

mrinaljain commented 2 years ago

i also have this problem,please fix it