diff options
| author | Zim <zezeozue@google.com> | 2021-07-29 20:51:10 +0100 |
|---|---|---|
| committer | Zim <zezeozue@google.com> | 2021-07-29 22:05:17 +0100 |
| commit | 366493edd29b1ec2a7be0323dd547d242e6797fa (patch) | |
| tree | 2328b35bf9f07e42e91bd248fc4de2ad3c16d1ca /core/java/android/os/FileUtils.java | |
| parent | 9eee7d3f29d051cdc29404ea8fa235695796d1cc (diff) | |
Fix fd leak while bypassing transcoding in media APIs
To call the media service while bypassing transcoding there are 4 file
descriptors involved:
A: The original file descriptor the app owns and is responsible for
closing
B: Dupe of A, to pass into the MediaStore API as an input to receive a
non-transoding fd
C: The output from the MediaStore API
D: Final fd owned by the media service, after C gets duped over binder
as part of setDataSource
We were leaking (B) and (C). Now we close them appropriately.
See I0124ec8fbd0471237e99bab321f903544f8fe1f8 for another 2 fd leak
fix in MediaProvider
Test: atest TranscodeTest
Test: Manual with 'adb shell lsof | grep <filename>'
Bug: 194828489
Bug: 179804072
Change-Id: I978257bbc4a8f6813b6e6a5ce22124257204f432
Merged-In: I978257bbc4a8f6813b6e6a5ce22124257204f432
Diffstat (limited to 'core/java/android/os/FileUtils.java')
| -rw-r--r-- | core/java/android/os/FileUtils.java | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/core/java/android/os/FileUtils.java b/core/java/android/os/FileUtils.java index f3e0ce9cd19e..1bc64951ad96 100644 --- a/core/java/android/os/FileUtils.java +++ b/core/java/android/os/FileUtils.java @@ -1460,15 +1460,15 @@ public final class FileUtils { /** {@hide} */ @VisibleForTesting public static ParcelFileDescriptor convertToModernFd(FileDescriptor fd) { - try { - Context context = AppGlobals.getInitialApplication(); - if (UserHandle.getAppId(Process.myUid()) == getMediaProviderAppId(context)) { - // Never convert modern fd for MediaProvider, because this requires - // MediaStore#scanFile and can cause infinite loops when MediaProvider scans - return null; - } - return MediaStore.getOriginalMediaFormatFileDescriptor(context, - ParcelFileDescriptor.dup(fd)); + Context context = AppGlobals.getInitialApplication(); + if (UserHandle.getAppId(Process.myUid()) == getMediaProviderAppId(context)) { + // Never convert modern fd for MediaProvider, because this requires + // MediaStore#scanFile and can cause infinite loops when MediaProvider scans + return null; + } + + try (ParcelFileDescriptor dupFd = ParcelFileDescriptor.dup(fd)) { + return MediaStore.getOriginalMediaFormatFileDescriptor(context, dupFd); } catch (Exception e) { Log.d(TAG, "Failed to convert to modern format file descriptor", e); return null; |
