Skip to content

fix(db): retry initial Mongo connection to avoid worker crash-loop#559

Open
Kuchizu wants to merge 1 commit into
masterfrom
fix/mongo-connect-retry
Open

fix(db): retry initial Mongo connection to avoid worker crash-loop#559
Kuchizu wants to merge 1 commit into
masterfrom
fix/mongo-connect-retry

Conversation

@Kuchizu
Copy link
Copy Markdown
Member

@Kuchizu Kuchizu commented Jun 3, 2026

DatabaseController.connect() now retries the initial handshake instead of throwing on the first failure.

@neSpecc neSpecc requested a review from Copilot June 4, 2026 19:03
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the MongoDB DatabaseController.connect() startup behavior so workers don’t crash-loop when MongoDB is temporarily unreachable, by retrying the initial connection handshake with a fixed delay and a bounded server-selection timeout.

Changes:

  • Add configurable retry loop for the initial MongoDB connection attempt (MONGO_RECONNECT_TRIES, MONGO_RECONNECT_INTERVAL).
  • Add serverSelectionTimeoutMS to ensure each attempt fails fast during outages.
  • Adjust connect() to return the existing Db instance when already connected.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/db/controller.ts
Comment on lines +80 to +81
const tries = Number(process.env.MONGO_RECONNECT_TRIES) || DEFAULT_RECONNECT_TRIES;
const intervalMs = Number(process.env.MONGO_RECONNECT_INTERVAL) || DEFAULT_RECONNECT_INTERVAL_MS;
Comment thread lib/db/controller.ts
serverSelectionTimeoutMS: SERVER_SELECTION_TIMEOUT_MS,
...(this.appName ? { appName: this.appName } : {}),
});
this.db = await this.connection.db();
Comment thread lib/db/controller.ts
Comment on lines +83 to +84
for (let attempt = 1; attempt <= tries; attempt++) {
try {
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

Successfully merging this pull request may close these issues.

2 participants