Skip to content

fix: keep spark refusals when cleanup fails - #2062

Open
ovitrif wants to merge 5 commits into
masterfrom
fix/spark-cleanup-refusals
Open

ovitrif wants to merge 5 commits into
masterfrom
fix/spark-cleanup-refusals

Conversation

@ovitrif

@ovitrif ovitrif commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #2060

Description

  • Fixes a refused Spark send blocking retries of the same invoice until it expires. Rolling back a send that never went out took three separate CacheStore writes (hold, hash, attempt); if a later one failed, the hash stayed without its hold, so every retry on Spark or the node saw a pending send and failed with PaymentPendingException.
  • CacheStore.dropSparkUnsentSend now drops the hold, the hash and, under a new key, the attempt in one updateData write, replacing restoreSparkSendAttempt. A failed write leaves the whole send held, as a send cut off mid-way is, and the in-memory hold is dropped only after the write succeeds.
  • SparkRepo.payInvoice's dropUnsentSend is now best effort, so every pre-send refusal (expired invoice, short balance, late deadline or exit) reaches the caller even when its cleanup write fails, and QuickPay falls back instead of treating it as an unknown outcome.
  • SparkRepo.withdraw closes its journal best effort on its refusals before sending (short balance, fee rise, late deadline or exit), so those refusals reach the caller too; clearJournal was already one write.
  • When the drop's write fails, the whole send stays held until its invoice expires, as any send with an unknown outcome does, so a retry of that invoice waits for it.
  • The late authorization refusal from fix: recheck spark send authorization at submission #2053 goes through the same dropUnsentSend, so it is covered once both are on master.
  • No iOS twin: on bitkit-ios master the matching rollback in LightningService.swift is three UserDefaults.set calls, which neither throw nor fail, and its expired-invoice, insufficient-funds and fee refusals throw before anything is recorded, so a cleanup failure can't replace them.

Design

N/A — no UI changes.

Preview

N/A — no user-visible changes.

QA Notes

Setup

N/A — no device testing required: the bug needs a storage write to fail between two others, which unit tests reach and a device can't.

Journeys

N/A — no user-visible behaviour change.

Manual Tests

N/A

Automated Checks

  • added SparkRepoTest.kt — a short-balance refusal stays a refusal when dropping the unsent send fails
  • added SparkRepoTest.kt — a withdrawal refused for its balance or a fee rise stays refused when closing its journal fails
  • added CacheStoreTest.kt — dropping an unsent send clears its hold and hash and keeps a reused attempt
  • added CacheStoreTest.kt — the drop is one store write, and a failed write leaves the whole held send in place
  • updated SparkRepoTest.kt and CacheStoreTest.kt — the rollback is checked through dropSparkUnsentSend

Evidence

  • The refusal test, written against master's separate writes (removeSparkSendHash failing), fails on master: the IOException replaces SparkError.SendRefused.
  • Local verification: SparkRepoTest, CacheStoreTest, QuickPayRepoTest and just lint.

Gaps

  • A retry after a failed drop is not tested end to end: SparkRepoTest mocks CacheStore, so the held state it leaves is covered by the CacheStoreTest case above instead.

Models used

  • Planning/scoping: Not used
  • Implementation: Claude Opus 5.5 (reasoning: medium), Claude Sonnet 5.5 (reasoning: high)
  • Review: Not performed

@ovitrif ovitrif self-assigned this Nov 26, 2026
@ovitrif
ovitrif requested review from jvsena42 and pwltr November 26, 2026 09:48
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T09:49:23.801716Z 986fe08 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@greptile-apps

greptile-apps Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 4/5 · The PR appears safe to merge, with a non-blocking gap in tests for failed cache writes.RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change is limited to Spark payment refusals and their retry path.

The PR appears safe to merge, with a non-blocking gap in tests for failed cache writes.

Findings

  1. P2 Partial writes lack test coverage ▶

Summary

This PR makes cleanup of an unsent Spark payment use one cache write and preserves the original refusal when that write fails.

  • Spark callers still receive refusals when cleanup fails.

Reviews (1) · Last reviewed commit: "fix: keep spark refusals when cleanup fa..." · Reviewed by Greptile

@talosmachina talosmachina left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No findings. Makes the rollback of a Spark send refused before it went out a single CacheStore write (hold, attempt id, hash, expiry and, under a new key, the attempt), drops the in-memory hold only after that write lands, and makes the rollback best effort so the refusal reaches the caller. Reviewed 986fe08b, light tier on a payments path, files read whole.

