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

Reconnecting to Mongos proxy server never fires the reconnect event

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 3.2.0
    • Affects Version/s: 3.0.2
    • Component/s: Core, MongoDB 3.4
    • Labels:
    • Environment:
      Ubuntu, Docker, AWS

      *BUG*

      *EXPECTED BEHAVIOR*
      When a Mongos server resets the reconnect event should fired on the connection.

      *ACTUAL BEHAVIOR*
      The reconnect event is never fired.

      *STEP TO REPRODUCE*
      1. I'm using a Mongos container
      2. Simple connection:

      import { MongoClient } from 'mongodb'
      
      const dburl = 'mongodb://YOURCONTAINERADRESS:27017/'
      
      MongoClient.connect(
        dburl,
        {
          reconnectTries: Number.MAX_VALUE,
          reconnectInterval: 1000
        },
      
        function(err, db) {
          db.on('close', () => console.log('closed'))
          db.on('reconnect', () => console.log('reconnect'))
          db.on('disconnect', () => console.log('disconnect'))
        }
      )
      

      3. Restart the container

      With the same code but on my local machine and doing: service mongod stop -> service mongod start the reconnect event is properly fired.

      **POSSIBLE CAUSE**
      After digging a bit I found out that in
      mongodb-core/mongos.js

      return reconnectProxies(self, self.disconnectedProxies, function() {
              if (self.state === DESTROYED || self.state === UNREFERENCED) return;
      
              // Are we connected ? emit connect event
              if (self.state === CONNECTING && options.firstConnect) {
                self.emit('connect', self);
                self.emit('fullsetup', self);
                self.emit('all', self);
              } else if (self.isConnected()) {
                self.emit('reconnect', self);
              } else if (!self.isConnected() && self.listeners('close').length > 0) {
                self.emit('close', self);
              }
      
              // Perform topology monitor
              topologyMonitor(self);
            });
      

      the self.isConnected() is never true on a reconnection therefore the reconnect event is never emitted. This is because the connectedProxies array is alway empty at this point in time.

            Assignee:
            matt.broadstone@mongodb.com Matt Broadstone
            Reporter:
            maloguertin Malo Guertin
            Votes:
            2 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: