Skip to content

Commit abd6526

Browse files
committed
CDRIVER-2007 crash creating index disconnected
1 parent 5a108cb commit abd6526

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

src/mongoc/mongoc-collection.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,10 @@ mongoc_collection_create_index_with_opts (mongoc_collection_t *collection,
13271327
server_stream = mongoc_cluster_stream_for_reads (
13281328
&collection->client->cluster, NULL, error);
13291329

1330+
if (!server_stream) {
1331+
GOTO (done);
1332+
}
1333+
13301334
if (opts && bson_iter_init (&iter, opts)) {
13311335
while (bson_iter_next (&iter)) {
13321336
if (BSON_ITER_IS_KEY (&iter, "writeConcern")) {

tests/test-mongoc-collection.c

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4431,14 +4431,12 @@ test_getmore_read_concern_live (void *ctx)
44314431
mongoc_write_concern_set_w (wc, MONGOC_WRITE_CONCERN_W_MAJORITY);
44324432
mongoc_collection_set_write_concern (collection, wc);
44334433

4434-
for (i=5000; i > 0; i--) {
4435-
mongoc_collection_insert (collection, MONGOC_INSERT_NONE, tmp_bson
4436-
("{'a': 1}"), NULL, NULL);
4434+
for (i = 5000; i > 0; i--) {
4435+
mongoc_collection_insert (
4436+
collection, MONGOC_INSERT_NONE, tmp_bson ("{'a': 1}"), NULL, NULL);
44374437
}
4438-
cursor = mongoc_collection_find_with_opts (collection,
4439-
tmp_bson ("{}"),
4440-
NULL,
4441-
NULL);
4438+
cursor = mongoc_collection_find_with_opts (
4439+
collection, tmp_bson ("{}"), NULL, NULL);
44424440

44434441
while (mongoc_cursor_next (cursor, &doc)) {
44444442
i++;
@@ -4455,7 +4453,6 @@ test_getmore_read_concern_live (void *ctx)
44554453
}
44564454

44574455

4458-
44594456
static void
44604457
test_aggregate_read_concern (void)
44614458
{
@@ -4737,6 +4734,33 @@ test_insert_duplicate_key (void)
47374734
mongoc_client_destroy (client);
47384735
}
47394736

4737+
static void
4738+
test_create_index_fail (void)
4739+
{
4740+
mongoc_client_t *client;
4741+
mongoc_collection_t *collection;
4742+
bool r;
4743+
bson_t reply;
4744+
bson_error_t error;
4745+
4746+
client = mongoc_client_new ("mongodb://example.com/?connectTimeoutMS=10");
4747+
collection = mongoc_client_get_collection (client, "test", "test");
4748+
r = mongoc_collection_create_index_with_opts (
4749+
collection, tmp_bson ("{'a': 1}"), NULL, NULL, &reply, &error);
4750+
4751+
ASSERT (!r);
4752+
ASSERT_ERROR_CONTAINS (error,
4753+
MONGOC_ERROR_SERVER_SELECTION,
4754+
MONGOC_ERROR_SERVER_SELECTION_FAILURE,
4755+
"connection timeout");
4756+
4757+
/* reply was initialized */
4758+
ASSERT (bson_empty (&reply));
4759+
4760+
mongoc_collection_destroy (collection);
4761+
mongoc_client_destroy (client);
4762+
}
4763+
47404764
void
47414765
test_collection_install (TestSuite *suite)
47424766
{
@@ -4882,9 +4906,12 @@ test_collection_install (TestSuite *suite)
48824906
TestSuite_Add (suite, "/Collection/stats/read_pref", test_stats_read_pref);
48834907
TestSuite_Add (
48844908
suite, "/Collection/find_read_concern", test_find_read_concern);
4885-
TestSuite_AddFull (
4886-
suite, "/Collection/getmore_read_concern_live",
4887-
test_getmore_read_concern_live, NULL, NULL, test_framework_skip_if_max_wire_version_less_than_4);
4909+
TestSuite_AddFull (suite,
4910+
"/Collection/getmore_read_concern_live",
4911+
test_getmore_read_concern_live,
4912+
NULL,
4913+
NULL,
4914+
test_framework_skip_if_max_wire_version_less_than_4);
48884915
TestSuite_AddLive (
48894916
suite, "/Collection/find_and_modify", test_find_and_modify);
48904917
TestSuite_Add (suite,
@@ -4913,4 +4940,6 @@ test_collection_install (TestSuite *suite)
49134940
suite, "/Collection/find_indexes/error", test_find_indexes_err);
49144941
TestSuite_AddLive (
49154942
suite, "/Collection/insert/duplicate_key", test_insert_duplicate_key);
4943+
TestSuite_Add (
4944+
suite, "/Collection/create_index/fail", test_create_index_fail);
49164945
}

0 commit comments

Comments
 (0)