{"record":{"id":"c380e09882d6be7f","repo":"mongodb/node-mongodb-native","slug":"expired-after-this-timeoutms-ms","errorCode":null,"errorMessage":"Expired after ${this.timeoutMS}ms","messagePattern":"Expired after (.+?)ms","errorType":"exception","errorClass":"MongoOperationTimeoutError","httpStatus":null,"severity":"error","filePath":"src/timeout.ts","lineNumber":314,"sourceCode":"    this.minRoundTripTime = 0;\n    this._serverSelectionTimeout?.clear();\n    this._connectionCheckoutTimeout?.clear();\n  }\n\n  clear(): void {\n    this._serverSelectionTimeout?.clear();\n    this._connectionCheckoutTimeout?.clear();\n  }\n\n  /**\n   * @internal\n   * Throws a MongoOperationTimeoutError if the context has expired.\n   * If the context has not expired, returns the `remainingTimeMS`\n   **/\n  getRemainingTimeMSOrThrow(message?: string): number {\n    const { remainingTimeMS } = this;\n    if (remainingTimeMS <= 0)\n      throw new MongoOperationTimeoutError(message ?? `Expired after ${this.timeoutMS}ms`);\n    return remainingTimeMS;\n  }\n\n  /**\n   * @internal\n   * This method is intended to be used in situations where concurrent operation are on the same deadline, but cannot share a single `TimeoutContext` instance.\n   * Returns a new instance of `CSOTTimeoutContext` constructed with identical options, but setting the `start` property to `this.start`.\n   */\n  clone(): CSOTTimeoutContext {\n    const timeoutContext = new CSOTTimeoutContext({\n      timeoutMS: this.timeoutMS,\n      serverSelectionTimeoutMS: this.serverSelectionTimeoutMS\n    });\n    timeoutContext.start = this.start;\n    return timeoutContext;\n  }\n\n  override refreshed(): CSOTTimeoutContext {","sourceCodeStart":296,"sourceCodeEnd":332,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/timeout.ts#L296-L332","documentation":"Thrown by CSOTTimeoutContext.getRemainingTimeMSOrThrow() when the remaining time budget is zero or negative, i.e. the operation's total timeoutMS has been exhausted. This is the canonical CSOT (Client-Side Operation Timeout) expiry error, raised as a MongoOperationTimeoutError with the configured timeoutMS value in the message.","triggerScenarios":"Any operation running under CSOT (timeoutMS set on the client, session, or operation) where the elapsed time since the context started exceeds timeoutMS. Surfaces during server selection, command execution, cursor getMore, or transaction commit/abort when the deadline has passed.","commonSituations":"Long-running aggregations or queries against slow clusters with a tight timeoutMS. Also common in withTransaction() callbacks whose total work exceeds the transaction's timeoutMS, or when retries consume the remaining budget.","solutions":["Increase the timeoutMS value on the client, session, or operation to fit the workload's actual duration.","Optimize the query (add indexes, reduce result size, use $project) so it completes within the current budget.","For transactions, ensure withTransaction's total operations fit within the configured timeoutMS."],"exampleFix":"// before\ncollection.aggregate(pipeline, { timeoutMS: 1000 }); // too tight\n\n// after\ncollection.aggregate(pipeline, { timeoutMS: 30000 });","handlingStrategy":"retry","validationCode":null,"typeGuard":"import { MongoOperationTimeoutError } from 'mongodb';\nfunction isCSOTTimeout(e: unknown): e is MongoOperationTimeoutError {\n  return e instanceof MongoOperationTimeoutError || (e instanceof Error && /Expired after/.test(e.message));\n}","tryCatchPattern":"async function runWithBackoff<T>(fn: () => Promise<T>, retries = 2, baseMs = 1000): Promise<T> {\n  for (let i = 0; i <= retries; i++) {\n    try { return await fn(); }\n    catch (e) { if (isCSOTTimeout(e) && i < retries) { await new Promise(r => setTimeout(r, baseMs * 2 ** i)); continue; } throw e; }\n  }\n  throw new Error('unreachable');\n}","preventionTips":["Profile slow queries and set timeoutMS comfortably above observed p99 latency.","Add indexes for query predicates to keep operations within the CSOT budget.","For transactions, budget timeoutMS for the sum of all inner operations, not just one."],"tags":["timeout","csot","operation","performance"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}