Build skipped: Bitkit Android PR branches are not built here (Gradle would run the branch's code with a token in its environment), so this review is reasoned from the code and from CI. CI was still running at review time.

Merge confidence: 4/5, no findings, but CI had not finished and the change was not built or run here.

What I checked, and 4 candidates I ruled out

Read in full: SparkRepo.payInvoice through settleSentResult, bestEffort, sendHold; CacheStore Spark send writers (addSparkSendHash, removeSparkSendHash, addSparkUncertainSend, removeSparkUncertainSend, setSparkSendAttempt, dropSparkUnsentSend).
Call sites traced: dropUnsentSend (deadline/exit check inside spending, expired invoice, short balance); restoreSparkSendAttempt has no remaining callers in app/.

Ruled out

  • Field parity with the old three writes: dropSparkUnsentSend clears sparkUncertainSends, sparkSendAttemptIds, sparkSendHashes and sparkSendExpiries, the same fields removeSparkUncertainSend and removeSparkSendHash cleared; the attempt branch matches the old restoreSparkSendAttempt gate (!isReusedKey), and restoresAttempt = false leaves the map untouched.
  • Reordering the in-memory removal after the write: on a failed write the cache still holds the hold and attempt id written for this attempt, so keeping uncertainSends in step with it is consistent with the stated intent, not a leak.
  • Deadline path: a failing rollback inside spending's onFailure used to replace PaymentDeadlineExpired with the storage error; it is now logged and the deadline error is rethrown.
  • Cancellation swallowed by bestEffort: it goes through runSuspendCatching, the same wrapper the settlement path already uses for these writes.

Reviewer model: claude-opus-5-5
Reasoning effort: Not exposed (the runtime reports no named effort level for this run)

@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Regtest APK

Built from d7089a4 (run).

Download bitkit-dev-debug universal APK (expires in 30 days).

pwltr
pwltr previously approved these changes Nov 26, 2026

@pwltr pwltr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 986fe08.

Low: A failed rollback can still block retrying the same Lightning invoice. If Spark refuses a fresh send for insufficient funds and the cleanup write fails, SparkRepo.kt:663 preserves the refusal while leaving the saved hold in place. The Paykit send screen offers a retry. If storage recovers and channel liquidity is now sufficient, SparkRepo.kt:2037 still reports the invoice as pending with an empty Spark payment list, so LightningRepo.kt:1818 stops before sending. This can block the invoice until expiry despite the retry promise in changelog.d/next/2060.fixed.md:1. Extend the refusal test to fail the cleanup write against a real journal, restore writes, and retry the same invoice through the channel path.

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

@ovitrif

ovitrif commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

643d52e adds a CacheStoreTest case: dropping an unsent Spark send is one store write, and a failed write leaves the whole held send in place. This PR goes in after #2053, whose rollback tests it then updates to dropSparkUnsentSend.

@ovitrif

ovitrif commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

Low: A failed rollback can still block retrying the same Lightning invoice. [...] This can block the invoice until expiry despite the retry promise in changelog.d/next/2060.fixed.md:1.

@pwltr Changed the changelog in 5c7ba4c instead: when the drop itself fails, the journal on disk still holds the send, and a later reader can't tell it never left, so it stays held until its invoice expires, as any unknown outcome does. Freeing it there would risk paying twice. The entry now promises only that the refusal shows.

7811b5f also keeps a Spark withdrawal's refusal (short balance, fee rise, late deadline or exit) when closing its journal fails, with tests for the first two.

@ovitrif
ovitrif requested a review from pwltr November 26, 2026 10:49
A Spark send refused before anything went out now drops its journal in
one atomic CacheStore write, so a failed write leaves the whole send held
instead of a hash without its hold that blocks retries until the invoice
expires. The cleanup is best effort, so the refusal (expired invoice,
short balance, late deadline or exit) still reaches the caller.

Verified: SparkRepoTest (new test fails on master), CacheStoreTest, QuickPayRepoTest, just lint
A store that fails its write leaves the whole held send in place, and a
successful drop takes exactly one write.

Verified: CacheStoreTest, just lint
A cleanup write that fails keeps the whole send held until its invoice
expires, so the entry no longer promises an immediate retry.

Verified: none (changelog text only)
A withdrawal Spark refuses before anything left (short balance, fee
rise, late deadline or exit) now reaches the caller even when closing
its journal fails, so the refused leg's proof and list are released.

Verified: SparkRepoTest (short-balance test fails before the change), just lint
The late authorization refusal from #2053 now rolls back through
dropSparkUnsentSend, which is already best effort.

Verified: SparkRepoTest, CacheStoreTest, LightningRepoTest, just lint
@ovitrif
ovitrif force-pushed the fix/spark-cleanup-refusals branch from 7811b5f to d7089a4 Compare November 26, 2026 11:03
@ovitrif

ovitrif commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

Rebased on master after #2053 merged. d7089a4 moves that PR's late authorization refusal onto dropSparkUnsentSend: its tests now check the one write and a failing drop, and the outer bestEffort it no longer needs is gone.

@pwltr pwltr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed d7089a4. The rebase onto master brings #2053's late authorization refusal under the same atomic, best-effort rollback, and its tests now check that a failing write still returns the refusal. Nothing else changed. No issues found.

Review details

General review
Model: Claude Opus 5.5
Reasoning effort: high

This branch has not been deployed

No deployments
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.

bug: a failed spark cleanup write blocks retrying an unsent payment

3 participants