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

Multiple databases causing EventEmitter warnings

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 3.0.0, 3.0.1
    • Component/s: None
    • Environment:
      node v8.9.3

      Using more than 9 databases with one client (with a registered listener) causes MaxListenersExceededWarning: Possible EventEmitter memory leak detected warnings.

      The following snippet demonstrates the problem:

      const { MongoClient } = require('mongodb');
      
      function connect(url) {
        return MongoClient.connect(url).then((client) => {
          client.on('authenticated', () => { console.log('authenticated:', url); });
          client.on('close', () => { console.log('close:', url); });
          client.on('error', () => { console.log('error:', url); });
          client.on('fullsetup', () => { console.log('fullsetup:', url); });
          client.on('parseError', () => { console.log('parseError:', url); });
          client.on('reconnect', () => { console.log('reconnect:', url); });
          client.on('reconnectFailed', () => { console.log('reconnectFailed:', url); });
          client.on('timeout', () => { console.log('timeout:', url); });
          return client;
        });
      }
      
      function useDb(client, dbName) {
        return client.then((c) => c.db(dbName));
      }
      
      const client = connect('mongodb://localhost');
      
      const dbNames = ['foo1', 'foo2', 'foo3', 'foo4', 'foo5', 'foo6', 'foo7', 'foo8', 'foo9', 'foo10'];
      
      dbNames.forEach((dbName) => {
        useDb(client, dbName);
      });
      

      Output:

      (node:23675) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added. Use emitter.setMaxListeners() to increase limit
      (node:23675) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 timeout listeners added. Use emitter.setMaxListeners() to increase limit
      (node:23675) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 close listeners added. Use emitter.setMaxListeners() to increase limit
      (node:23675) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 parseError listeners added. Use emitter.setMaxListeners() to increase limit
      (node:23675) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 open listeners added. Use emitter.setMaxListeners() to increase limit
      (node:23675) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 fullsetup listeners added. Use emitter.setMaxListeners() to increase limit
      (node:23675) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 all listeners added. Use emitter.setMaxListeners() to increase limit
      (node:23675) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 reconnect listeners added. Use emitter.setMaxListeners() to increase limit
      

            Assignee:
            jessica.lord Jessica Lord
            Reporter:
            tjokimie Tuomo Jokimies
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: