Skip to content

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

Open

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

Activity

  1. added
    status: needs-triagePossible bug which hasn't been reproduced yet
    validate-reproductionAuto-added tag on create to tell bot to check recreation URL, removed after check.
    on May 28, 2026
  2. added
    db: mongodb@payloadcms/db-mongodb
    and removed
    validate-reproductionAuto-added tag on create to tell bot to check recreation URL, removed after check.
    on May 28, 2026
  3. cbratschi commented on May 29, 2026

    @cbratschi

    Might be related to #4350.

  4. dolanoadriano commented on Jun 3, 2026

    @dolanoadriano
    Author
  5. Shankhadeep25 commented on Jun 6, 2026

    @Shankhadeep25

    Hi @dolanoadriano — great writeup. Are you actively working on a PR for this? If not, I'd like to take a shot at implementing the runWithTransactionRetry wrapper. Happy to coordinate if you're already mid-way through it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions