Skip to content

fix!: run hook side effects inside the request's transaction - #124

Merged
bvdaakster merged 1 commit into
mainfrom
fix/hooks-join-request-transaction
Nov 28, 2026
Merged

bvdaakster merged 1 commit into
mainfrom
fix/hooks-join-request-transaction

Conversation

@bvdaakster

Copy link
Copy Markdown
Contributor

Problem

The afterChange / beforeChange hooks of the refunds, invoices and payments collections called req.payload.findByID / update without passing req, so that work ran on another connection than the request's transaction.

On Postgres (every operation is transactional):

  • Self-deadlock. Creating a refund, or an invoice with payment set, locks the payment row; the hook then updated that payment from a second connection and waited for the request, which waited for the hook. Without lock_timeout the request never returns.
  • Stale status. updatePaymentRefundStatus summed the succeeded refunds without seeing the one being saved, so the payment never became refunded / partially_refunded. A run of the suite against a MongoDB replica set showed the same stale status on main.
  • Parallel refundPayment with one idempotency key timed out behind the stuck transaction.

Change

  • Every nested Local API call made from a hook passes req. updatePaymentRefundStatus, sumRefunds and validateRefundRequest take an optional trailing req; existing callers compile unchanged.
  • src/utils/sideEffect.ts holds the one rule for a failing side effect: if the failure ended the request's transaction (Payload rolls it back and removes it from req when an operation using it fails), rethrow; otherwise log and continue as before.
  • The host's refund callbacks (onRefundSucceeded etc.) now run as the last step of the refunds hook, after the plugin's own writes, and follow the same rule.
  • refundPayment, processRefund and the sweeps deliberately take no req: reserve, verify, send needs committed data around the provider call. The refund job handler calls processRefund without req for the same reason. Documented in the README ("Call it outside your own transaction") and in the doc comments.
  • The 9 testFailingOnPostgres markers and dev/helpers/knownPostgresDefects.ts are gone. lock_timeout=3000 stays on the Postgres test connections so a regression fails fast.
  • README: new "Transactions" section, the rule for refundPayment, and a Troubleshooting entry describing the symptom on 0.7.0 and older.

Tests

dev/hooks-transaction.spec.ts adds: the helper's rule; refund list and payment status after partial and full refunds; parallel refunds with one idempotency key; invoice/payment links in both directions; a throwing callback not undoing a refund; and on Postgres, rollback when the payment update fails (refund create, refundPayment, invoice create) and when the caller rolls back its own transaction.

before after
SQLite 284 passed 295 passed, 3 skipped (Postgres-only)
Postgres 275 passed, 9 expected fail 297 passed, 1 skipped (SQLite-only)

Postgres run locally: about 65 s on main's CI, 11 to 18 s now (no more waiting for lock timeouts).

MongoDB replica set (one-off local run with a temporary adapter switch, not part of this PR): the refund-status tests that fail on main pass with this change. Parallel refundPayment calls with one idempotency key fail on MongoDB before and after (the losing insert gets a write conflict instead of a duplicate-key error); not addressed here.

BREAKING CHANGE: on a database with transactions (Postgres, MongoDB on a replica set, SQLite with transactionOptions) a failing hook side effect (updating the payment's refund list or refund status, linking a payment to its invoice, marking the invoice or payment paid) now fails the request and rolls the saved document back. It used to be logged while the document was kept. Hosts that relied on a refund, invoice or payment being stored although its related update failed (for example a payment whose linked invoice no longer exists) must handle the rejected request. On MongoDB replica sets two concurrent requests that change the same payment can now end in a write conflict for one of them; retry it. Without transactions (SQLite by default, MongoDB standalone) nothing changes.

🤖 Generated with Claude Code

https://claude.ai/code/session_0122PV8TJ7ELGdAX7H2hdrRU

The refunds, invoices and payments collection hooks read and updated related
documents without the request they were given, so that work ran on another
database connection than the request's transaction. On Postgres creating a
refund, or an invoice with `payment` set, waited on a row lock the request
itself held, a refunded payment stayed `succeeded` because the status was
derived without seeing the refund being saved, and parallel `refundPayment`
calls with one idempotency key timed out. The stale status also occurred on
MongoDB replica sets.

Every nested Local API call in the hooks now passes `req`, and
`updatePaymentRefundStatus`, `sumRefunds` and `validateRefundRequest` take an
optional `req`. `refundPayment` and `processRefund` deliberately take none:
they must read and write committed data around the provider call.

BREAKING CHANGE: on a database with transactions (Postgres, MongoDB replica
set, SQLite with `transactionOptions`) a failing hook side effect now fails
the request and rolls the saved document back, instead of being logged while
the document is kept. Payload has already rolled the transaction back at that
point, so there is no stored document to return. Without transactions the
failure is still logged and the document kept. On MongoDB replica sets two
concurrent requests that change the same payment can now end in a write
conflict for one of them; retry it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0122PV8TJ7ELGdAX7H2hdrRU
@claude

claude Bot commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@bvdaakster
bvdaakster merged commit 21a6a71 into main Nov 28, 2026
10 checks passed
@bvdaakster
bvdaakster deleted the fix/hooks-join-request-transaction branch November 28, 2026 21:11
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.

1 participant