Skip to content

MongoDB TransientTransactionError / WriteConflict is not retried for Payload Local API writes #16766

@dolanoadriano

Description

@dolanoadriano

Describe the Bug

Payload starts MongoDB transactions by default for Local API write operations, but when MongoDB throws a retryable transaction error such as TransientTransactionError / WriteConflict, Payload aborts the transaction and rethrows the error instead of retrying the whole transaction body.

MongoDB treats WriteConflict inside transactions as expected optimistic concurrency behavior. The recommended handling is to retry the full transaction when the error has the TransientTransactionError label. The MongoDB driver's higher-level session.withTransaction() API does this retry behavior automatically.

❌ In Payload, the current flow appears to be manual transaction handling:

const shouldCommit = !args.disableTransaction && await initTransaction(args.req)

try {
  const result = await operation(args)

  if (shouldCommit) {
    await commitTransaction(args.req)
  }

  return result
} catch (err) {
  if (shouldCommit) {
    await killTransaction(args.req)
  }

  throw err
}

✅ Expected flow:

await runWithTransactionRetry(async () => { // ‼️
  const shouldCommit = !args.disableTransaction && await initTransaction(args.req)

  try {
    const result = await operation(args)

    if (shouldCommit) {
      await commitTransaction(args.req)
    }

    return result
  } catch (err) {
    if (shouldCommit) {
      await killTransaction(args.req)
    }

    throw err
  }
})

Link to the code that reproduces this issue

https://github.com/dolanoadriano/payload-write-conflict-repro

Reproduction Steps

  1. Run:
pnpm install
pnpm test:int _community

Which area(s) are affected?

db: mongodb

Environment Info

Binaries:
  Node: 24.14.0
  npm: 11.9.0
  Yarn: N/A
  pnpm: 11.1.1

Relevant Packages:
  payload: 3.80.0

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.3.0: Wed Jan 28 20:54:38 PST 2026; root:xnu-12377.91.3~2/RELEASE_ARM64_T6050
  Available memory (MB): 24576

Metadata

Metadata

Assignees

No one assigned

    Labels

    db: mongodb@payloadcms/db-mongodbstatus: needs-triagePossible bug which hasn't been reproduced yetv3

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions