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

MongoClient emits 'close' event twice

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 3.6.7, 4.0.0
    • Affects Version/s: 3.6.6
    • Component/s: None
    • Environment:
      Xubuntu 18.04, MongoDB server 4.4.0, Node.js 14.4.0
    • Not Needed

      The below script prints "Got close" twice:

      'use strict';
      
      const { MongoClient } = require('mongodb');
      
      void async function main() {
        const client = await MongoClient.connect('mongodb://localhost:27017/test', {
          useNewUrlParser: true,
          useUnifiedTopology: true
        });
      
        client.on('close', () => console.log('Got close'));
      
        await client.close();
      
        await new Promise(resolve => setTimeout(resolve, 100));
        console.log('Done');
      }();
      

      It looks like `MongoClient#close()` emits 'close' once: https://github.com/mongodb/node-mongodb-native/blob/2b18411d2f57e06d11262d5a308c56a9f561789e/lib/mongo_client.js#L284 , but then the Topology class also bubbles up its 'close' event to the MongoClient instance: https://github.com/mongodb/node-mongodb-native/blob/2b18411d2f57e06d11262d5a308c56a9f561789e/lib/operations/connect.js#L207

            Assignee:
            durran.jordan@mongodb.com Durran Jordan
            Reporter:
            val@karpov.io Valeri Karpov
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: