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

Calling `next()` on a closed cursor no longer throws an error

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 6.8.0
    • Component/s: Mongoose
    • 3
    • 1
    • Hide

      Create a copy of the Kickoff Template with the issue key (NODE-XXX) in the filename and share a link to the new doc via this field.

      Show
      Create a copy of the Kickoff Template with the issue key (NODE-XXX) in the filename and share a link to the new doc via this field.
    • 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?

      Use Case

      As a... developer
      I want... to get an error message when I try to use a closed cursor
      So that... this is the behavior in 6.7.0

      User Impact

      Mongoose tests fail when using Node driver 6.8.0. This blocks Mongoose from using Node driver 6.8.0.

      The test that failed is called gh-4258 which was closed by 4a4662a and the test was added in ecf8ed1 (link to modern version)

      Dependencies

      N/A

      Unknowns

      • What is the expectation for a cursor that is closed but still has documents to return versus one that has exhausted in its first batch?
      • What about tail-able cursors that do not exhaust?

      Acceptance Criteria

      • The driver should always throw a CursorExhaustedError if the cursor is closed while it has not been exhausted. This matches pre-6.8.0 behavior.
      • The following script should succeed (add a test that adapts this logic to driver test suite code)
      const mongodb = require('mongodb');
      const assert = require('assert');
      
      async function run() {
        const client = new mongodb.MongoClient('mongodb://localhost:27017');
      
        try {
          await client.connect();
          const db = client.db('testdb');
          const collection = db.collection('testcollection');
          
          // Inserting a document to ensure the test case runs properly
          await collection.insertMany([{ name: 'Axl', test: 'test' }]);
      
          const cursor = collection.find().sort({ name: 1 });
          
          const doc = await cursor.next();
          assert.equal(doc.name, 'Axl');
          assert.equal(doc.test, 'test');
          
          await cursor.close();
      
          try {
            await cursor.next();
            assert.ok(false);
          } catch (error) {
            console.log('B', error);
            assert.equal(error.name, 'MongoCursorExhaustedError');
          }
        } catch (err) {
          console.error(err);
        } finally {    
          await client.close();
        }
      }
      
      run().catch(console.dir);
      

      Implementation Requirements

      • Determine when it is expected for Exhaustion errors to throw.
        • The driver should always throw a CursorExhaustedError if the cursor is closed while it has not been exhausted.
      • Correct regression

      Testing Requirements

      • See mongoose test and reproduction script.

      Documentation Requirements

      • Add API docs that clarify when an Exhaust error throws from a cursor API

      Follow Up Requirements

      • Let mongoose developers know what version this fix is available in

            Assignee:
            Unassigned Unassigned
            Reporter:
            val@karpov.io Valeri Karpov
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated: