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

Investigate Node-3263: Calling close() does not terminate all connections

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
    • 2
    • Not Needed

      In light of handling CTRL-C in the shell we observed that using `MongoClient.close(...)` does not close all connections that were opened by the client if a connection is still running an operation.

      Take the following script as an example:

      const MongoClient = require('mongodb').MongoClient;
      (async() => {
          console.log('Connecting...');
          const client = await MongoClient.connect('mongodb://localhost:30001');
          const db = client.db('test');
          db.collection('ctrlc').find({ $where: 'while(true) {}' }).toArray().then(r => console.log(r));
          setTimeout(async () => {
              await client.close(true);
              console.log('Closed...');
              setInterval(() => {
                  console.log('alive', new Date().toISOString());
              }, 10_000);
          }, 1000);
      })().catch(e => process.nextTick(() => { console.log(e); process.exit(1); }));
      

      The find operation will be running in the background and continues even after calling client.close(...) (which does not throw an error). The socket is still kept open until I terminate the process.

      (NOTE: the example above only works as designed when there is a collection called ctrlc with at least one document)

      This seems to be a regression in 4.x.

            Assignee:
            Unassigned Unassigned
            Reporter:
            dbeng-pm-bot PM Bot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: