How to debug leak caused by UnixDirectoryStream in Android app
Asked Answered
W

1

23

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?

Weinert answered 8/5, 2021 at 4:28 Comment(5)
Did you find out where the violation originates?Excerpta
@Excerpta sadly, noWeinert
It seems to very deep in the system itself. It seems the offending resource is allocated before the app has the chance to set strictmode so the stacktrace of the allocation is not recorded.Kilby
did you find a solution for it yet?Boughten
@Boughten sadly, noWeinert
P
6

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.

Phila answered 30/4, 2023 at 18:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.