Skip to content

fix(plugin-multi-tenant): serialize transaction dataloader finds#16463

Open
cyphercodes wants to merge 1 commit intopayloadcms:3.xfrom
cyphercodes:fix/16462-serialize-transaction-finds
Open

fix(plugin-multi-tenant): serialize transaction dataloader finds#16463
cyphercodes wants to merge 1 commit intopayloadcms:3.xfrom
cyphercodes:fix/16462-serialize-transaction-finds

Conversation

@cyphercodes
Copy link
Copy Markdown

What?

Serialize payloadDataLoader.find calls only while a request is running inside a transaction. This prevents multiple relationship/filterOptions validators from issuing concurrent MongoDB operations on the same transaction session.

Why?

The multi-tenant plugin auto-injects relationship filterOptions. Array rows are validated in parallel, so creating a tenant-scoped document with several relationship rows can trigger concurrent payload.find calls in the same MongoDB transaction and surface false invalid relationship validation errors.

How?

  • Add a per-request find queue in getDataLoader for transactional finds.
  • Leave non-transactional finds concurrent.
  • Add unit coverage for transactional serialization and non-transactional concurrency.
  • Add a multi-tenant integration regression covering array relationship creation with same-tenant items.

Fixes #16462

Verification

  • pnpm exec vitest run --project unit packages/payload/src/collections/dataloader.spec.ts
  • pnpm exec vitest run --project int test/plugin-multi-tenant/int.spec.ts -t 'should create an array of same-tenant relationships without transaction races' --no-cache
  • pnpm exec vitest run --project int test/plugin-multi-tenant/int.spec.ts --no-cache
  • pnpm exec prettier --check packages/payload/src/collections/dataloader.ts packages/payload/src/collections/dataloader.spec.ts test/plugin-multi-tenant/int.spec.ts
  • pnpm exec eslint --flag v10_config_lookup_from_file packages/payload/src/collections/dataloader.ts
  • pnpm exec eslint --flag v10_config_lookup_from_file --no-warn-ignored packages/payload/src/collections/dataloader.spec.ts
  • NODE_OPTIONS=--max-old-space-size=4096 pnpm exec eslint --flag v10_config_lookup_from_file --cache --cache-strategy content --no-error-on-unmatched-pattern test/plugin-multi-tenant/int.spec.ts (passes with existing warnings for token and blueDogTenantID)
  • pnpm --filter payload build:swc
  • git diff --check

Note: a combined ESLint run over both changed non-spec files hit local Node heap/timeout limits, so I verified the files separately.

@amitzur
Copy link
Copy Markdown

amitzur commented May 4, 2026

@cyphercodes thanks for the quick turnaround!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants