Repository navigation
fix!: run hook side effects inside the request's transaction - #124
Merged
Merged
Conversation
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
Contributor
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
afterChange/beforeChangehooks of the refunds, invoices and payments collections calledreq.payload.findByID/updatewithout passingreq, so that work ran on another connection than the request's transaction.On Postgres (every operation is transactional):
paymentset, locks the payment row; the hook then updated that payment from a second connection and waited for the request, which waited for the hook. Withoutlock_timeoutthe request never returns.updatePaymentRefundStatussummed the succeeded refunds without seeing the one being saved, so the payment never becamerefunded/partially_refunded. A run of the suite against a MongoDB replica set showed the same stale status on main.refundPaymentwith one idempotency key timed out behind the stuck transaction.Change
req.updatePaymentRefundStatus,sumRefundsandvalidateRefundRequesttake an optional trailingreq; existing callers compile unchanged.src/utils/sideEffect.tsholds the one rule for a failing side effect: if the failure ended the request's transaction (Payload rolls it back and removes it fromreqwhen an operation using it fails), rethrow; otherwise log and continue as before.onRefundSucceededetc.) now run as the last step of the refunds hook, after the plugin's own writes, and follow the same rule.refundPayment,processRefundand the sweeps deliberately take noreq: reserve, verify, send needs committed data around the provider call. The refund job handler callsprocessRefundwithoutreqfor the same reason. Documented in the README ("Call it outside your own transaction") and in the doc comments.testFailingOnPostgresmarkers anddev/helpers/knownPostgresDefects.tsare gone.lock_timeout=3000stays on the Postgres test connections so a regression fails fast.refundPayment, and a Troubleshooting entry describing the symptom on 0.7.0 and older.Tests
dev/hooks-transaction.spec.tsadds: 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.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
refundPaymentcalls 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