Skip to content

Commit efe4da2

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` each `addr_info` loop iteration. Closes #151
1 parent a0e6e03 commit efe4da2

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/Client/UnixStream.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ UnixStream::connect(const ConnectOptions &opts_arg)
169169
addr_info.last_error());
170170
int socket_errno = 0, connect_errno = 0;
171171
for (auto &inf: addr_info) {
172+
socket_errno = connect_errno = 0;
172173
fd = ::socket(inf.ai_family, inf.ai_socktype, inf.ai_protocol);
173174
if (fd < 0) {
174175
socket_errno = errno;
@@ -190,10 +191,7 @@ UnixStream::connect(const ConnectOptions &opts_arg)
190191
return US_TELL(SS_ESTABLISHED,
191192
"Connected", opts);
192193
} else if (errno == EINPROGRESS || errno == EAGAIN) {
193-
// TODO remove timeout and #include <poll.h>
194-
//return US_TELL(SS_CONNECT_PENDING,
195-
// "Connect pending", opts);
196-
194+
errno = 0;
197195
set_status(SS_CONNECT_PENDING);
198196
struct pollfd fds;
199197
fds.fd = fd;
@@ -207,7 +205,10 @@ UnixStream::connect(const ConnectOptions &opts_arg)
207205
}
208206
} while (errno == EINTR);
209207
close();
210-
connect_errno = errno;
208+
if (connect_errno == 0)
209+
connect_errno = errno;
210+
else
211+
assert(connect_errno == ETIMEDOUT && errno == 0);
211212
}
212213
if (connect_errno != 0)
213214
return US_DIE("Failed to connect", strerror(connect_errno));

0 commit comments

Comments
 (0)