{"id":"1c70e1954e0711c3","repo":"mongodb/node-mongodb-native","slug":"timed-out","errorCode":null,"errorMessage":"Timed out","messagePattern":"Timed out","errorType":"exception","errorClass":"TimeoutError","httpStatus":null,"severity":"error","filePath":"src/timeout.ts","lineNumber":110,"sourceCode":"  }\n\n  /**\n   * Clears the underlying timeout. This method is idempotent\n   */\n  clear(): void {\n    clearTimeout(this.id);\n    this.id = undefined;\n    this.timedOut = false;\n    this.cleared = true;\n  }\n\n  throwIfExpired(): void {\n    if (this.timedOut) {\n      // This method is invoked when someone wants to throw immediately instead of await the result of this promise\n      // Since they won't be handling the rejection from the promise (because we're about to throw here)\n      // attach handling to prevent this from bubbling up to Node.js\n      this.then(undefined, squashError);\n      throw new TimeoutError('Timed out', { duration: this.duration });\n    }\n  }\n\n  public static expires(duration: number, unref?: true): Timeout {\n    return new Timeout(undefined, { duration, unref });\n  }\n\n  static override reject(rejection?: Error): Timeout {\n    return new Timeout(undefined, { duration: 0, unref: true, rejection });\n  }\n}\n\n/** @internal */\nexport type TimeoutContextOptions = (LegacyTimeoutContextOptions | CSOTTimeoutContextOptions) & {\n  session?: ClientSession;\n};\n\n/** @internal */","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/timeout.ts#L92-L128","documentation":"Thrown by the @internal Timeout.throwIfExpired() when the timeout has already fired (timedOut === true). It produces a TimeoutError with message 'Timed out' and the configured duration. This is the synchronous check counterpart to awaiting the Timeout promise's rejection. The internal Timeout class is shared by both legacy and CSOT timeout contexts.","triggerScenarios":"Driver-internal code calling timeout.throwIfExpired() after the timeout already elapsed (e.g. between awaiting a connection and issuing a command). Not thrown directly by user-facing methods under normal use; users see MongoOperationTimeoutError wrappers instead.","commonSituations":"Slow operations where the deadline expires mid-flight; CSOT contexts checking expiry before each phase (server selection, checkout, socket write/read).","solutions":["Increase the operation timeout (timeoutMS) or relevant socketTimeoutMS / serverSelectionTimeoutMS.","Reduce operation latency: add indexes, narrow queries, scale the cluster.","If seen unexpectedly, report it - throwIfExpired firing where the await path should have surfaced the error first can indicate a driver bug."],"exampleFix":"// before\ncursor.addCursorFlag('noCursorTimeout', true); // unrelated, does not help\n\n// after - size the timeout to the workload\nawait coll.find({}, { timeoutMS: 60_000 }).toArray();","handlingStrategy":"retry","validationCode":"// user-side: size the timeout to the workload\nconst timeoutMS = estimatedOpMs * 2;\nawait coll.find({}, { timeoutMS }).toArray();","typeGuard":"import { MongoOperationTimeoutError } from 'mongodb';\nfunction isTimeoutError(e: unknown): boolean {\n  return e instanceof MongoOperationTimeoutError ||\n    (e != null && typeof e === 'object' && (e as any).name === 'TimeoutError');\n}","tryCatchPattern":"try {\n  await op();\n} catch (e) {\n  if (isTimeoutError(e)) {\n    // optionally retry once with a larger timeoutMS, or surface to the user\n  } else throw e;\n}","preventionTips":["Set timeoutMS based on measured p99 latency, not guesses.","Index the fields used by slow queries that approach the deadline.","For long transactions, budget timeoutMS for the whole transaction."],"tags":["timeout","csot","internal","deadline"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}