Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-5187

driver does not close cursor when `initialize` returns a single document

    • 3
    • 0
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      When a cursor returns a single document in the firstBatch and the cursor is exhausted, we do not clean the cursor up.  The following test demonstrates this behavior:

      it.only('should close the cursor when there are no documents remaining', async function () {    
      
        await client.connect();
        await client.db('foo')
          .collection('bar')
          .insertOne({ name: 'bailey' });
        const cursor = client.db('foo')
          .collection('bar')
          .find({});    
        cursor.limit(1);    
        cursor.batchSize(-1);
        const doc = await cursor.next();
          
        expect(doc, 'expected a result but none was received').to.exist;
        expect(
          cursor.closed, 
          'expected cursor to be closed but it was not'
          ).to.be.true;  
      }); 

      Root Cause

      In abstract_cursor.ts' next method, if the cursor is not initialized, we:

      • initialize the cursor
      • return the first document

      After returning the first document, we don't check if the cursor is exhausted and close it if it is.

      Unknowns

      Is this a bug or works as designed?  We tell users they must always close their cursors, so we could demand that users iterate their cursors until they're complete (another call to `next` will close the cursor in the above test).  However, in `findOne` we construct a cursor and immediately return the result of a single document cursor without closing it.  So we either need to fix the bug outlined in this ticket, or ensure we close the cursor we're creating in `findOne`.

      AC

      Implementation

      • Add handling for cursor exhaustion upon firstBatch, if id == 0, cleanup.

      Testing

      • closes the cursor if it is exhausted on the firstBatch

            Assignee:
            alena.khineika@mongodb.com Alena Khineika
            Reporter:
            bailey.pearson@mongodb.com Bailey Pearson
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: