From d6c2f78676584f95f15dc00e58d5241b97dba383 Mon Sep 17 00:00:00 2001 From: Vansh98789 Date: Wed, 12 Aug 2026 22:50:11 +0530 Subject: [PATCH 1/3] fix(api): disable mongoose autoIndex in check-alerts worker The worker only reads from MongoDB, so background createIndexes calls raced the short-lived connection close (endSessions) and failed with MongoExpiredSessionError on mongodb.createIndexes spans. Closes #2734 --- packages/api/src/models/index.ts | 3 ++- .../api/src/tasks/checkAlerts/providers/default.ts | 11 ++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/api/src/models/index.ts b/packages/api/src/models/index.ts index 3e932d006a..1779e3dc41 100644 --- a/packages/api/src/models/index.ts +++ b/packages/api/src/models/index.ts @@ -51,7 +51,7 @@ mongoose.connection.on('reconnectFailed', () => { logger.error('Failed to reconnect to MongoDB'); }); -export const connectDB = async () => { +export const connectDB = async (options?: mongoose.ConnectOptions) => { // breadcrumbs for future greppers: aws4 is included as a dependency of the api so that // users can use AWS auth in their mongo connection string here, e.g. // mongodb+srv://blahblah...mongodb.net/hyperdx?authSource=%24external&authMechanism=MONGODB-AWS @@ -61,6 +61,7 @@ export const connectDB = async () => { await mongoose.connect(config.MONGO_URI, { heartbeatFrequencyMS: 10000, // retry failed heartbeats maxPoolSize: 100, // 5 nodes -> max 1000 connections + ...options, }); }; diff --git a/packages/api/src/tasks/checkAlerts/providers/default.ts b/packages/api/src/tasks/checkAlerts/providers/default.ts index 079cdcc89d..e586199644 100644 --- a/packages/api/src/tasks/checkAlerts/providers/default.ts +++ b/packages/api/src/tasks/checkAlerts/providers/default.ts @@ -270,9 +270,14 @@ async function loadAlert( } export default class DefaultAlertProvider implements AlertProvider { - async init() { - await Promise.all([connectDB()]); - } + async init() { + // The check-alerts worker only reads from MongoDB and never needs to + // ensure indexes exist (the API service owns that). Disabling autoIndex + // prevents background createIndexes calls from racing the short-lived + // connection close (which sends endSessions), surfaced as + // MongoExpiredSessionError on mongodb.createIndexes spans. + await Promise.all([connectDB({ autoIndex: false })]); +} async asyncDispose() { await Promise.all([mongooseConnection.close()]); From 3ed2ef5995569b4eaf66fcdab73babec1f597fa6 Mon Sep 17 00:00:00 2001 From: Vansh Maheshwari <121272010+Vansh98789@users.noreply.github.com> Date: Wed, 12 Aug 2026 23:01:51 +0530 Subject: [PATCH 2/3] Apply suggestion from @greptile-apps[bot] Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> --- .../src/tasks/checkAlerts/providers/default.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/api/src/tasks/checkAlerts/providers/default.ts b/packages/api/src/tasks/checkAlerts/providers/default.ts index c555cb53e8..4075b09560 100644 --- a/packages/api/src/tasks/checkAlerts/providers/default.ts +++ b/packages/api/src/tasks/checkAlerts/providers/default.ts @@ -273,14 +273,14 @@ async function loadAlert( } export default class DefaultAlertProvider implements AlertProvider { - async init() { - // The check-alerts worker only reads from MongoDB and never needs to - // ensure indexes exist (the API service owns that). Disabling autoIndex - // prevents background createIndexes calls from racing the short-lived - // connection close (which sends endSessions), surfaced as - // MongoExpiredSessionError on mongodb.createIndexes spans. - await Promise.all([connectDB({ autoIndex: false })]); -} + async init() { + // The check-alerts worker only reads from MongoDB and never needs to + // ensure indexes exist (the API service owns that). Disabling autoIndex + // prevents background createIndexes calls from racing the short-lived + // connection close (which sends endSessions), surfaced as + // MongoExpiredSessionError on mongodb.createIndexes spans. + await Promise.all([connectDB({ autoIndex: false })]); + } async asyncDispose() { await Promise.all([mongooseConnection.close()]); From 339430a3fbae2a24228fb5697107e14a918ee6f0 Mon Sep 17 00:00:00 2001 From: Vansh98789 Date: Thu, 13 Aug 2026 21:04:21 +0530 Subject: [PATCH 3/3] chore: add changeset for autoIndex fix --- .changeset/dull-carrots-bow.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/dull-carrots-bow.md diff --git a/.changeset/dull-carrots-bow.md b/.changeset/dull-carrots-bow.md new file mode 100644 index 0000000000..950c823735 --- /dev/null +++ b/.changeset/dull-carrots-bow.md @@ -0,0 +1,5 @@ +--- +"@hyperdx/api": patch +--- + +Disable mongoose autoIndex in check-alerts worker to prevent MongoExpiredSessionError