5

I have implemented a BackupAgent following the guidelines for Data Backup. The code behaves as expected until StrictMode.VmPolicy is set to detect leaked closeable objects. After a backup is performed and GC occurs, CloseGuard reports a leaked ParcelFileDescriptor with this stack trace:

06-28 21:47:39.683  25072-25081/com.qbix.nub E/StrictMode﹕ A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
    java.lang.Throwable: Explicit termination method 'close' not called
            at dalvik.system.CloseGuard.open(CloseGuard.java:184)
            at android.os.ParcelFileDescriptor.<init>(ParcelFileDescriptor.java:179)
            at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:905)
            at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:897)
            at android.app.IBackupAgent$Stub.onTransact(IBackupAgent.java:64)
            at android.os.Binder.execTransact(Binder.java:404)
            at dalvik.system.NativeStart.run(Native Method)
06-28 21:47:39.683  25072-25081/com.qbix.nub W/System.err﹕ StrictMode VmPolicy violation with POLICY_DEATH; shutting down.
06-28 21:47:39.683  25072-25081/com.qbix.nub I/Process﹕ Sending signal. PID: 25072 SIG: 9

To confirm that I was not leaking the ParcelFileDescriptor in my BackupAgent, I stubbed onBackup() like this:

@Override
public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState) throws IOException {
    if (oldState != null) {
        oldState.close();
    }
    newState.close();
    return;
}

This change did not fix the leak.

These are the steps I use to reproduce the problem:

  1. Modify app data to trigger a call to BackupManager.dataChanged()
  2. Use adb shell bmgr run to force a backup operation
  3. Initiate GC from Android Studio

I don't know enough about bound services to understand if the stack trace provides clues about whether the leak is in the system BackupService or is caused by an error in my code I am unable to see. The continued occurrence of the leak when running with the stubbed onBackup() suggests to me that the leak is in the service.

While researching this issue, I found these other issues posted in the last few months that included the same leak report in their logcats:

Resource leak in Android

Having problems With navigation Drawer in Android

Android - HTTP Get - Explicit Termination not called error. What am I missing?

Resolving java.lang.Throwable exception in an android

The logcat shown above is from a KitKat device. On another device running Lollipop, I think the same error is occurring. The app is killed but the logcat does not include the CloseGuard dump. Don't know what I need to do to see that.

Community
  • 1
  • 1
Bob Snyder
  • 37,759
  • 6
  • 111
  • 158
  • 1
    "The continued occurrence of the leak when running with the stubbed `onBackup()` suggests to me that the leak is in the service" -- I agree. OTOH, I didn't see an issue filed about it, and I would have expected one if every backup basically leaked a `ParcelFileDescriptor`. I detest the backup system on privacy and security grounds, so I have not used it personally. – CommonsWare Jun 30 '15 at 17:59
  • 1
    I created [AOSP Issue 178506](https://code.google.com/p/android/issues/detail?id=178506) for this. – Bob Snyder Jul 01 '15 at 03:02
  • @BobSnyder any news on this? I have the same issue on Android 5.1; I add a BackupAgent that does nothing and I get this leak. Could it have something to do with faulty manifest merging? Also in Google example backup app, they don't manually close the ParcelFileDescriptors. – JohnyTex Jan 04 '22 at 09:07
  • @BobSnyder Btw, did you get backup working properly while getting this error? – JohnyTex Jan 04 '22 at 09:18
  • @CommonsWare " I agree. OTOH, I didn't see an issue filed about it, and I would have expected one if every backup basically leaked a ParcelFileDescriptor" - but you have to enable StrictMode to see it, most people don't, right? Also bug might not be serious? – JohnyTex Jan 04 '22 at 09:29

0 Answers0