Skip to content

Commit f6e2dc6

Browse files
committed
CDRIVER-826: Improve errno handling
We need to capture failure reasons on Windows properly too
1 parent 7f4b946 commit f6e2dc6

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

src/mongoc/mongoc-buffer.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717

1818
#include <bson.h>
19-
#include <errno.h>
2019
#include <stdarg.h>
2120

2221
#include "mongoc-error.h"

src/mongoc/mongoc-cluster.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
#include "mongoc-config.h"
1919

20-
#include <errno.h>
2120
#ifdef MONGOC_ENABLE_SASL
2221
#include <sasl/sasl.h>
2322
#include <sasl/saslutil.h>
@@ -2236,7 +2235,6 @@ mongoc_cluster_sendv_to_server (mongoc_cluster_t *cluster,
22362235

22372236
iov = (mongoc_iovec_t *)cluster->iov.data;
22382237
iovcnt = cluster->iov.len;
2239-
errno = 0;
22402238

22412239
BSON_ASSERT (cluster->iov.len);
22422240

src/mongoc/mongoc-socket.c

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ _mongoc_socket_wait (int sd, /* IN */
167167
} else if (ret < 0) {
168168
/* poll itself failed */
169169

170+
TRACE("errno is: %d", errno);
170171
if (MONGOC_ERRNO_IS_AGAIN(errno)) {
171172
now = bson_get_monotonic_time();
172173

@@ -342,6 +343,7 @@ _mongoc_socket_capture_errno (mongoc_socket_t *sock) /* IN */
342343
#else
343344
sock->errno_ = errno;
344345
#endif
346+
TRACE("setting errno: %d", sock->errno_);
345347
}
346348

347349

@@ -365,6 +367,7 @@ _mongoc_socket_capture_errno (mongoc_socket_t *sock) /* IN */
365367
static bool
366368
_mongoc_socket_errno_is_again (mongoc_socket_t *sock) /* IN */
367369
{
370+
TRACE("errno is: %d", sock->errno_);
368371
return MONGOC_ERRNO_IS_AGAIN (sock->errno_);
369372
}
370373

@@ -437,7 +440,6 @@ mongoc_socket_accept_ex (mongoc_socket_t *sock, /* IN */
437440
sd = accept (sock->sd, (struct sockaddr *) &addr, &addrlen);
438441

439442
_mongoc_socket_capture_errno (sock);
440-
441443
#ifdef _WIN32
442444
failed = (sd == INVALID_SOCKET);
443445
#else
@@ -801,7 +803,6 @@ mongoc_socket_recv (mongoc_socket_t *sock, /* IN */
801803
{
802804
ssize_t ret = 0;
803805
bool failed = false;
804-
bool try_again = false;
805806

806807
ENTRY;
807808

@@ -820,11 +821,8 @@ mongoc_socket_recv (mongoc_socket_t *sock, /* IN */
820821
#endif
821822
if (failed) {
822823
_mongoc_socket_capture_errno (sock);
823-
}
824-
try_again = (failed && _mongoc_socket_errno_is_again (sock));
825-
826-
if (failed && try_again) {
827-
if (_mongoc_socket_wait (sock->sd, POLLIN, expire_at)) {
824+
if (_mongoc_socket_errno_is_again (sock) &&
825+
_mongoc_socket_wait (sock->sd, POLLIN, expire_at)) {
828826
GOTO (again);
829827
}
830828
}
@@ -1025,27 +1023,30 @@ _mongoc_socket_try_sendv (mongoc_socket_t *sock, /* IN */
10251023
ret = sendmsg (sock->sd, &msg,
10261024
# ifdef MSG_NOSIGNAL
10271025
MSG_NOSIGNAL);
1028-
#else
1026+
# else
10291027
0);
10301028
# endif
10311029
#endif
10321030

1033-
TRACE("Send %ld out of %ld bytes (errno=%d)", ret, iov->iov_len, errno);
1034-
/*
1035-
* Check to see if we have sent an iovec too large for sendmsg to
1036-
* complete. If so, we need to fallback to the slow path of multiple
1037-
* send() commands.
1038-
*/
1031+
TRACE("Send %ld out of %ld bytes", ret, iov->iov_len);
1032+
1033+
if (ret == -1) {
1034+
_mongoc_socket_capture_errno (sock);
1035+
1036+
/*
1037+
* Check to see if we have sent an iovec too large for sendmsg to
1038+
* complete. If so, we need to fallback to the slow path of multiple
1039+
* send() commands.
1040+
*/
10391041
#ifdef _WIN32
1040-
if ((ret == -1) && (errno == WSAEMSGSIZE)) {
1042+
if (errno == WSAEMSGSIZE) {
10411043
#else
1042-
if ((ret == -1) && (errno == EMSGSIZE)) {
1044+
if (errno == EMSGSIZE) {
10431045
#endif
1044-
RETURN(_mongoc_socket_try_sendv_slow (sock, iov, iovcnt));
1046+
RETURN(_mongoc_socket_try_sendv_slow (sock, iov, iovcnt));
1047+
}
10451048
}
10461049

1047-
_mongoc_socket_capture_errno (sock);
1048-
10491050
RETURN (ret);
10501051
}
10511052

0 commit comments

Comments
 (0)