Repository navigation
fix: recheck spark send authorization at submission - #2053
Conversation
Verified: just compile and just test before the review fixes; after them SparkRepoTest, LightningRepoTest, QuickPayRepoTest and just lint
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
[Medium impact] The PR appears safe to merge. SummarySpark now checks payment authorization again after its journal writes, immediately before submission.
Diagram%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Initial authorization and admission] --> B[Write send journal]
B --> C{Still authorized?}
C -->|Yes| D[Check deadline and exit]
D --> E[Submit Spark send]
C -->|No| F[Clean up unsent attempt]
F --> G[Return PaymentStoppedAtSubmission]
G --> H[QuickPay returns to confirmation]
Reviews (1) · Last reviewed commit: "fix: recheck spark send authorization at..." · Reviewed by Greptile |
talosmachina
left a comment
There was a problem hiding this comment.
No findings. Adds a second authorization check right before sparkService.send, inside the spending block after the journal writes, so a QuickPay send whose authorization lapses while addSparkSendHash / setSparkSendAttempt / addSparkUncertainSend suspend fails with PaymentStoppedAtSubmission and rolls the journal back. Reviewed bd98bcb, full tier, static only: on bitkit-android an auto review never builds the branch, so this was reasoned from the code, and CI was still running when I read it.
Merge confidence: 4/5, CI had not finished at review time, and the only test is a unit test with mocks (the new SparkRepoTest.kt case), which can't show a device timing.
What I checked, and 5 candidates I ruled out
Read in full: SparkRepo.payInvoice, sendRetryingLeafSelection and spending; LightningRepo.payInvoiceChecked and its authorizeSend wrapper; QuickPayRepo.dispatchBolt11, fallBackBeforeDispatch and tryMarkDispatched.
Call sites traced: sparkRepo.payInvoice has one caller (LightningRepo.kt:1660), so the second onBeforeSend call always lands on the isChecked branch, which calls only isAuthorizedAtSubmission (null means true). The onBeforeSend lambdas with side effects (QuickPay's tryMarkDispatched, the Paykit and AppViewModel callers) still run once.
QuickPay path: the first check marks the op dispatched, then the refusal at submission comes back as PaymentStoppedAtSubmission. isAuthorizationLapsed is still false, so isStoppedAtSubmission() routes to fallBackBeforeDispatch(isMarkedDispatched = true), which matches current.dispatched, releases the spend and falls back to confirm. LightningRepo's onFailure puts back the earlier rail because isAdmitted is true and the error is not PaymentPendingException.
Ruled out
runCatchingtorunSuspendCatchinglets a cancellation skipdropUnsentSend: in practice the new call does not suspend. Past the first check,authorizeSendonly invokes the non-suspending() -> Boolean, so no cancellation can arrive between the journal writes and the send. A cancellation anywhere else keeps the hold, as intended.- A keyed retry of a held send is refused but keeps its hold (
isReusedKey && wasHeldreturns early): the deadline and exit refusals at the same point already behave this way, and keeping the hold is deliberate, since an earlier attempt under that key may have committed a transfer. sendRecordedAtis bumped before the check and not rolled back: this was already the case for the deadline and exit refusals, andadmitSendkeeps same-invoice sends exclusive, so only one that overlaps the refused one sees it.- The leaf-selection retry now counts three checks instead of two: it still reruns only the side-effect-free check, and the updated test asserts the new count.
- Manual sends:
isAuthorizedAtSubmissionisnull, so the new check returnstrueand nothing changes for them.
Reviewer model: claude-opus-5-5
Reasoning effort: Not exposed (the run's metadata does not report an effort setting)
Regtest APKDownload bitkit-dev-debug universal APK (expires in 30 days). |
pwltr
left a comment
There was a problem hiding this comment.
Reviewed bd98bcb. Spark now rechecks QuickPay authorization after writing the send journal and before calling the SDK.
Low: If the wallet locks during journaling and a cleanup write then fails, SparkRepo.kt:662 lets the storage exception replace PaymentStoppedAtSubmission. QuickPay can show the payment as pending and keep its spending reservation even though Spark never submitted it. Preserve the typed refusal when cleanup fails so QuickPay can release the reservation and return to swipe confirmation.
Review details
Quick review
Model: Claude Opus 5.5
Reasoning effort: high
General review
Model: GPT-6.2 Sol
Reasoning effort: high
Combined review
Model: GPT-6.2 Sol
Reasoning effort: high
A refusal at submission now reaches the caller even when dropping the unsent send's journal entries fails, so QuickPay falls back instead of showing a send that never left as pending. Verified: SparkRepoTest (new test fails before the change), just lint
@pwltr Fixed in 3c593c8: a failed cleanup write after the late refusal is now logged and |
jvsena42
left a comment
There was a problem hiding this comment.
Approving at 3c593c8 with one LOW open, inline. Fix it here or move it to a follow-up issue, your choice; please reply on the thread with which, and the issue link if it's a follow-up.
Checked and clean:
- Callers:
SparkRepo.payInvoicehas one production caller (LightningRepo.kt:1660, always withauthorizeSend). QuickPay is the onlypayInvoice*caller that setsisAuthorizedAtSubmission, and its recheck is real. Node-only paths (payInvoiceWithNode,payInvoiceOverLdk) never reach Spark;withdrawAll/withdraware untouched. - TOCTOU: from the recheck at
SparkRepo.kt:658tosparkService.sendthere's no suspension, mutex or queue, and the secondauthorizeSendcall cannot suspend. - Amount and fee: the send uses the same
preparedobject whose fee was checked againstmaxTotalFeeMsat. Nothing is re-quoted in between. - Side effects run once:
isCheckedis set beforeonBeforeSend(), sotryMarkDispatched,ensurePaymentAllowed,admitSendandrecordSendRaildon't run twice. - Refusal path:
spendsInFlight/sendsInFlightare released, the earlier rail is restored, and there's no fallback to the node. QuickPay emitsFallBackToConfirmwithisMarkedDispatched = true. A later attempt reuses the unsent attempt key, so there's no double pay. - 3c593c8:
bestEffort { dropUnsentSend() }usesrunSuspendCatchingand keeps the original refusal.
perf: not relevant (one extra non-suspending lambda call per Spark send).
Device gate: no device check needed — no UI change, and the window the recheck closes can't be timed on a device.
CI is still running at this head; this approval assumes it goes green.
Reviewer model: Opus 5.5 (claude-opus-5-5) coordinating and verifying; Fable 5.2 (claude-fable-5-2) primary review
Reasoning effort: Not exposed
pwltr
left a comment
There was a problem hiding this comment.
Reviewed 3c593c8. Spark now rechecks the wallet-lock authorization before submitting QuickPay.
Low: changelog.d/next/1962.fixed.md:1 describes the fix as applying to Spark Lightning payments generally. Manual Paykit requests still run ensurePaymentAllowed before journaling and supply no submission check (AppViewModel.kt:8250, LightningRepo.kt:1603). Scope the changelog to QuickPay and change Closes #1962 to Part of #1962 so the Paykit case stays open.
Review details
Quick review
Model: Claude Opus 5.5
Reasoning effort: high
General review
Model: GPT-6.2 Sol
Reasoning effort: high
Combined review
Model: GPT-6.2 Sol
Reasoning effort: high
Only QuickPay's wallet-lock check runs again at submission; a Paykit request's authorization is still checked once, so the entry says so. Verified: none (changelog text only)
@pwltr Done in 3abdd06: the changelog is scoped to QuickPay and the wallet lock, and this PR is now |
pwltr
left a comment
There was a problem hiding this comment.
Reviewed 3abdd06. Spark rechecks QuickPay’s wallet-lock authorization after writing the send journal and before calling the SDK. If the wallet locks during those writes, QuickPay releases the reservation and returns to swipe confirmation. The changelog now describes that behavior, and #1962 remains open for the Paykit case. No findings.
Review details
Quick review
Model: Claude Opus 5.5
Reasoning effort: high
General review
Model: GPT-6.2 Sol
Reasoning effort: high
Combined review
Model: GPT-6.2 Sol
Reasoning effort: high
The late authorization refusal from #2053 now rolls back through dropSparkUnsentSend, which is already best effort. Verified: SparkRepoTest, CacheStoreTest, LightningRepoTest, just lint
Part of #1962
Description
onBeforeSend) ran before the send's journal writes (addSparkSendHash,setSparkSendAttempt,addSparkUncertainSend), which suspend.SparkRepo.payInvoicenow runs it again right beforesparkService.send, next to the deadline and exit checks.ServiceError.PaymentStoppedAtSubmission, as the node rail does on its queue, and rolls the journal back the same way the late deadline and exit refusals do; a failed cleanup write there is logged and the refusal still reaches the caller. QuickPay already handles that error: it releases the payment and falls back to the confirm screen.LightningRepo's side-effect-freeisAuthorizedAtSubmission, the same check Spark's leaf-selection retry already reruns. Only QuickPay passes one, so a manual send rechecks nothing at submission, on either rail, as before.SparkManager.sendruns thebeforeSubmissioncheck synchronously insidespends.run, right beforesdk.sendPayment, with no await between them.Out of Scope
SparkRepo.withdraw: the on-chain contact leg runsonBeforeSendbefore its journal write too. That check starts a payment proof, so it can't simply run twice.ensurePaymentAllowed) is still checked once, before the journal writes, as on master;isAuthorizedAtSubmissionis a plain() -> Booleanand that check suspends, so this stays open on bug: spark send can dispatch on a stale authorization check #1962.Design
N/A — no UI changes.
Preview
N/A — no user-visible changes.
QA Notes
Setup
N/A — no device testing required: the window is a few cache writes wide and needs an authorization change inside it, which unit tests reach and a device can't time.
Journeys
N/A — no user-visible behaviour change.
Manual Tests
N/A
Automated Checks
SparkRepoTest.kt— a send whose authorization lapses while its journal is written never leaves, and the journal rolls backSparkRepoTest.kt— the leaf-selection retry test counts the new check at submissionEvidence
SparkRepo.ktand passes with this change.just compile,just test,just lint; after the review fixes,SparkRepoTest,LightningRepoTest,QuickPayRepoTestandjust lint.Gaps
None.
Models used
Claude Opus 5.5(reasoning:medium)Claude Opus 5.5(reasoning:medium),Claude Sonnet 5.5(reasoning:high)gemini-4-neon-high(reasoning:Not exposed)The review record names only the model id, with no separate reasoning setting.