{"record":{"id":"3f6aeabb02c8a9e0","repo":"mongodb/node-mongodb-native","slug":"an-operation-cannot-be-given-a-timeoutms-setting-w","errorCode":null,"errorMessage":"An operation cannot be given a timeoutMS setting when inside a withTransaction call that has a timeoutMS setting","messagePattern":"An operation cannot be given a timeoutMS setting when inside a withTransaction call that has a timeoutMS setting","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/utils.ts","lineNumber":551,"sourceCode":"            wtimeout: undefined,\n            wtimeoutMS: undefined\n          }\n        });\n      }\n      result.writeConcern = writeConcern;\n    }\n  }\n\n  result.timeoutMS = timeoutMS;\n\n  const readPreference = ReadPreference.fromOptions(options) ?? parent?.readPreference;\n  if (readPreference) {\n    result.readPreference = readPreference;\n  }\n\n  const isConvenientTransaction = session?.explicit && session?.timeoutContext != null;\n  if (isConvenientTransaction && options?.timeoutMS != null) {\n    throw new MongoInvalidArgumentError(\n      'An operation cannot be given a timeoutMS setting when inside a withTransaction call that has a timeoutMS setting'\n    );\n  }\n\n  return result;\n}\n\nexport function isSuperset(set: Set<any> | any[], subset: Set<any> | any[]): boolean {\n  set = Array.isArray(set) ? new Set(set) : set;\n  subset = Array.isArray(subset) ? new Set(subset) : subset;\n  for (const elem of subset) {\n    if (!set.has(elem)) {\n      return false;\n    }\n  }\n  return true;\n}\n","sourceCodeStart":533,"sourceCodeEnd":569,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/utils.ts#L533-L569","documentation":"Thrown by resolveOptions() when an operation is executed inside a convenient transaction (session.withTransaction with a timeoutMS / timeoutContext set) AND the per-operation options also specify a timeoutMS. CSOT semantics derive a single deadline from the transaction; nesting a separate timeoutMS is ambiguous and disallowed. Raised as MongoInvalidArgumentError.","triggerScenarios":"Inside session.withTransaction(async (session) => { collection.findOne({}, { session, timeoutMS: 1000 }) }) where the withTransaction call (or the session) already has a timeoutMS. Passing timeoutMS on both the transaction and its inner operations.","commonSituations":"Adopting CSOT (Client-Side Operation Timeouts) and setting timeoutMS both at the session/transaction level and on individual operations out of habit. Common when refactoring legacy code that set per-operation timeouts into a transactional context.","solutions":["Remove timeoutMS from the inner operation options; let the transaction's timeoutMS govern the deadline.","If an inner operation needs its own shorter deadline, reconsider whether it belongs in the same transaction.","Set timeoutMS only at the transaction level when using withTransaction."],"exampleFix":"// before\nawait session.withTransaction(async (session) => {\n  await collection.findOne({}, { session, timeoutMS: 1000 });\n}, { timeoutMS: 5000 });\n\n// after\nawait session.withTransaction(async (session) => {\n  await collection.findOne({}, { session });\n}, { timeoutMS: 5000 });","handlingStrategy":"validation","validationCode":"function stripTimeoutMSInTxn(session: import('mongodb').ClientSession, options: any) {\n  if (session?.explicit && session?.timeoutContext != null && options?.timeoutMS != null) {\n    const { timeoutMS: _omit, ...rest } = options;\n    return rest;\n  }\n  return options;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set timeoutMS only at the transaction level when using withTransaction; do not pass it on inner operations.","When refactoring code into a transaction, audit and remove per-operation timeoutMS settings.","Document the single-deadline CSOT model for your team to avoid conflicting timeouts."],"tags":["transaction","timeout","csot","validation"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}