{"record":{"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/dce7939f86fb283e167ad709955abedb7bf23124/src/timeout.ts#L92-L128","documentation":"Thrown by Timeout.throwIfExpired() when the timeout has already elapsed (timedOut flag is true) and a code path checks synchronously rather than awaiting the promise. This is the internal TimeoutError class (a Promise subclass), distinct from the public MongoOperationTimeoutError. It signals that an operation's deadline passed before work could proceed.","triggerScenarios":"Any operation that uses a TimeoutContext and calls throwIfExpired() after the deadline elapses, e.g. server selection, connection checkout, or command execution when the configured timeoutMS or socketTimeoutMS has run out. Most commonly surfaces to users wrapped as a driver-level timeout error.","commonSituations":"Slow or unreachable MongoDB deployments where serverSelectionTimeoutMS (default 30s) expires during connect, or when timeoutMS/CSOT is enabled and an operation cannot complete in the remaining budget. Network latency spikes or overloaded connection pools also trigger it.","solutions":["Increase the relevant timeout (serverSelectionTimeoutMS, socketTimeoutMS, or timeoutMS) to accommodate your deployment's latency.","Verify the MongoDB instance is reachable and healthy (network, firewall, DNS).","Scale the connection pool or reduce concurrent load if checkout timeouts are the cause."],"exampleFix":"// before\nconst client = new MongoClient(uri); // default 30s server selection\n\n// after\nconst client = new MongoClient(uri, { serverSelectionTimeoutMS: 60000 });","handlingStrategy":"retry","validationCode":null,"typeGuard":"import { MongoOperationTimeoutError } from 'mongodb';\nfunction isTimeoutError(e: unknown): boolean {\n  return e instanceof Error && (e.name === 'TimeoutError' || e instanceof MongoOperationTimeoutError);\n}","tryCatchPattern":"let attempt = 0;\nwhile (attempt < 3) {\n  try {\n    return await collection.findOne(filter, { timeoutMS: 5000 });\n  } catch (e) {\n    if (isTimeoutError(e) && attempt < 2) { attempt++; continue; }\n    throw e;\n  }\n}","preventionTips":["Size timeouts to your deployment's realistic p99 latency plus a safety margin.","Monitor server selection and socket timeouts in metrics to catch chronic slowness early.","For intermittent network issues, wrap operations in a bounded retry with backoff."],"tags":["timeout","network","connection"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}