v4: a failing local API operation rolls back a transaction the caller owns, so later writes commit outside it

Aperta
#19,657 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@ghozali25 ci sta già lavorando.

Dal 17/11/2026.

  • #19662 di @ghozali25 — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
node.js, postgresql, typescript
Ambito
backend, database

Direzione di ricerca

Start with the catch blocks in packages/payload/src/collections/operations/create.ts, then compare the listed catch blocks in update.ts, updateByID.ts, delete.ts, deleteByID.ts, restoreVersion.ts and renameFile.ts. Trace how initTransaction signals whether an operation owns the transaction and how killTransaction uses that ownership. Reproduce with repro-2.ts; done when a failed operation leaves the caller's transaction intact and the writes behave as expected at commit.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

area: core Bug db-postgres Payload OSS
Describe the Bug

When the caller owns a transaction (it called payload.db.beginTransaction() and passes req.transactionID), a local-API create that fails (here a validation error) rolls back the caller's transaction and deletes req.transactionID. The operation's catch calls killTransaction(args.req) unconditionally (packages/payload/src/collections/operations/create.ts), even though initTransaction returned false for it because the transaction was not its own. killTransaction's own doc comment says only the operation that started the transaction should call it.

For a caller that catches the error and carries on (e.g. a bulk import that skips invalid rows), the result is:

  • the writes made before the failure are silently rolled back;
  • req.transactionID is now undefined, so the writes made after it run outside any transaction and commit on their own;
  • the caller's commitTransaction has nothing to commit, so no error is raised anywhere.

Expected: an operation that did not start the transaction leaves it alone (the caller decides whether to roll back), or at least the caller's commit fails loudly.

update.ts, updateByID.ts, delete.ts, deleteByID.ts, restoreVersion.ts and renameFile.ts have the same unconditional killTransaction in their catch.

Link to the code that reproduces this issue

https://github.com/elghaied/payload-v4-reproductions (repro-2.ts)

Reproduction Steps
  1. pnpm install, then cp .env.example .env and point DATABASE_URL at an empty PostgreSQL database
  2. pnpm repro repro-2.ts

The script begins a transaction, creates note A, tries to create an invalid note (the error is caught), creates note C, then commits. Output:

transactionID before failing op: 7833329b-0871-4092-aa20-c1ef0a475cfa
expected failure: The following field is invalid: Title
transactionID after failing op: undefined (caller owns it; should be unchanged)
observed committed: [ 'tx-…-C' ]  expected: [A, C] together (or loud commit failure)
Which area(s) are affected?

area: core, db-postgres

Environment Info
payload: 4.0.0-beta.9
@payloadcms/db-postgres: 4.0.0-beta.9
Node.js: 24.22.1
PostgreSQL: 17.10
(local API only, no Next.js app)
Lingua principale
TypeScript
Stelle
45.8k
Fork
4.4k
Merge medio
3g 4h
PR unite (30g)
210

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di payloadcms/payload

Tutte le issue di payloadcms/payload

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.