From c8490879af989a5fc0af5b14830c1c19eee1679d Mon Sep 17 00:00:00 2001 From: Andrei Homescu Date: Thu, 23 Jun 2022 05:18:51 +0000 Subject: libbinder: fix hanging wait in RpcSession Fixes a hanging wait in RpcSession by notifying the condition variable for available connections immediately after adding a new outgoing connection. Bug: 224644083 Test: atest binderRpcTest Change-Id: I27c81127482859342cb789eaac087f55ae5c16d9 --- libs/binder/RpcSession.cpp | 21 +++++++++++---------- libs/binder/include/binder/RpcSession.h | 1 + 2 files changed, 12 insertions(+), 10 deletions(-) (limited to 'libs') diff --git a/libs/binder/RpcSession.cpp b/libs/binder/RpcSession.cpp index 8edc78f2f5..7ba08edec4 100644 --- a/libs/binder/RpcSession.cpp +++ b/libs/binder/RpcSession.cpp @@ -699,10 +699,7 @@ status_t RpcSession::addOutgoingConnection(std::unique_ptr rpcTran mRpcBinderState->sendConnectionInit(connection, sp::fromExisting(this)); } - { - std::lock_guard _l(mMutex); - connection->exclusiveTid = std::nullopt; - } + clearConnectionTid(connection); return status; } @@ -773,6 +770,15 @@ bool RpcSession::removeIncomingConnection(const sp& connection) { return false; } +void RpcSession::clearConnectionTid(const sp& connection) { + std::unique_lock _l(mMutex); + connection->exclusiveTid = std::nullopt; + if (mConnections.mWaitingThreads > 0) { + _l.unlock(); + mAvailableConnectionCv.notify_one(); + } +} + std::vector RpcSession::getCertificate(RpcCertificateFormat format) { return mCtx->getCertificate(format); } @@ -902,12 +908,7 @@ RpcSession::ExclusiveConnection::~ExclusiveConnection() { // is using this fd, and it retains the right to it. So, we don't give up // exclusive ownership, and no thread is freed. if (!mReentrant && mConnection != nullptr) { - std::unique_lock _l(mSession->mMutex); - mConnection->exclusiveTid = std::nullopt; - if (mSession->mConnections.mWaitingThreads > 0) { - _l.unlock(); - mSession->mAvailableConnectionCv.notify_one(); - } + mSession->clearConnectionTid(mConnection); } } diff --git a/libs/binder/include/binder/RpcSession.h b/libs/binder/include/binder/RpcSession.h index e76b14046b..7d5d481f83 100644 --- a/libs/binder/include/binder/RpcSession.h +++ b/libs/binder/include/binder/RpcSession.h @@ -264,6 +264,7 @@ private: sp assignIncomingConnectionToThisThread( std::unique_ptr rpcTransport); [[nodiscard]] bool removeIncomingConnection(const sp& connection); + void clearConnectionTid(const sp& connection); [[nodiscard]] status_t initShutdownTrigger(); -- cgit v1.2.3-59-g8ed1b