From b9520e49b20f70e991b97fb77f2b5dc015d56229 Mon Sep 17 00:00:00 2001 From: Shukang Zhou Date: Fri, 6 Jan 2017 13:26:13 -0800 Subject: [PATCH] Close a file descriptor before losing its reference. In ActivityManagerService.profileControl(..), a duplicate of 'mProfileFd' is passed to the app via proc.thread.profilerControl(..) as the output file of profiling trace. 'mProfileFd' itself is set to null shortly after. This made the ParcelFileDescriptor object referred by 'mProfileFd' unreachable, but the file descriptor remained open until next garbage collection, which would close the file descriptor before deallocating it. This behavior was not harmful to ActivityManagerService, but at system level the file was kept open for longer than it is needed. Other entities on the system that are interested in the same file may get confused if they are monitoring the file's status. This CL closes the file descriptor promptly after it becomes useless. More details can be found at b/33300094#comment3. Bug: b/33300094. Test: Flashed angler-eng build to a Nexus 6P and it worked as expected. Change-Id: Ifa511dea5101a854c5db5f369504cd04e4425f43 --- .../java/com/android/server/am/ActivityManagerService.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java index 8f93a222eb3c0..6be33727e24c4 100644 --- a/services/core/java/com/android/server/am/ActivityManagerService.java +++ b/services/core/java/com/android/server/am/ActivityManagerService.java @@ -21814,6 +21814,10 @@ public class ActivityManagerService extends IActivityManager.Stub profilerInfo.profileFd = fd; proc.thread.profilerControl(start, profilerInfo, profileType); fd = null; + try { + mProfileFd.close(); + } catch (IOException e) { + } mProfileFd = null; } else { stopProfilerLocked(proc, profileType);