bug-fix: rtpserver死锁 (#4421)
Some checks are pending
Android / build (push) Waiting to run
CodeQL / Analyze (cpp) (push) Waiting to run
CodeQL / Analyze (javascript) (push) Waiting to run
Docker / build (push) Waiting to run
Linux / build (push) Waiting to run
macOS / build (push) Waiting to run
Windows / build (push) Waiting to run

1、RtpServer对应的socket在读取数据后,会加锁后进行数据处理
  a) LOCK_GUARD(_mtx_event);
  b) _on_multi_read
当_on_multi_read处理中出现问题,会通过回调,调用 s_rtp_server.erase(key); ,
这里锁的调用顺序为:_mtx_event -> s_rtp_server._mtx
2、当外部调用api关闭RtpServer时,会先调用 s_rtp_server.erase(key);
,释放RtpServer对象时,会调用Socket的 setOnRead(nullptr),这里会调用Socket的 _mtx_event。
这里的锁调用顺序为:s_rtp_server._mtx -> _mtx_event
以上两种情况,存在 交叉调用两把锁的问题,会出现死锁。
This commit is contained in:
yingxiaodong 2025-09-01 11:35:50 +08:00 committed by GitHub
parent 41a71f7994
commit 2faa04da30
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -330,8 +330,17 @@ public:
}
size_t erase(const std::string &key) {
std::lock_guard<std::recursive_mutex> lck(_mtx);
return _map.erase(key);
Pointer erase_ptr;
{
std::lock_guard<std::recursive_mutex> lck(_mtx);
auto itr = _map.find(key);
if (itr != _map.end()) {
erase_ptr = itr->second;
_map.erase(itr);
return 1;
}
}
return 0;
}
size_t size() {