Skip to content

Commit 82a6106

Browse files
authored
Fix a race condition of TimingWheel class (#80)
1 parent ba5b99f commit 82a6106

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

trantor/net/TcpServer.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,16 @@ void TcpServer::stop()
150150
connection->forceClose();
151151
}
152152
loopPoolPtr_.reset();
153-
for (auto iter : timingWheelMap_)
153+
for (auto &iter : timingWheelMap_)
154154
{
155-
iter.second.reset();
155+
std::promise<int> pro;
156+
auto f = pro.get_future();
157+
iter.second->getLoop()->runInLoop([&iter, &pro]() mutable {
158+
iter.second.reset();
159+
pro.set_value(1);
160+
});
161+
f.get();
156162
}
157-
timingWheelMap_.clear();
158163
}
159164
void TcpServer::connectionClosed(const TcpConnectionPtr &connectionPtr)
160165
{

trantor/utils/TimingWheel.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ TimingWheel::TimingWheel(trantor::EventLoop *loop,
6464

6565
TimingWheel::~TimingWheel()
6666
{
67+
loop_->assertInLoopThread();
6768
loop_->invalidateTimer(timerId_);
6869
for (auto iter = wheels_.rbegin(); iter != wheels_.rend(); ++iter)
6970
{

0 commit comments

Comments
 (0)