Skip to content

Commit 59d0ba7

Browse files
authored
Fix a bug when closing connections on Windows/MacOS (#202)
1 parent d489cce commit 59d0ba7

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

trantor/net/TcpServer.cc

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -162,30 +162,38 @@ void TcpServer::stop()
162162
f.get();
163163
}
164164
}
165-
void TcpServer::connectionClosed(const TcpConnectionPtr &connectionPtr)
165+
void TcpServer::handleCloseInLoop(const TcpConnectionPtr &connectionPtr)
166166
{
167-
LOG_TRACE << "connectionClosed";
168-
if (loop_->isInLoopThread())
167+
size_t n = connSet_.erase(connectionPtr);
168+
(void)n;
169+
assert(n == 1);
170+
auto connLoop = connectionPtr->getLoop();
171+
if (connLoop == loop_)
169172
{
170-
size_t n = connSet_.erase(connectionPtr);
171-
(void)n;
172-
assert(n == 1);
173-
loop_->queueInLoop([connectionPtr]() {
174-
static_cast<TcpConnectionImpl *>(connectionPtr.get())
175-
->connectDestroyed();
176-
});
173+
static_cast<TcpConnectionImpl *>(connectionPtr.get())
174+
->connectDestroyed();
177175
}
178176
else
179177
{
180-
loop_->queueInLoop([this, connectionPtr]() {
181-
size_t n = connSet_.erase(connectionPtr);
182-
(void)n;
183-
assert(n == 1);
178+
connLoop->queueInLoop([connectionPtr]() {
184179
static_cast<TcpConnectionImpl *>(connectionPtr.get())
185180
->connectDestroyed();
186181
});
187182
}
188183
}
184+
void TcpServer::connectionClosed(const TcpConnectionPtr &connectionPtr)
185+
{
186+
LOG_TRACE << "connectionClosed";
187+
if (loop_->isInLoopThread())
188+
{
189+
handleCloseInLoop(connectionPtr);
190+
}
191+
else
192+
{
193+
loop_->queueInLoop(
194+
[this, connectionPtr]() { handleCloseInLoop(connectionPtr); });
195+
}
196+
}
189197

190198
const std::string TcpServer::ipPort() const
191199
{

trantor/net/TcpServer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ class TRANTOR_EXPORT TcpServer : NonCopyable
215215

216216
private:
217217
EventLoop *loop_;
218+
void handleCloseInLoop(const TcpConnectionPtr &connectionPtr);
218219
std::unique_ptr<Acceptor> acceptorPtr_;
219220
void newConnection(int fd, const InetAddress &peer);
220221
std::string serverName_;

0 commit comments

Comments
 (0)