Skip to content

Commit bef2b38

Browse files
committed
Merge pull request #278 from ksuarz/CDRIVER-880
CDRIVER-880: aggregation may return destroyed cursor
2 parents 3560d8d + e634924 commit bef2b38

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/mongoc/mongoc-collection.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,9 @@ mongoc_collection_aggregate (mongoc_collection_t *collection, /* IN */
305305
_mongoc_cursor_cursorid_init(cursor);
306306
cursor->limit = 0;
307307

308-
if (! _mongoc_cursor_cursorid_prime (cursor)) {
309-
mongoc_cursor_destroy (cursor);
310-
}
308+
/* we always return the cursor, even if it fails; users can detect the failure on performing
309+
* a cursor operation. see CDRIVER-880. */
310+
_mongoc_cursor_cursorid_prime (cursor);
311311
} else {
312312
/* for older versions we get an array that we can create a synthetic
313313
* cursor on top of */

tests/test-mongoc-collection.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <bcon.h>
22
#include <mongoc.h>
33
#include <mongoc-client-private.h>
4+
#include <mongoc-cursor-private.h>
45

56
#include "TestSuite.h"
67

@@ -1298,6 +1299,7 @@ test_aggregate (void)
12981299
bool r;
12991300
bson_t opts;
13001301
bson_t *pipeline;
1302+
bson_t *broken_pipeline;
13011303
bson_t *b;
13021304
bson_iter_t iter;
13031305
int i, j;
@@ -1312,6 +1314,7 @@ test_aggregate (void)
13121314
ASSERT (collection);
13131315

13141316
pipeline = BCON_NEW ("pipeline", "[", "{", "$match", "{", "hello", BCON_UTF8 ("world"), "}", "}", "]");
1317+
broken_pipeline = BCON_NEW ("pipeline", "[", "{", "$asdf", "{", "foo", BCON_UTF8 ("bar"), "}", "}", "]");
13151318
b = BCON_NEW ("hello", BCON_UTF8 ("world"));
13161319

13171320
again:
@@ -1323,6 +1326,15 @@ test_aggregate (void)
13231326
MONGOC_INSERT_NONE, b, NULL, &error), error);
13241327
}
13251328

1329+
cursor = mongoc_collection_aggregate (collection, MONGOC_QUERY_NONE, broken_pipeline, NULL, NULL);
1330+
ASSERT (cursor);
1331+
1332+
r = mongoc_cursor_next (cursor, &doc);
1333+
ASSERT (!r);
1334+
ASSERT (mongoc_cursor_error (cursor, &error));
1335+
ASSERT (cursor->failed);
1336+
ASSERT (error.code == 16436);
1337+
13261338
for (i = 0; i < 2; i++) {
13271339
if (i % 2 == 0) {
13281340
cursor = mongoc_collection_aggregate(collection, MONGOC_QUERY_NONE, pipeline, NULL, NULL);
@@ -1385,6 +1397,7 @@ test_aggregate (void)
13851397
mongoc_client_destroy(client);
13861398
bson_destroy(b);
13871399
bson_destroy(pipeline);
1400+
bson_destroy (broken_pipeline);
13881401
}
13891402

13901403

0 commit comments

Comments
 (0)