androidandroid-strictmode

How to debug leak caused by UnixDirectoryStream in Android app


After enabling StrictMode I noticed this:

2021-05-08 11:09:42.854 14347-14773/com.myapp D/StrictMode: StrictMode policy violation: android.os.strictmode.LeakedClosableViolation: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks. Callsite: close
        at android.os.StrictMode$AndroidCloseGuardReporter.report(StrictMode.java:1929)
        at dalvik.system.CloseGuard.warnIfOpen(CloseGuard.java:305)
        at sun.nio.fs.UnixDirectoryStream.finalize(UnixDirectoryStream.java:240)
        at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:291)
        at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:278)
        at java.lang.Daemons$Daemon.run(Daemons.java:139)
        at java.lang.Thread.run(Thread.java:923)
2021-05-08 11:09:42.855 14347-14773/com.myapp D/StrictMode: StrictMode policy violation: android.os.strictmode.LeakedClosableViolation: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks. Callsite: close
        at android.os.StrictMode$AndroidCloseGuardReporter.report(StrictMode.java:1929)
        at dalvik.system.CloseGuard.warnIfOpen(CloseGuard.java:305)
        at sun.nio.fs.UnixSecureDirectoryStream.finalize(UnixSecureDirectoryStream.java:580)
        at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:291)
        at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:278)
        at java.lang.Daemons$Daemon.run(Daemons.java:139)
        at java.lang.Thread.run(Thread.java:923)

How to debug this?


Solution

  • Setting breakpoints in StrictMode and Daemons, I was able to track this down to a directory stream opened in ActivityThread.handleAttachStartupAgents. Indeed the directory stream opened there was not closed. This has been fixed in March 2023 by this commit:

    https://github.com/aosp-mirror/platform_frameworks_base/commit/e7ae30f76788bcec4457c4e0b0c9cbff2cf892f3

    At this time the fix is not in my SDK, so I'll just continue to see the error.