From e7ae30f76788bcec4457c4e0b0c9cbff2cf892f3 Mon Sep 17 00:00:00 2001 From: Mateus Azis Date: Mon, 27 Mar 2023 13:16:34 -0700 Subject: [PATCH] Close DirectoryStream in ActivityThread after use. Right now, the directory stream is never closed. This leads to confusing "A resource failed to call close" errors. After some debugging, they seem to be coming from sun.nio.fs.UnixSecureDirectoryStream objects and reported here: https://cs.android.com/android/platform/superproject/+/master:libcore/dalvik/src/main/java/dalvik/system/CloseGuard.java;l=340;drc=e32570b11273e703580b60fc9d59b96223f376da. Test: m dist -j && acloud create --local-instance --local-image Change-Id: Ie9fddc2eab505527985d8c6b2822623fe16c7f89 --- core/java/android/app/ActivityThread.java | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java index 559989368a9f9..5ba7a4cb4f541 100644 --- a/core/java/android/app/ActivityThread.java +++ b/core/java/android/app/ActivityThread.java @@ -238,6 +238,7 @@ import java.io.PrintWriter; import java.lang.ref.WeakReference; import java.lang.reflect.Method; import java.net.InetAddress; +import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; @@ -4221,18 +4222,20 @@ public final class ActivityThread extends ClientTransactionHandler static void handleAttachStartupAgents(String dataDir) { try { - Path code_cache = ContextImpl.getCodeCacheDirBeforeBind(new File(dataDir)).toPath(); - if (!Files.exists(code_cache)) { + Path codeCache = ContextImpl.getCodeCacheDirBeforeBind(new File(dataDir)).toPath(); + if (!Files.exists(codeCache)) { return; } - Path startup_path = code_cache.resolve("startup_agents"); - if (Files.exists(startup_path)) { - for (Path p : Files.newDirectoryStream(startup_path)) { - handleAttachAgent( - p.toAbsolutePath().toString() - + "=" - + dataDir, - null); + Path startupPath = codeCache.resolve("startup_agents"); + if (Files.exists(startupPath)) { + try (DirectoryStream startupFiles = Files.newDirectoryStream(startupPath)) { + for (Path p : startupFiles) { + handleAttachAgent( + p.toAbsolutePath().toString() + + "=" + + dataDir, + null); + } } } } catch (Exception e) {