修复析构中调用getOwnerPoller抛异常导致崩溃的bug:#2117

This commit is contained in:
xiongziliang 2022-11-26 10:14:37 +08:00
parent 9fd5152aa1
commit 50fa671564
4 changed files with 22 additions and 17 deletions

@ -1 +1 @@
Subproject commit 617b6b1db23f13e2592b29204b84b1b9dbbf81c0 Subproject commit 25ba684a49142cc59e43719c98112031e1ec4bda

View File

@ -344,7 +344,8 @@ Value makeMediaSourceJson(MediaSource &media){
} }
//getLossRate有线程安全问题使用getMediaInfo接口才能获取丢包率getMediaList接口将忽略丢包率 //getLossRate有线程安全问题使用getMediaInfo接口才能获取丢包率getMediaList接口将忽略丢包率
auto current_thread = media.getOwnerPoller()->isCurrentThread(); auto current_thread = false;
try { current_thread = media.getOwnerPoller()->isCurrentThread();} catch (...) {}
float last_loss = -1; float last_loss = -1;
for(auto &track : media.getTracks(false)){ for(auto &track : media.getTracks(false)){
Value obj; Value obj;

View File

@ -124,7 +124,6 @@ MediaSource::MediaSource(const string &schema, const string &vhost, const string
_app = app; _app = app;
_stream_id = stream_id; _stream_id = stream_id;
_create_stamp = time(NULL); _create_stamp = time(NULL);
_default_poller = EventPollerPool::Instance().getPoller();
} }
MediaSource::~MediaSource() { MediaSource::~MediaSource() {
@ -289,23 +288,29 @@ toolkit::EventPoller::Ptr MediaSource::getOwnerPoller() {
if (listener) { if (listener) {
return listener->getOwnerPoller(*this); return listener->getOwnerPoller(*this);
} }
WarnL << toolkit::demangle(typeid(*this).name()) + "::getOwnerPoller failed, now return default poller: " + getUrl(); throw std::runtime_error(toolkit::demangle(typeid(*this).name()) + "::getOwnerPoller failed: " + getUrl());
return _default_poller;
} }
void MediaSource::onReaderChanged(int size) { void MediaSource::onReaderChanged(int size) {
weak_ptr<MediaSource> weak_self = shared_from_this(); try {
auto listener = _listener.lock(); weak_ptr<MediaSource> weak_self = shared_from_this();
if (!listener) { getOwnerPoller()->async([weak_self, size]() {
return; auto strong_self = weak_self.lock();
if (!strong_self) {
return;
}
auto listener = strong_self->_listener.lock();
if (listener) {
listener->onReaderChanged(*strong_self, size);
}
});
} catch (MediaSourceEvent::NotImplemented &ex) {
// 未实现接口,应该打印异常
WarnL << ex.what();
} catch (...) {
// getOwnerPoller()接口抛异常机制应该只对外不对内
// 所以listener已经销毁导致获取归属线程失败的异常直接忽略
} }
getOwnerPoller()->async([weak_self, size, listener]() {
auto strong_self = weak_self.lock();
if (!strong_self) {
return;
}
listener->onReaderChanged(*strong_self, size);
});
} }
bool MediaSource::setupRecord(Recorder::type type, bool start, const string &custom_path, size_t max_second){ bool MediaSource::setupRecord(Recorder::type type, bool start, const string &custom_path, size_t max_second){

View File

@ -406,7 +406,6 @@ private:
std::string _app; std::string _app;
std::string _stream_id; std::string _stream_id;
std::weak_ptr<MediaSourceEvent> _listener; std::weak_ptr<MediaSourceEvent> _listener;
toolkit::EventPoller::Ptr _default_poller;
// 对象个数统计 // 对象个数统计
toolkit::ObjectStatistic<MediaSource> _statistic; toolkit::ObjectStatistic<MediaSource> _statistic;
}; };