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

Unstable behavior when using useUnifiedTopology

    • Type: Icon: Bug Bug
    • Resolution: Gone away
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.3.4
    • Component/s: None
    • Labels:

      I am experiencing strange behaviors when i enabled useUnifiedTopology. It is completely random. To reproduce it try to shutdown MongoDB service or disable network after a few successful commands.

      Environment

      OS: Windows 10 Pro 1803

      NodeJS: 10.16.3

      MongoDB: 4.2.1

      MongoDB Driver: 3.3.4

      Expected Behavior

      It should throw a timeout error if command or connection fails and continue code execution

      Current Behavior

      Sometimes it is working as expected and throws timeout errors after 30 seconds

      OR

      It is waiting indefinitely when executing toArray() on a cursor.  No timeout error but it continues after connection is back.

      OR

      Application shuts down without notice after a few reconnection attempts. (Process is emitting exit event.)

       

      const mongodb = require("mongodb");
      
      const host = "";
      const dbName = "";
      const collectionName = "";
      const user = "";
      const pass = "";
      
      const connectionString = `mongodb://${host}`;
      
      const config = {
        auth: {
          user: user,
          password: pass,
        },
        authSource: dbName,
        useNewUrlParser: true,
        poolSize: 10,
      };
      
      const unifiedConfig = {
        useUnifiedTopology: true,
        serverSelectionTimeoutMS: 5000, // for testing
      };
      
      async function main() {
        const client = new mongodb.MongoClient(connectionString, {
          ...config,
          ...unifiedConfig,
        });
      
        const pool = await client.connect();
        const db = pool.db(dbName);
      
        while (true) {
          try {
            const cursor = db.collection(collectionName).find({});
      
            // Got stuck here until connection is restored
            // or process shuts down without error after a few reconnection attempts
            const result = await cursor.toArray();
      
            console.debug(JSON.stringify(result));
          } catch (error) {
            console.error(error.message);
          }
          await delay(1000);
        }
      
        // await pool.close();
      }
      
      try {
        main();
      } catch (error) {
        console.error(error);
      }
      
      async function delay(timeout) {
        return new Promise((resolve, _reject) => {
          setTimeout(() => {
            resolve();
          }, timeout);
        });
      }
      

            Assignee:
            matt.broadstone@mongodb.com Matt Broadstone
            Reporter:
            osmanyavuz@live.com Osman Yavuz
            Votes:
            2 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: