Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loadBalanced=true drops connection after a delay and never reconnects #15042

Open
2 tasks done
pieromarini opened this issue Nov 15, 2024 · 0 comments
Open
2 tasks done
Milestone

Comments

@pieromarini
Copy link

pieromarini commented Nov 15, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.7.0

Node.js version

16.20.2

MongoDB server version

7.0

Typescript version (if applicable)

No response

Description

When trying to connect to a database using loadBalanced=true in the connection string, mongoose drops the connection after some time but never reconnects.

This happens in all versions >=8.7.0
8.6.4 doesn't have this issue and it reconnect successfully after the delay.

Steps to Reproduce

Providing sample code in which we manually add a delay so the connection drops and we never reconnect to MongoDB.

const mongoose = require("mongoose")
const { ObjectId } = require("bson")

// some connection string using `loadBalanced=true`
const connectionString = ""

const delay = ms => new Promise(res => setTimeout(res, ms));

async function execTest() {
  mongoose.connection.on('connected', () => console.log('connected'));
  mongoose.connection.on('open', () => console.log('open'));
  mongoose.connection.on('disconnected', () => console.log('disconnected'));
  mongoose.connection.on('reconnected', () => console.log('reconnected'));
  mongoose.connection.on('disconnecting', () => console.log('disconnecting'));
  mongoose.connection.on('close', () => console.log('close'));

  mongoose.connection.on('error', () => console.log('close'));

  await mongoose.connect(connectionString, { maxPoolSize: 100 })
  console.log("MongoDB Connection State:", mongoose.connection.readyState)

  const drawingSchema = mongoose.Schema({
    name: { type: String, required: true },
    prop: { type: String, required: true },
  });
  const someSchema = mongoose.model("someSchema", someSchema);

  await someSchema.insertMany([
    { name: "Name1", prop: "abc" },
    { name: "Name2", prop: "def" },
    { name: "Name3", prop: "ghi" },
    { name: "Name4", prop: "jkl" },
    { name: "Name5", prop: "mno" },
  ])

  await delay(20000)

  console.log("MongoDB Connection State:", mongoose.connection.readyState) // 0

  console.log("start session")
  const session = await mongoose.startSession(); // it hangs here, since it never reconnects
  console.log("start transaction")
  session.startTransaction();
  console.log("after start transaction")

  const x = await someSchema.findOne({ name: "Drawing1" }).lean()

  session.commitTransaction()

  session.endSession()
  console.log(x)

  console.log("after update")
  return
}

execTest()

Setting heartbeatFrequencyMS to a value higher that the delay makes the issue go away but I'm not sure if this would be a recommended workaround.

Expected Behavior

Mongoose should reconnect to the MongoDB database and the query should work

@pieromarini pieromarini changed the title loadBalanced=true drops connection and never reconnects loadBalanced=true drops connection after a delay and never reconnects Nov 15, 2024
@vkarpov15 vkarpov15 added this to the 8.8.3 milestone Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants