Skip to content

Commit 62c9027

Browse files
authored
Fix socket fd leak if Connector destruct before connection callback is made (#230)
1 parent 77ed7a6 commit 62c9027

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

trantor/net/inner/Connector.cc

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ Connector::Connector(EventLoop *loop, InetAddress &&addr, bool retry)
2727
{
2828
}
2929

30+
Connector::~Connector()
31+
{
32+
if (socketHanded_ == false && fd_ != -1)
33+
{
34+
#ifndef _WIN32
35+
::close(fd_);
36+
#else
37+
closesocket(fd_);
38+
#endif
39+
}
40+
}
41+
3042
void Connector::start()
3143
{
3244
connect_ = true;
@@ -65,9 +77,10 @@ void Connector::startInLoop()
6577
}
6678
void Connector::connect()
6779
{
68-
int sockfd = Socket::createNonblockingSocketOrDie(serverAddr_.family());
80+
socketHanded_ = false;
81+
fd_ = Socket::createNonblockingSocketOrDie(serverAddr_.family());
6982
errno = 0;
70-
int ret = Socket::connect(sockfd, serverAddr_);
83+
int ret = Socket::connect(fd_, serverAddr_);
7184
int savedErrno = (ret == 0) ? 0 : errno;
7285
switch (savedErrno)
7386
{
@@ -76,7 +89,7 @@ void Connector::connect()
7689
case EINTR:
7790
case EISCONN:
7891
LOG_TRACE << "connecting";
79-
connecting(sockfd);
92+
connecting(fd_);
8093
break;
8194

8295
case EAGAIN:
@@ -86,7 +99,7 @@ void Connector::connect()
8699
case ENETUNREACH:
87100
if (retry_)
88101
{
89-
retry(sockfd);
102+
retry(fd_);
90103
}
91104
break;
92105

@@ -99,10 +112,11 @@ void Connector::connect()
99112
case ENOTSOCK:
100113
LOG_SYSERR << "connect error in Connector::startInLoop "
101114
<< savedErrno;
115+
socketHanded_ = true;
102116
#ifndef _WIN32
103-
::close(sockfd);
117+
::close(fd_);
104118
#else
105-
closesocket(sockfd);
119+
closesocket(fd_);
106120
#endif
107121
if (errorCallback_)
108122
errorCallback_();
@@ -111,10 +125,11 @@ void Connector::connect()
111125
default:
112126
LOG_SYSERR << "Unexpected error in Connector::startInLoop "
113127
<< savedErrno;
128+
socketHanded_ = true;
114129
#ifndef _WIN32
115-
::close(sockfd);
130+
::close(fd_);
116131
#else
117-
closesocket(sockfd);
132+
closesocket(fd_);
118133
#endif
119134
if (errorCallback_)
120135
errorCallback_();
@@ -154,6 +169,7 @@ int Connector::removeAndResetChannel()
154169

155170
void Connector::handleWrite()
156171
{
172+
socketHanded_ = true;
157173
if (status_ == Status::Connecting)
158174
{
159175
int sockfd = removeAndResetChannel();
@@ -168,6 +184,7 @@ void Connector::handleWrite()
168184
}
169185
else
170186
{
187+
socketHanded_ = true;
171188
#ifndef _WIN32
172189
::close(sockfd);
173190
#else
@@ -188,6 +205,7 @@ void Connector::handleWrite()
188205
}
189206
else
190207
{
208+
socketHanded_ = true;
191209
#ifndef _WIN32
192210
::close(sockfd);
193211
#else
@@ -208,6 +226,7 @@ void Connector::handleWrite()
208226
}
209227
else
210228
{
229+
socketHanded_ = true;
211230
#ifndef _WIN32
212231
::close(sockfd);
213232
#else
@@ -225,6 +244,7 @@ void Connector::handleWrite()
225244

226245
void Connector::handleError()
227246
{
247+
socketHanded_ = true;
228248
if (status_ == Status::Connecting)
229249
{
230250
status_ = Status::Disconnected;

trantor/net/inner/Connector.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Connector : public NonCopyable,
3030
using ConnectionErrorCallback = std::function<void()>;
3131
Connector(EventLoop *loop, const InetAddress &addr, bool retry = true);
3232
Connector(EventLoop *loop, InetAddress &&addr, bool retry = true);
33+
~Connector();
3334
void setNewConnectionCallback(const NewConnectionCallback &cb)
3435
{
3536
newConnectionCallback_ = cb;
@@ -76,6 +77,8 @@ class Connector : public NonCopyable,
7677
int maxRetryInterval_{kMaxRetryDelayMs};
7778

7879
bool retry_;
80+
bool socketHanded_{false};
81+
int fd_{-1};
7982

8083
void startInLoop();
8184
void connect();

0 commit comments

Comments
 (0)