File tree Expand file tree Collapse file tree 1 file changed +16
-2
lines changed Expand file tree Collapse file tree 1 file changed +16
-2
lines changed Original file line number Diff line number Diff line change @@ -148,7 +148,15 @@ void TcpServer::stop()
148
148
if (loop_->isInLoopThread ())
149
149
{
150
150
acceptorPtr_.reset ();
151
- for (auto connection : connSet_)
151
+ // copy the connSet_ to a vector, use the vector to close the
152
+ // connections to avoid the iterator invalidation.
153
+ std::vector<TcpConnectionPtr> connPtrs;
154
+ connPtrs.reserve (connSet_.size ());
155
+ for (auto &conn : connSet_)
156
+ {
157
+ connPtrs.push_back (conn);
158
+ }
159
+ for (auto connection : connPtrs)
152
160
{
153
161
connection->forceClose ();
154
162
}
@@ -159,7 +167,13 @@ void TcpServer::stop()
159
167
auto f = pro.get_future ();
160
168
loop_->queueInLoop ([this , &pro]() {
161
169
acceptorPtr_.reset ();
162
- for (auto connection : connSet_)
170
+ std::vector<TcpConnectionPtr> connPtrs;
171
+ connPtrs.reserve (connSet_.size ());
172
+ for (auto &conn : connSet_)
173
+ {
174
+ connPtrs.push_back (conn);
175
+ }
176
+ for (auto connection : connPtrs)
163
177
{
164
178
connection->forceClose ();
165
179
}
You can’t perform that action at this time.
0 commit comments