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

APM API can cause cursors not to be closed

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.2.13
    • Affects Version/s: 2.2.2
    • Component/s: None
    • Environment:
      OSX, Node 6.9.1

      When a collection.find().limit() is called with APM being activated, the cursor for the find() call does not get killed. The key is to use the limit() function with a number that is less than the total number of records in the collection.

      Here is a full repro:

      var mongodb = require("mongodb");
      
      var url = 'mongodb://localhost:27017'
      
      var instrumenter = mongodb.instrument();
      
      var db;
      var adminDb;
      var cursor;
      var cursorCountBefore;
      var cursorCountAfter;
      
      mongodb.MongoClient.connect(url)
        .then(connection => {
          db = connection;
          admindb = db.admin();
          collection = db.collection('MyCollection');
      
          // make sure collection has records (more than 2)
          return collection.insertMany([
            {a : 1}, {a : 2}, {a : 3}
          ]);
        })
        .then(() => {
          // get number of cursors before
          return admindb.serverStatus();
        })
        .then(result => {
          cursorCountBefore = result.cursors.clientCursors_size;
      
          cursor = collection.find({}).limit(2);
      
          // the issue is not there without the limit call
          // cursor = collection.find({})
      
          return cursor.toArray();
        })
        .then(arr => {
          return cursor.close();
        })
        .then(() => {
          // get number of cursors after
          return admindb.serverStatus();
        })
        .then(result => {
          cursorCountAfter = result.cursors.clientCursors_size;
      
          console.log(cursorCountBefore);
          console.log(cursorCountAfter);
        })
        .catch(err => {
          console.log('error: ' + err);
        })
      

            Assignee:
            christkv Christian Amor Kvalheim
            Reporter:
            martinkuba Martin Kuba
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: