Skip to content

xpay: return success immediately in slow mode; let a success outrank pending stragglers - #9724

Open
cdecker wants to merge 3 commits into
masterfrom
2026m11-xpay-fast-success
Open

cdecker wants to merge 3 commits into
masterfrom
2026m11-xpay-fast-success

Conversation

@cdecker

@cdecker cdecker commented Nov 10, 2026

Copy link
Copy Markdown
Member

Summary

xpay-slow-mode exists so a caller who retries a failed payment (maybe from another node) can't have our straggler HTLCs collected alongside theirs and overpay. That concern doesn't apply to success: once we hold the preimage, the payment is final, and any later attempt on that payment_hash is answered from the wallet without sending an HTLC. So slow mode now only delays failure; success returns at once.

That reasoning only holds if lightningd really does answer "already paid" while a straggler is still pending. It didn't: check_progress bailed on the first PENDING row from another group, and rows are scanned in HTLC-creation order, so an earlier straggler could hide a later success.

 check_progress(payment_hash, group)
   for row in payments_by_hash()          # HTLC-creation order
     case COMPLETE: have_complete = true
     case PENDING if row.groupid != group:
-      fail PAY_IN_PROGRESS               # shadows a later COMPLETE row
+      pending_other_group = true
   if have_complete
     fail PAY_RHASH_ALREADY_USED  (or hand back stored preimage)
+  if pending_other_group
+    fail PAY_IN_PROGRESS
 payment_succeeded(xpay)
-  if cmd && should_finish_command()      # slow mode: wait for all parts
+  if cmd                                 # success is final: return now
     return { preimage, amount_sent (in-flight counted optimistically) }

No HTLC is sent in any of the have_complete paths, so moving the "other group in progress" check after it can't mix HTLCs from two groups.

Evidence

New test test_pay.py::test_retry_after_success_pending_straggler:

part 1 (via l2, commits disabled)  -> stuck PENDING, created first
part 2 (via l3, full amount)       -> COMPLETE
sendpay(partid=2, groupid=2)       -> complete, preimage returned, no HTLC
sendpay(partid=1, groupid=2)       -> "Already succeeded"
listsendpays                       -> still 2 rows
  • Before: both retries fail with Payment with groupid=1 still in progress, cannot retry before that completes.
    After: retries return the preimage or "Already succeeded"; no new HTLC.

test_xpay.py::test_fail_after_success[slow_mode=True]:

  • Before: xpay blocks until the stuck part times out (13 blocks later), then returns successful_parts: 1, failed_parts: 1.
    After: returns immediately with successful_parts: 2, same as slow_mode=False. A retry during the stuck part is never reported as "still in progress" and sends no HTLC.

Merge Danger

Door: two-way

Behaviour only, no DB or wire changes. Revert restores the old semantics.

Blast Radius: API callers

  • xpay-slow-mode users who relied on success waiting for all parts now get an optimistic amount_sent_msat / successful_parts, as in default mode.
  • sendpay/pay retries on a paid hash with a pending straggler now get 201 PAY_RHASH_ALREADY_USED (or the preimage) instead of 200 PAY_IN_PROGRESS. Callers that treat 200 as "wait and retry" now get a final answer sooner.

🤖 Generated with Claude Code

cdecker added 2 commits November 10, 2026 13:51
…_progress.

The "another payment group is still in progress" check fired as soon as
it saw a PENDING payment row of a different group.  But payments_by_hash()
returns rows in HTLC-creation order, so a straggler part created *before*
a later, successful part would shadow that success: retrying an
already-paid payment_hash could get "still in progress, cannot retry"
instead of the "already succeeded" answer, depending purely on which
part we created first.

Since a success is final (we return the stored preimage without sending
an HTLC, or refuse with "Already succeeded other parts"), the group
check is moot once anything completed: in every one of those cases no
HTLC is sent, so HTLCs from the other group cannot mix with ours and
overpay.  Instead, collect have_complete and pending_other_group while
scanning, and only report PAY_IN_PROGRESS if nothing has succeeded.

Changelog-Changed: pay: retrying a payment_hash where a part already succeeded now reports "Already succeeded" even if other parts are still pending.
Signed-off-by: cdecker <cdecker@blockstream.com>
xpay-slow-mode was added so that a caller who might retry a *failed*
payment (possibly from another node!) does not do so while our straggler
HTLCs could still be collected alongside theirs, overpaying the invoice.

For success this concern does not apply: once we hold the preimage, the
payment is final.  Any future attempt to pay this payment_hash is
answered by lightningd out of the wallet without sending an HTLC
(and the previous commit guarantees a pending straggler can no longer
shadow that answer), so it can never overpay, no matter when our
stragglers resolve.

Thus slow mode now only waits for all parts before returning failure;
a successful xpay returns as soon as a part succeeds, reporting
in-flight parts optimistically as the default mode already does.

Changelog-Changed: xpay: `xpay-slow-mode` now only waits until all parts have completed before returning failure: success is always returned immediately.
Signed-off-by: cdecker <cdecker@blockstream.com>
@cdecker
cdecker requested a review from Lagrang3 as a code owner November 10, 2026 15:36

@agammann agammann left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Deferring the pending_other_group error until after the payment-row scan is necessary here: the earlier pending straggler can otherwise hide a later successful part. The new sendpay test forces that row order, checks the retry returns the stored preimage, and asserts the payment count stays at two, so it covers both precedence and the no-new-HTLC safety condition.

@Lagrang3 Lagrang3 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ack

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.

3 participants