Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TCPServer::Stop race condition bug #26

Closed
zieckey opened this issue Apr 9, 2017 · 1 comment
Closed

TCPServer::Stop race condition bug #26

zieckey opened this issue Apr 9, 2017 · 1 comment

Comments

@zieckey
Copy link
Collaborator

zieckey commented Apr 9, 2017

 77 void TCPServer::StopInLoop() {
 78     LOG_TRACE << "Entering TCPServer::StopInLoop";
 79     listener_->Stop();
 80     listener_.reset();
 81 
 82     for (auto& c : connections_) {
 83         c.second->Close();
 84     }
 85 
 86     tpool_->Stop(true);
 87     assert(tpool_->IsStopped());
 88     LOG_TRACE << "TCPServer::StopInLoop exited";
 89 }

上述83、86两行,都会调用到工作线程的EventLoop::QueueInLoop函数,也就是至少会调用两次。设想以下场景:

  1. time 1:当前线程中,83行调用完毕,工作线程正好唤醒,执行到EventLoop::DoPendingFunctors函数中,
  2. time 2:当前线程执行到86行,EventLoop::QueueInLoop函数中,刚把一个函数对象放到队列中,但还没有来得及调用watcher_.Notify()
  3. time 3:工作线程这个时候,刚好将上述两个函数对象执行完毕。然后第二函数对象是EventLoop::StopInLoop,刚刚好也执行完毕,会将EventLoop::watcher_对象析构
  4. time 4:当前线程接着执行watcher_.Notify()。由于上面第三个时间点EventLoop::watcher_对象已经析构,最终导致程序崩溃。

这个场景可以用“testTCPServer1”来重现,可能需要运行很多次才能重现。

HTTPServer有类似问题,需要一起修复。

@zieckey
Copy link
Collaborator Author

zieckey commented Apr 10, 2017

Fix by commit a421752

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant