[CDRIVER-2093] mongoc_cursor_is_alive and mongoc_cursor_more incorrect querying an empty collection Created: 22/Mar/17  Updated: 03/May/17  Resolved: 29/Mar/17

Status: Closed
Project: C Driver
Component/s: libmongoc
Affects Version/s: 1.6.0
Fix Version/s: 1.7.0

Type: Bug Priority: Major - P3
Reporter: David Golden Assignee: David Golden
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File demo.c    
Issue Links:
Depends
is depended on by CXX-1258 Cursor iterators lose lockstep at cur... Closed

 Description   

On a server using find commands, a query on an empty collection returns a cursor that returns incorrect values from mongoc_cursor_is_alive and mongoc_cursor_more after successive calls to mongoc_cursor_next.

The attached code has a loop like this:

for ( int i=1; !mongoc_cursor_error(cursor, NULL); i++ ) {
    printf("Loop iteration %d\n", i);
 
    printf("mongoc_cursor_is_alive: %s\n", ( mongoc_cursor_is_alive(cursor) ? "true" : "false" ));
    printf("mongoc_cursor_more:     %s\n", ( mongoc_cursor_more(cursor) ? "true" : "false" ));
    printf("mongoc_cursor_next:     %s\n", ( mongoc_cursor_next(cursor, &doc) ? "valid" : "null" ));
    printf("mongoc_cursor_error:    '%s'\n", ( mongoc_cursor_error(cursor, &error) ? error.message : "" ));
 
    printf("\n");
}

On a 2.6 mongod, the (correct) results are as follows:

Loop iteration 1
mongoc_cursor_is_alive: true
mongoc_cursor_more:     true
mongoc_cursor_next:     null
mongoc_cursor_error:    ''
 
Loop iteration 2
mongoc_cursor_is_alive: false
mongoc_cursor_more:     false
mongoc_cursor_next:     null
mongoc_cursor_error:    'Cannot advance a completed or failed cursor.'

Note that "is_alive" and "more" are true before the first call to "next". The call to "next" returns null, indicating exhaustion. In the second loop iteration, both "is_alive and "more" are false, indicating that the user should not call "next". When "next" is called, an error occurs.

Now look at the output of the same loop on mongod 3.2:

Loop iteration 1
mongoc_cursor_is_alive: true
mongoc_cursor_more:     true
mongoc_cursor_next:     null
mongoc_cursor_error:    ''
 
Loop iteration 2
mongoc_cursor_is_alive: true
mongoc_cursor_more:     true
mongoc_cursor_next:     null
mongoc_cursor_error:    ''
 
Loop iteration 3
mongoc_cursor_is_alive: false
mongoc_cursor_more:     true
mongoc_cursor_next:     null
mongoc_cursor_error:    'Cannot advance a completed or failed cursor.'

In this case, even though iteration 1 had "next" return null, loop iteration 2 has "is_alive" and "more" true and the second call to "next" does not return an error. Iteration 3 finally has "is_alive" false, but oddly, still has "more" true. This time, the call to "next" returns an error.

I believe the mongoc_cursor_is_alive error is caused at least in part by these lines, which reset cursor->done because the empty response from the execution of the find command sets done but not end_of_event (whatever that is).

I believe the further problems of mongoc_cursor_more are also caused by end_of_event not being set. mongoc-cursor-cursorid.c does not provide a more function, causing mongoc_cursor_more to fall back to _mongoc_cursor_more, which checks end_of_event.



 Comments   
Comment by Githook User [ 27/Mar/17 ]

Author:

{u'username': u'ajdavis', u'name': u'A. Jesse Jiryu Davis', u'email': u'jesse@emptysquare.net'}

Message: Merge pull request #430 from xdg/CDRIVER-2093

CDRIVER-2093 fix cursor is_alive and more
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/5b24f894d460c5fa3052d4436ac1d84be14f14b4

Comment by Githook User [ 27/Mar/17 ]

Author:

{u'username': u'ajdavis', u'name': u'A. Jesse Jiryu Davis', u'email': u'jesse@emptysquare.net'}

Message: Merge pull request #430 from xdg/CDRIVER-2093

CDRIVER-2093 fix cursor is_alive and more
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/5b24f894d460c5fa3052d4436ac1d84be14f14b4

Comment by Githook User [ 27/Mar/17 ]

Author:

{u'username': u'xdg', u'name': u'David Golden', u'email': u'xdg@xdg.me'}

Message: CDRIVER-2093 fix cursor is_alive and more
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/1fd9a1c39203ad6f7b5793d30751b4fd820fa02b

Generated at Wed Feb 07 21:14:09 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.