Uploaded image for project: 'Drivers'
  1. Drivers
  2. DRIVERS-421

Cursor iteration should complete (abnormally) when another thread closes the cursor

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Component/s: None
    • Needed

      In order to make cursor iteration blocking for tailable cursors, and in general to support the possibility of empty batches, driver implementations of cursor iteration typically have an inner loop within the method that advances the cursor:

              while (serverCursor != null) {
                  _nextBatch = getMore();
                  if (!_nextBatch.isEmpty()) {
                      return true;  // has more results
                  }
              }
              return false;  // no more results
      

      Drivers should ensure that there is a check within that loop that the cursor has not been closed by another thread in the application.

              while (serverCursor != null) {
                 _nextBatch = getMore();
                  
                 // if another thread closed the cursor, throw an exception so that this method terminates
                 if (_closed) {
                     throw ...  
                 }
      
                 if (!_nextBatch.isEmpty()) {
                     return true;  // has more results
                 }
              }
              return false;  // no more results
      

      This is necessary because a call to killCursors from the close method of the cursor will fail if there is a getMore in progress on that cursor, and thus the repeated calls to getMore in the loop will never encounter a cursorNotFound error and the loop will not exit.

            Assignee:
            Unassigned Unassigned
            Reporter:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: