Skip to content

Commit d08a676

Browse files
client: fix connect error handling
Currently, if we timeout waiting for `connect` using `poll`, we set `connect_errno` to `ETIMEOUT`. However, after we exit the `connect` interruption loop, we unconditionally set to `errno`, which we do not even clear after checking the `connect` result. As a result, the timeout error gets masked by the in-progress connection error. To fix this, let's: 1. Clear `errno` after checking the `connect` result; 2. Check the value of `connect_errno` after exiting the `connect` interrupt loop before setting it to `errno`; 3. Zero out `socket_errno`, `connect_errno` and `errno` each `addr_info` loop iteration. Closes #151
1 parent 0465dee commit d08a676

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/Client/UnixStream.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,9 @@ UnixStream::connect(const ConnectOptions &opts_arg)
169169
if (addr_info.last_rc() != 0)
170170
return US_DIE("Network address resolve failed",
171171
addr_info.last_error());
172-
int socket_errno = 0, connect_errno = 0;
172+
int socket_errno, connect_errno;
173173
for (auto &inf: addr_info) {
174+
socket_errno = connect_errno = errno = 0;
174175
fd = ::socket(inf.ai_family, inf.ai_socktype, inf.ai_protocol);
175176
if (fd < 0) {
176177
socket_errno = errno;
@@ -192,10 +193,7 @@ UnixStream::connect(const ConnectOptions &opts_arg)
192193
return US_TELL(SS_ESTABLISHED,
193194
"Connected", opts);
194195
} else if (errno == EINPROGRESS || errno == EAGAIN) {
195-
// TODO remove timeout and #include <poll.h>
196-
//return US_TELL(SS_CONNECT_PENDING,
197-
// "Connect pending", opts);
198-
196+
errno = 0;
199197
set_status(SS_CONNECT_PENDING);
200198
struct pollfd fds;
201199
fds.fd = fd;
@@ -209,7 +207,10 @@ UnixStream::connect(const ConnectOptions &opts_arg)
209207
}
210208
} while (errno == EINTR);
211209
close();
212-
connect_errno = errno;
210+
if (connect_errno == 0)
211+
connect_errno = errno;
212+
else
213+
assert(connect_errno == ETIMEDOUT && errno == 0);
213214
}
214215
if (connect_errno != 0)
215216
return US_DIE("Failed to connect", strerror(connect_errno));

0 commit comments

Comments
 (0)