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

Calling close() does not terminate all connections

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • 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?

      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:
            michael.rose@mongodb.com Michael Rose (Inactive)
            Votes:
            2 Vote for this issue
            Watchers:
            11 Start watching this issue

              Created:
              Updated: