Skip to content

Commit c35aea0

Browse files
committed
CDRIVER-695 checked errors in cluster logic
Hope to make a crash in _mongoc_cluster_node_destroy easier to diagnose.
1 parent 8dc4e7d commit c35aea0

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

src/mongoc/mongoc-cluster-private.h

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ typedef struct
7979
int32_t max_write_batch_size;
8080
char *replSet;
8181
int64_t last_read_msec;
82+
bool valid;
8283
} mongoc_cluster_node_t;
8384

8485

src/mongoc/mongoc-cluster.c

+13-5
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ _mongoc_cluster_node_init (mongoc_cluster_node_t *node)
250250
bson_init(&node->tags);
251251
node->primary = 0;
252252
node->needs_auth = 0;
253+
node->valid = true;
253254

254255
EXIT;
255256
}
@@ -318,7 +319,8 @@ _mongoc_cluster_node_destroy (mongoc_cluster_node_t *node)
318319
{
319320
ENTRY;
320321

321-
BSON_ASSERT(node);
322+
ALWAYS_ASSERT(node);
323+
ALWAYS_ASSERT(node->valid);
322324

323325
if (node->stream) {
324326
mongoc_stream_close(node->stream);
@@ -566,7 +568,7 @@ _mongoc_cluster_destroy (mongoc_cluster_t *cluster) /* INOUT */
566568

567569
ENTRY;
568570

569-
bson_return_if_fail (cluster);
571+
ALWAYS_ASSERT (cluster);
570572

571573
mongoc_uri_destroy (cluster->uri);
572574

@@ -1052,9 +1054,9 @@ _mongoc_cluster_ismaster (mongoc_cluster_t *cluster,
10521054

10531055
ENTRY;
10541056

1055-
BSON_ASSERT(cluster);
1056-
BSON_ASSERT(node);
1057-
BSON_ASSERT(node->stream);
1057+
ALWAYS_ASSERT(cluster);
1058+
ALWAYS_ASSERT(node);
1059+
ALWAYS_ASSERT(node->stream);
10581060

10591061
bson_init(&command);
10601062
bson_append_int32(&command, "isMaster", 8, 1);
@@ -2204,6 +2206,11 @@ _mongoc_cluster_reconnect_replica_set (mongoc_cluster_t *cluster,
22042206
cluster->nodes = bson_realloc (cluster->nodes, sizeof (*cluster->nodes) * i);
22052207
cluster->nodes_len = i;
22062208

2209+
/* guard against counter errors, see CDRIVER-695 */
2210+
for (i = 0; i < cluster->nodes_len; i++) {
2211+
cluster->nodes[i].valid = false;
2212+
}
2213+
22072214
for (liter = list, i = 0; liter; liter = liter->next) {
22082215
if (!_mongoc_host_list_from_string(&host, liter->data)) {
22092216
MONGOC_WARNING("Failed to parse host and port: \"%s\"",
@@ -2276,6 +2283,7 @@ _mongoc_cluster_reconnect_replica_set (mongoc_cluster_t *cluster,
22762283

22772284
_mongoc_cluster_node_track_ping(&cluster->nodes[i], ping);
22782285

2286+
cluster->nodes[i].valid = true;
22792287
i++;
22802288
}
22812289

src/mongoc/mongoc-stream.c

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "mongoc-stream.h"
2828
#include "mongoc-stream-private.h"
2929
#include "mongoc-trace.h"
30+
#include "mongoc-util-private.h"
3031

3132

3233
#undef MONGOC_LOG_DOMAIN
@@ -54,6 +55,8 @@ mongoc_stream_close (mongoc_stream_t *stream)
5455

5556
bson_return_val_if_fail(stream, -1);
5657

58+
ALWAYS_ASSERT(stream->close);
59+
5760
ret = stream->close(stream);
5861

5962
RETURN (ret);
@@ -74,6 +77,8 @@ mongoc_stream_destroy (mongoc_stream_t *stream)
7477

7578
bson_return_if_fail(stream);
7679

80+
ALWAYS_ASSERT(stream->destroy);
81+
7782
stream->destroy(stream);
7883

7984
EXIT;

src/mongoc/mongoc-util-private.h

+9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@
2323

2424
#include <bson.h>
2525

26+
/* like assert, but for production builds too */
27+
#define ALWAYS_ASSERT(s) \
28+
do { \
29+
if (!(s)) { \
30+
fprintf (stderr, "precondition \"%s\" failed %s:%d: %s()\n", \
31+
#s, __FILE__, __LINE__, __FUNCTION__); \
32+
abort (); \
33+
} \
34+
} while (0)
2635

2736
BSON_BEGIN_DECLS
2837

0 commit comments

Comments
 (0)