已合并
refactor render connection IPC into single pid-keyed map and fix concurrent reconnect race #32217
refactor render connection IPC into single pid-keyed map and fix concurrent reconnect race #32217
已合并
LyBbq创建于 8月12日
共 7 个文件变更+34-52
@@ -330,56 +330,42 @@ bool RSRenderPipeline::RemoveConnection(pid_t remotePid, const sptr<RSIConnectio
330 RS_LOGE("RemoveConnection: token is nullptr");330 RS_LOGE("RemoveConnection: token is nullptr");
331 return false;331 return false;
332 }332 }
333- // temporarily extending the life cycle
334- auto tokenObj = token->AsObject();
335 std::unique_lock<std::mutex> lock(renderConnectionMutex_);333 std::unique_lock<std::mutex> lock(renderConnectionMutex_);
336- auto iter = renderConnections_.find(tokenObj);334+ auto iter = renderConnections_.find(remotePid);
337 if (iter == renderConnections_.end()) {335 if (iter == renderConnections_.end()) {
338 return false;336 return false;
339 }337 }
340- uint64_t tokenMaskId = iter->second.first;338+ // verify token consistency
341- tokenMaskIdMapTokens_.erase(tokenMaskId);339+ if (iter->second.token != token->AsObject()) {
342- renderConnections_.erase(tokenObj);340+ RS_LOGE("RemoveConnection: token mismatch for pid %{public}d", remotePid);
343- connectionProcessPid_.erase(remotePid);341+ return false;
344- lock.unlock();342+ }
343+ renderConnections_.erase(iter);
345 return true;344 return true;
346}345}
347 346 
348-void RSRenderPipeline::AddConnection(pid_t remotePid, uint64_t tokenMaskId,347+std::pair<sptr<RSIClientToRenderConnection>, uint64_t> RSRenderPipeline::AddConnection(pid_t remotePid,
349- sptr<IRemoteObject>& token, sptr<RSIClientToRenderConnection> connectToRenderConnection)348+ uint64_t tokenMaskId, sptr<IRemoteObject>& token, sptr<RSIClientToRenderConnection> connectToRenderConnection)
350{349{
351 std::unique_lock<std::mutex> lock(renderConnectionMutex_);350 std::unique_lock<std::mutex> lock(renderConnectionMutex_);
352- if (renderConnections_.find(token) != renderConnections_.end()) {351+ auto iter = renderConnections_.find(remotePid);
353- RS_LOGE("RSRenderPipeline::AddConnection: token already exists");352+ if (iter != renderConnections_.end()) {
354- return;353+ RS_LOGE("RSRenderPipeline::AddConnection: pid %{public}d already exists", remotePid);
354+ return {iter->second.connection, iter->second.tokenMaskId};
355 }355 }
356- 356+ renderConnections_[remotePid] = {tokenMaskId, token, connectToRenderConnection};
357- if (tokenMaskIdMapTokens_.find(tokenMaskId) != tokenMaskIdMapTokens_.end()) {357+ return {connectToRenderConnection, tokenMaskId};
358- RS_LOGE("RSRenderPipeline::AddConnection: tokenMaskId already exists");
359- return;
360- }
361- renderConnections_[token] = {tokenMaskId, connectToRenderConnection};
362- tokenMaskIdMapTokens_[tokenMaskId] = token;
363- connectionProcessPid_[remotePid] = token;
364}358}
365 359 
366std::pair<sptr<RSIClientToRenderConnection>, uint64_t> RSRenderPipeline::FindClientToRenderConnection(360std::pair<sptr<RSIClientToRenderConnection>, uint64_t> RSRenderPipeline::FindClientToRenderConnection(
367 uint64_t remotePid)361 uint64_t remotePid)
368{362{
369 std::unique_lock<std::mutex> lock(renderConnectionMutex_);363 std::unique_lock<std::mutex> lock(renderConnectionMutex_);
370- 364+ auto iter = renderConnections_.find(static_cast<pid_t>(remotePid));
371- auto iter = connectionProcessPid_.find(remotePid);365+ if (iter == renderConnections_.end()) {
372- if (iter == connectionProcessPid_.end() || iter->second == nullptr) {
373 return {nullptr, INVALID_TOKEN_MASK_ID};366 return {nullptr, INVALID_TOKEN_MASK_ID};
374 }367 }
375- 368+ return {iter->second.connection, iter->second.tokenMaskId};
376- sptr<IRemoteObject> tmpToken = iter->second;
377- auto it = renderConnections_.find(tmpToken);
378- if (it != renderConnections_.end()) {
379- auto [tokenMaskId, clientToRenderConnection] = it->second;
380- return {clientToRenderConnection, tokenMaskId};
381- }
382- return {nullptr, INVALID_TOKEN_MASK_ID};
383}369}
384 370 
385void RSRenderPipeline::RegisterJudgeLppLayerCB(const sptr<IRSComposerToRenderConnection>& composerToRenderConn)371void RSRenderPipeline::RegisterJudgeLppLayerCB(const sptr<IRSComposerToRenderConnection>& composerToRenderConn)
@@ -121,7 +121,7 @@ private:
121 void InitUniRenderThread();121 void InitUniRenderThread();
122 void InitDumper(const std::shared_ptr<AppExecFwk::EventHandler>& handler);122 void InitDumper(const std::shared_ptr<AppExecFwk::EventHandler>& handler);
123 bool RemoveConnection(pid_t remotePid, const sptr<RSIConnectionToken>& token);123 bool RemoveConnection(pid_t remotePid, const sptr<RSIConnectionToken>& token);
124- void AddConnection(pid_t remotePid, uint64_t tokenMaskId,124+ std::pair<sptr<RSIClientToRenderConnection>, uint64_t> AddConnection(pid_t remotePid, uint64_t tokenMaskId,
125 sptr<IRemoteObject>& token, sptr<RSIClientToRenderConnection> connectToRenderConnection);125 sptr<IRemoteObject>& token, sptr<RSIClientToRenderConnection> connectToRenderConnection);
126 std::pair<sptr<RSIClientToRenderConnection>, uint64_t> FindClientToRenderConnection(uint64_t remotePid);126 std::pair<sptr<RSIClientToRenderConnection>, uint64_t> FindClientToRenderConnection(uint64_t remotePid);
127 void AddTransactionDataPidInfo(pid_t remotePid);127 void AddTransactionDataPidInfo(pid_t remotePid);
@@ -140,9 +140,12 @@ private:
140 void RegisterLayerStateChangedCB(const sptr<IRSComposerToRenderConnection>& composerToRenderConn);140 void RegisterLayerStateChangedCB(const sptr<IRSComposerToRenderConnection>& composerToRenderConn);
141 RSMainThread* mainThread_ = nullptr;141 RSMainThread* mainThread_ = nullptr;
142 RSUniRenderThread* uniRenderThread_ = nullptr;142 RSUniRenderThread* uniRenderThread_ = nullptr;
143- std::map<sptr<IRemoteObject>, std::pair<uint64_t, sptr<RSIClientToRenderConnection>>> renderConnections_ = {};143+ struct RenderConnectionInfo {
144- std::unordered_map<uint64_t, sptr<IRemoteObject>> tokenMaskIdMapTokens_ = {};144+ uint64_t tokenMaskId = INVALID_TOKEN_MASK_ID;
145- std::unordered_map<uint64_t, sptr<IRemoteObject>> connectionProcessPid_;145+ sptr<IRemoteObject> token;
146+ sptr<RSIClientToRenderConnection> connection;
147+ };
148+ std::unordered_map<pid_t, RenderConnectionInfo> renderConnections_;
146 mutable std::mutex renderConnectionMutex_;149 mutable std::mutex renderConnectionMutex_;
147 std::shared_ptr<ImageEnhanceManager> imageEnhanceManager_ = nullptr;150 std::shared_ptr<ImageEnhanceManager> imageEnhanceManager_ = nullptr;
148 std::shared_ptr<RSPipelineDumper> rpDumper_ = nullptr;151 std::shared_ptr<RSPipelineDumper> rpDumper_ = nullptr;
@@ -2532,14 +2532,14 @@ void RSRenderPipelineAgent::AddTransactionDataPidInfo(pid_t remotePid)
2532 pipeline->AddTransactionDataPidInfo(remotePid);2532 pipeline->AddTransactionDataPidInfo(remotePid);
2533}2533}
2534 2534 
2535-void RSRenderPipelineAgent::AddConnection(pid_t remotePid, uint64_t tokenMaskId,2535+std::pair<sptr<RSIClientToRenderConnection>, uint64_t> RSRenderPipelineAgent::AddConnection(pid_t remotePid,
2536- sptr<IRemoteObject>& token, sptr<RSIClientToRenderConnection> connectToRenderConnection)2536+ uint64_t tokenMaskId, sptr<IRemoteObject>& token, sptr<RSIClientToRenderConnection> connectToRenderConnection)
2537{2537{
2538 auto pipeline = rsRenderPipeline_.lock();2538 auto pipeline = rsRenderPipeline_.lock();
2539- if (!pipeline) {2539+ if (pipeline == nullptr) {
2540- return;2540+ return {nullptr, INVALID_TOKEN_MASK_ID};
2541 }2541 }
2542- pipeline->AddConnection(remotePid, tokenMaskId, token, connectToRenderConnection);2542+ return pipeline->AddConnection(remotePid, tokenMaskId, token, connectToRenderConnection);
2543}2543}
2544 2544 
2545std::pair<sptr<RSIClientToRenderConnection>, uint64_t> RSRenderPipelineAgent::FindClientToRenderConnection(2545std::pair<sptr<RSIClientToRenderConnection>, uint64_t> RSRenderPipelineAgent::FindClientToRenderConnection(
@@ -218,7 +218,7 @@ public:
218 sptr<IApplicationAgent> UnRegisterApplicationAgent(uint32_t pid);218 sptr<IApplicationAgent> UnRegisterApplicationAgent(uint32_t pid);
219 bool RemoveConnection(pid_t remotePid, const sptr<RSIConnectionToken>& token);219 bool RemoveConnection(pid_t remotePid, const sptr<RSIConnectionToken>& token);
220 void AddTransactionDataPidInfo(pid_t remotePid);220 void AddTransactionDataPidInfo(pid_t remotePid);
221- void AddConnection(pid_t remotePid, uint64_t tokenMaskId,221+ std::pair<sptr<RSIClientToRenderConnection>, uint64_t> AddConnection(pid_t remotePid, uint64_t tokenMaskId,
222 sptr<IRemoteObject>& token, sptr<RSIClientToRenderConnection> connectToRenderConnection);222 sptr<IRemoteObject>& token, sptr<RSIClientToRenderConnection> connectToRenderConnection);
223 void SetCacheEnabledForRotation(bool enabled);223 void SetCacheEnabledForRotation(bool enabled);
224 std::pair<sptr<RSIClientToRenderConnection>, uint64_t> FindClientToRenderConnection(uint64_t remotePid);224 std::pair<sptr<RSIClientToRenderConnection>, uint64_t> FindClientToRenderConnection(uint64_t remotePid);
@@ -33,15 +33,10 @@ std::pair<sptr<RSIClientToRenderConnection>, uint64_t> RSConnectToRenderProcess:
33 auto tokenObj = token->AsObject();33 auto tokenObj = token->AsObject();
34 pid_t remotePid = GetCallingPid();34 pid_t remotePid = GetCallingPid();
35 RS_PROFILER_ON_CREATE_CONNECTION(remotePid);35 RS_PROFILER_ON_CREATE_CONNECTION(remotePid);
36- auto [renderConnection, oldTokenMaskId] = renderPipelineAgent_->FindClientToRenderConnection(remotePid);
37- if (renderConnection != nullptr) {
38- return {renderConnection, INVALID_TOKEN_MASK_ID};
39- }
40 auto newRenderConn =36 auto newRenderConn =
41 sptr<RSClientToRenderConnection>::MakeSptr(remotePid, renderPipelineAgent_, tokenObj, needRefresh);37 sptr<RSClientToRenderConnection>::MakeSptr(remotePid, renderPipelineAgent_, tokenObj, needRefresh);
42 renderPipelineAgent_->AddTransactionDataPidInfo(remotePid);38 renderPipelineAgent_->AddTransactionDataPidInfo(remotePid);
43- renderPipelineAgent_->AddConnection(remotePid, tokenMaskId, tokenObj, newRenderConn);39+ return renderPipelineAgent_->AddConnection(remotePid, tokenMaskId, tokenObj, newRenderConn);
44- return {newRenderConn, INVALID_TOKEN_MASK_ID};
45}40}
46 41 
47std::pair<sptr<RSIClientToRenderConnection>, uint64_t> RSConnectToRenderProcess::FindClientToRenderConnection()42std::pair<sptr<RSIClientToRenderConnection>, uint64_t> RSConnectToRenderProcess::FindClientToRenderConnection()
@@ -152,8 +152,7 @@ sptr<RSIClientToRenderConnection> RSRenderServiceConnectHub::FindClientToRenderC
152 "tokenMaskId is not find ClientToRenderConnection");152 "tokenMaskId is not find ClientToRenderConnection");
153 return nullptr;153 return nullptr;
154 }154 }
155- auto renderProcessInfo = iter->second;155+ return iter->second.clientToRenderConnection;
156- return renderProcessInfo.clientToRenderConnection;
157}156}
158 157 
159sptr<RSIClientToRenderConnection> RSRenderServiceConnectHub::GetClientToRenderConnection(uint64_t tokenMaskId)158sptr<RSIClientToRenderConnection> RSRenderServiceConnectHub::GetClientToRenderConnection(uint64_t tokenMaskId)
@@ -1642,8 +1642,7 @@ void RSProfiler::DumpConnections(const ArgList& args)
1642 1642 
1643 std::string out;1643 std::string out;
1644 const std::lock_guard<std::mutex> guard(renderPipeline_->renderConnectionMutex_);1644 const std::lock_guard<std::mutex> guard(renderPipeline_->renderConnectionMutex_);
1645- for (const auto& [_, connection] : renderPipeline_->renderConnections_) {1645+ for (const auto& [pid, info] : renderPipeline_->renderConnections_) {
1646- const auto pid = GetConnectionPid(connection.second);
1647 const auto name = Utils::GetProcessName(pid);1646 const auto name = Utils::GetProcessName(pid);
1648 SendMessage("%s %d", !name.empty() ? name.data() : "<unknown>", pid);1647 SendMessage("%s %d", !name.empty() ? name.data() : "<unknown>", pid);
1649 }1648 }