Skip to content

Commit

Permalink
Fix the EventPoller exit exception (ZLMediaKit#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
PioLing authored Aug 6, 2024
1 parent abf61ef commit a6e30e4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
32 changes: 17 additions & 15 deletions src/Poller/EventPoller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ EventPoller::~EventPoller() {
#endif

//退出前清理管道中的数据
onPipeEvent();
onPipeEvent(true);
InfoL << getThreadName();
}

Expand Down Expand Up @@ -276,22 +276,24 @@ bool EventPoller::isCurrentThread() {
return !_loop_thread || _loop_thread->get_id() == this_thread::get_id();
}

inline void EventPoller::onPipeEvent() {
inline void EventPoller::onPipeEvent(bool flush) {
char buf[1024];
int err = 0;
while (true) {
if ((err = _pipe.read(buf, sizeof(buf))) > 0) {
// 读到管道数据,继续读,直到读空为止
continue;
}
if (err == 0 || get_uv_error(true) != UV_EAGAIN) {
// 收到eof或非EAGAIN(无更多数据)错误,说明管道无效了,重新打开管道
ErrorL << "Invalid pipe fd of event poller, reopen it";
delEvent(_pipe.readFD());
_pipe.reOpen();
addEventPipe();
}
break;
if (!flush) {
for (;;) {
if ((err = _pipe.read(buf, sizeof(buf))) > 0) {
// 读到管道数据,继续读,直到读空为止
continue;
}
if (err == 0 || get_uv_error(true) != UV_EAGAIN) {
// 收到eof或非EAGAIN(无更多数据)错误,说明管道无效了,重新打开管道
ErrorL << "Invalid pipe fd of event poller, reopen it";
delEvent(_pipe.readFD());
_pipe.reOpen();
addEventPipe();
}
break;
}
}

decltype(_list_task) _list_swap;
Expand Down
2 changes: 1 addition & 1 deletion src/Poller/EventPoller.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class EventPoller : public TaskExecutor, public AnyStorage, public std::enable_s
/**
* 内部管道事件,用于唤醒轮询线程用
*/
void onPipeEvent();
void onPipeEvent(bool flush = false);

/**
* 切换线程并执行任务
Expand Down

0 comments on commit a6e30e4

Please sign in to comment.