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

Leak detected in standard AIDL service pattern #1906

Closed
jpd236 opened this issue Aug 12, 2020 · 3 comments
Closed

Leak detected in standard AIDL service pattern #1906

jpd236 opened this issue Aug 12, 2020 · 3 comments

Comments

@jpd236
Copy link

jpd236 commented Aug 12, 2020

LeakCanary is fairly consistently reporting a leak due to the Binder object created in an AIDL Service implementation after the Service is destroyed. From some digging, it seems like this is expected platform behavior, and that there was previously a PR that was meant to resolve this (or something that sounds a lot like it):

#351

but it doesn't appear to be working, at least the way it's wired up.

I've attached a sample app which demonstrates the problem: LeakTest.zip. This consists of an AIDL service which is pretty much exactly what is documented at https://developer.android.com/guide/components/aidl#Implement, and an Activity which binds to the service upon clicking a button. Clicking the button fairly reliably produces the below leak, and I can't see anything wrong with what the app is doing. The leak monitoring in the Service is done according to the example at https://square.github.io/leakcanary/recipes/#watching-objects-with-a-lifecycle.

Is this indeed a regression from that PR, or something else?

LeakTrace information

====================================
HEAP ANALYSIS RESULT
====================================
1 APPLICATION LEAKS

References underlined with "~~~" are likely causes.
Learn more at https://squ.re/leaks.

1132 bytes retained by leaking objects
Signature: 9a265ac2c81e62a8947aa84ddba98dd79dc3258e
┬───
│ GC Root: Global variable in native code
│
├─ com.example.leaktest.MainService$1 instance
│    Leaking: UNKNOWN
│    Anonymous subclass of com.example.leaktest.IMainService$Stub
│    ↓ MainService$1.this$0
│                    ~~~~~~
╰→ com.example.leaktest.MainService instance
​     Leaking: YES (ObjectWatcher was watching this because MainService received Service#onDestroy callback)
​     key = 9ce278da-0f73-4d6f-93fe-dd57582dfec1
​     watchDurationMillis = 11411
​     retainedDurationMillis = 6409
====================================
0 LIBRARY LEAKS

A Library Leak is a leak caused by a known bug in 3rd party code that you do not have control over.
See https://square.github.io/leakcanary/fundamentals-how-leakcanary-works/#4-categorizing-leaks
====================================
METADATA

Please include this in bug reports and Stack Overflow questions.

Build.VERSION.SDK_INT: 29
Build.MANUFACTURER: Google
LeakCanary version: 2.4
App process name: com.example.leaktest
Analysis duration: 3037 ms
Heap dump file path: /data/user/0/com.example.leaktest/files/leakcanary/2020-08-12_12-53-46_586.hprof
Heap dump timestamp: 1597262030944
====================================
@jpd236
Copy link
Author

jpd236 commented Aug 12, 2020

Ah, that commit seems to have been reverted in #598, so perhaps the underlying problem was just never fixed.

I guess maybe the "right" thing to do here is to just never make a Binder that depends on the outer service (using the Application context if a context is necessary)? In which case maybe the issue is with the Android documentation suggesting a bad pattern?

@pyricau
Copy link
Member

pyricau commented Aug 31, 2020

Thanks for this @jpd236 !

You're absolutely right: this is a "bad" pattern that unfortunately happens too often. AFAIK binder instances are held until the other processes finalizes the object on its side. This can happen long after the service is unbound. What that means is that binders should always always clear their references to the components they talk to after they stop being used.

Unfortunately this has created many leaks in AOSP and apps.

Would you mind filing an issue on AOSP for this? The doc should be updated.

@jpd236
Copy link
Author

jpd236 commented Aug 31, 2020

Thanks - filed as https://issuetracker.google.com/167281719.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants