Skip to content

Commit 1b58443

Browse files
committed
CDRIVER-826: Improve errno handling
We need to capture failure reasons on Windows properly too
1 parent 14eb5aa commit 1b58443

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-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>
@@ -2892,7 +2891,6 @@ _mongoc_cluster_sendv (mongoc_cluster_t *cluster,
28922891

28932892
iov = cluster->iov.data;
28942893
iovcnt = cluster->iov.len;
2895-
errno = 0;
28962894

28972895
BSON_ASSERT (cluster->iov.len);
28982896

src/mongoc/mongoc-socket.c

Lines changed: 19 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

@@ -265,6 +266,7 @@ _mongoc_socket_capture_errno (mongoc_socket_t *sock) /* IN */
265266
#else
266267
sock->errno_ = errno;
267268
#endif
269+
TRACE("setting errno: %d", sock->errno_);
268270
}
269271

270272

@@ -335,7 +337,6 @@ mongoc_socket_accept (mongoc_socket_t *sock, /* IN */
335337
sd = accept (sock->sd, &addr, &addrlen);
336338

337339
_mongoc_socket_capture_errno (sock);
338-
339340
#ifdef _WIN32
340341
failed = (sd == INVALID_SOCKET);
341342
#else
@@ -702,7 +703,6 @@ mongoc_socket_recv (mongoc_socket_t *sock, /* IN */
702703
{
703704
ssize_t ret = 0;
704705
bool failed = false;
705-
bool try_again = false;
706706

707707
ENTRY;
708708

@@ -721,11 +721,8 @@ mongoc_socket_recv (mongoc_socket_t *sock, /* IN */
721721
#endif
722722
if (failed) {
723723
_mongoc_socket_capture_errno (sock);
724-
}
725-
try_again = (failed && _mongoc_socket_errno_is_again (sock));
726-
727-
if (failed && try_again) {
728-
if (_mongoc_socket_wait (sock->sd, POLLIN, expire_at)) {
724+
if (_mongoc_socket_errno_is_again (sock) &&
725+
_mongoc_socket_wait (sock->sd, POLLIN, expire_at)) {
729726
GOTO (again);
730727
}
731728
}
@@ -928,27 +925,30 @@ _mongoc_socket_try_sendv (mongoc_socket_t *sock, /* IN */
928925
ret = sendmsg (sock->sd, &msg,
929926
# ifdef MSG_NOSIGNAL
930927
MSG_NOSIGNAL);
931-
#else
928+
# else
932929
0);
933930
# endif
934931
#endif
935932

936-
TRACE("Send %ld out of %ld bytes (errno=%d)", ret, iov->iov_len, errno);
937-
/*
938-
* Check to see if we have sent an iovec too large for sendmsg to
939-
* complete. If so, we need to fallback to the slow path of multiple
940-
* send() commands.
941-
*/
933+
TRACE("Send %ld out of %ld bytes", ret, iov->iov_len);
934+
935+
if (ret == -1) {
936+
_mongoc_socket_capture_errno (sock);
937+
938+
/*
939+
* Check to see if we have sent an iovec too large for sendmsg to
940+
* complete. If so, we need to fallback to the slow path of multiple
941+
* send() commands.
942+
*/
942943
#ifdef _WIN32
943-
if ((ret == -1) && (errno == WSAEMSGSIZE)) {
944+
if (errno == WSAEMSGSIZE) {
944945
#else
945-
if ((ret == -1) && (errno == EMSGSIZE)) {
946+
if (errno == EMSGSIZE) {
946947
#endif
947-
RETURN(_mongoc_socket_try_sendv_slow (sock, iov, iovcnt));
948+
RETURN(_mongoc_socket_try_sendv_slow (sock, iov, iovcnt));
949+
}
948950
}
949951

950-
_mongoc_socket_capture_errno (sock);
951-
952952
RETURN (ret);
953953
}
954954

0 commit comments

Comments
 (0)