From 37a6163f3f7e125f617ffed1d02b27dcd85b9b7f Mon Sep 17 00:00:00 2001 From: Steven Moreland Date: Wed, 4 Dec 2019 14:06:50 -0800 Subject: [PATCH] oboeservice: hold onto linked binder Before, in Android 10, this would have been a small leak because unlinkToDeath is not called, but now, objects are automatically unlinked. However, this presents a problem since you must hold onto a binder in order to linkToDeath. This now holds onto the binder and is relying on the automatic-unlinkToDeath behavior. Fixes: 145675653 Test: atest CtsNativeMediaAAudioTestCases arm64-v8a CtsNativeMediaAAudioTestCases: Passed: 122, Failed: 0, Ignored: 0, Assumption Failed: 0 Test: after using app and killing which uses AAudio MMAP stream, dumpsys output for media.aaudio is cleaned up: ------------ AAudio Service ------------ Stream Handles: AAudioClientTracker: AAudioEndpointManager: Exclusive MMAP Endpoints: 0 ExclusiveSearchCount: 3 ExclusiveFoundCount: 0 ExclusiveOpenCount: 1 ExclusiveCloseCount: 1 Shared Endpoints: 0 SharedSearchCount: 1 SharedFoundCount: 0 SharedOpenCount: 0 SharedCloseCount: 0 Change-Id: I2ef141320dc3ef0135fb7ef002d85377d06a75c2 --- services/oboeservice/AAudioClientTracker.cpp | 10 +++++----- services/oboeservice/AAudioClientTracker.h | 4 +++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/services/oboeservice/AAudioClientTracker.cpp b/services/oboeservice/AAudioClientTracker.cpp index 857256156e..6e14434c33 100644 --- a/services/oboeservice/AAudioClientTracker.cpp +++ b/services/oboeservice/AAudioClientTracker.cpp @@ -75,10 +75,10 @@ aaudio_result_t AAudioClientTracker::registerClient(pid_t pid, std::lock_guard lock(mLock); if (mNotificationClients.count(pid) == 0) { - sp notificationClient = new NotificationClient(pid); + sp binder = IInterface::asBinder(client); + sp notificationClient = new NotificationClient(pid, binder); mNotificationClients[pid] = notificationClient; - sp binder = IInterface::asBinder(client); status_t status = binder->linkToDeath(notificationClient); ALOGW_IF(status != NO_ERROR, "registerClient() linkToDeath = %d\n", status); return AAudioConvert_androidToAAudioResult(status); @@ -113,7 +113,7 @@ AAudioClientTracker::registerClientStream(pid_t pid, sp if (notificationClient == 0) { // This will get called the first time the audio server registers an internal stream. ALOGV("registerClientStream(%d,) unrecognized pid\n", pid); - notificationClient = new NotificationClient(pid); + notificationClient = new NotificationClient(pid, nullptr); mNotificationClients[pid] = notificationClient; } notificationClient->registerClientStream(serviceStream); @@ -136,8 +136,8 @@ AAudioClientTracker::unregisterClientStream(pid_t pid, return AAUDIO_OK; } -AAudioClientTracker::NotificationClient::NotificationClient(pid_t pid) - : mProcessId(pid) { +AAudioClientTracker::NotificationClient::NotificationClient(pid_t pid, const sp& binder) + : mProcessId(pid), mBinder(binder) { } AAudioClientTracker::NotificationClient::~NotificationClient() { diff --git a/services/oboeservice/AAudioClientTracker.h b/services/oboeservice/AAudioClientTracker.h index accf1a7b31..00ff467751 100644 --- a/services/oboeservice/AAudioClientTracker.h +++ b/services/oboeservice/AAudioClientTracker.h @@ -73,7 +73,7 @@ private: */ class NotificationClient : public IBinder::DeathRecipient { public: - NotificationClient(pid_t pid); + NotificationClient(pid_t pid, const android::sp& binder); virtual ~NotificationClient(); int32_t getStreamCount(); @@ -91,6 +91,8 @@ private: mutable std::mutex mLock; const pid_t mProcessId; std::set> mStreams; + // hold onto binder to receive death notifications + android::sp mBinder; }; mutable std::mutex mLock;