When phongo_execute_command() calls phongo_advance_cursor_and_check_for_error() after constructing a command cursor from the envelope response, the ensuing mongoc_cursor_next() is prone to causing a getMore if the firstBatch in the original response is empty. This can be seen with traditional command cursors (e.g. aggregate) and tailable command cursors (e.g. aggregate with $changeStream), although the issue is more pronounced with tailable command cursors as the first getMore may block for one second (default await time).
The phongo_advance_cursor_and_check_for_error() call was introduced in 18e2e2d for PHPC-602. Looking at the history of that commit, it appears a call to next() is necessary to advance the cursor to the first document in the command cursor reply. Removing this call causes several tests to fail, as the cursor is never advanced to its first element and the valid iteration handler likely returns false. We should investigate if it's possible to delay this advance to the cursor's rewind handler.
Today, it's not actually necessary to call the rewind handler to iterate a cursor (demonstrated if we wrap a Cursor with an IteratorIterator and manually iterate). We should also see if that behavior can be preserved.
- is depended on by
-
PHPLIB-298 Do not rewind command cursor in ChangeStream constructor
- Closed
- is related to
-
PHPLIB-276 Change stream support
- Closed
-
PHPC-602 Create command cursors with mongoc_cursor_new_from_command_document()
- Closed
- related to
-
PHPLIB-451 Rewinding change stream cursor should never execute a getMore command
- Closed
-
PHPC-1344 Make command cursor getMore tests less fragile
- Backlog