Skip to content

fix: close spark retry hold gaps - #1406

Merged
ovitrif merged 1 commit into
masterfrom
fix/ldk-send-in-flight-count
Nov 24, 2026
Merged

ovitrif merged 1 commit into
masterfrom
fix/ldk-send-in-flight-count

Conversation

@ovitrif

@ovitrif ovitrif commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1405
Twin: synonymdev/bitkit-android#2014

Description

  • Fixes a node send of an invoice returning and clearing the in-flight flag of a repeat node send of the same invoice still queued, because the in-flight node sends were a set of payment hashes. They are now counted per hash, so a Spark attempt of the invoice stays refused while any node send of it is live, and a later Spark attempt can no longer pay it a second time.
  • Fixes a node send refused before admission ending another send's in-flight entry, because the send path ended its hash whether or not it had been admitted.

Out of Scope

  • LightningService: the Spark rail's in-flight sends stay a set, as a second Spark send of one invoice is already refused when it begins.
  • Other duplicate-payment checks (attempt records, listings, uncertain Spark sends) are unchanged.

Design

N/A — no UI changes.

Preview

N/A

QA Notes

Setup

N/A — no device testing required: the race needs two concurrent node sends of one invoice and can't be driven from the UI.

Journeys

N/A — no user-visible behaviour change.

Manual Tests

N/A

Automated Checks

  • added ChannelFreeRailTests.swift — a Spark attempt stays refused after one of two node sends of the invoice returns

Evidence

  • ChannelFreeRailTests at 8ad1e70 on an iPhone 17 Pro simulator, iOS 26: all 116 tests pass. With master's LightningService.swift and this branch's tests, the new test fails (the Spark attempt goes out).
  • swiftformat --lint passes on the changed files.

Gaps

  • The full unit suite was not run locally; CI runs it.

Models used

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

Node sends in flight are counted per payment hash, so one node send of
an invoice returning no longer clears the flag a repeat node send of
the same invoice still relies on, and a Spark attempt stays refused
while any of them is live. A send refused before admission no longer
ends another send's entry.

Verified: ChannelFreeRailTests on an iOS 26 simulator (new test fails
with master's LightningService, passes with the fix); swiftformat --lint
on the changed files.
@ovitrif ovitrif added this to the 3.2.0 milestone Nov 24, 2026
@ovitrif ovitrif self-assigned this Nov 24, 2026
@ovitrif
ovitrif requested review from jvsena42 and pwltr November 24, 2026 15:02
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 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-24T15:03:53.289456Z 8ad1e70 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.

@ovitrif ovitrif changed the title fix: keep each node send in flight until it ends fix: close spark retry hold gaps Nov 24, 2026

@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. Counts node sends in flight per payment hash, so one node send of an invoice returning no longer clears a repeat node send still queued, and only an admitted node send ends its entry. Reviewed 8ad1e70, full tier, static review, the branch was not built.

What I checked, and 4 candidates I ruled out

Read in full: LightningService.send(bolt11:) both rails, admitSend, isSendLive, attemptMark, attemptsLive, beginLdkSend/endLdkSend, submitPayment, ServiceQueue.background
Call sites traced: every reader and writer of ldkSendsInFlight (all in this file; beginLdkSend is only called from tests), and the tests that pair beginLdkSend/endLdkSend
CI: SwiftFormat, validate and detect-changes green; Run Tests, Integration Tests and build-local still pending at review time

Ruled out

  • Read of isAdmitted racing the write on the ldk queue: the flag is set inside the queue block and read in the defer after the continuation resumes, so the resume orders the two; the Spark path already uses the same captured-flag pattern with isRefusedAtAdmission.
  • A throw after admission leaving the count up: after isAdmitted = true only noteAttempt and recordLdkAttempt run, neither throws, and any throw from send() still reaches the defer.
  • A count reaching zero left as a key: endLdkSend removes the key at the last decrement, and every membership read (Set(keys), [hash] != nil) sees only live counts.
  • Under-count from an end with no begin: endLdkSend returns when the hash is absent, and the refused path no longer calls it.

Merge confidence: 4/5, the change is covered by testANodeSendEndingLeavesARepeatNodeSendOfTheInvoiceInFlight but the unit suite on CI had not finished when this was posted.

Reviewer model: Claude Opus 5.5
Reasoning effort: Not exposed (the review runner does not report it)

@ovitrif ovitrif removed this from the 3.2.0 milestone Nov 24, 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 8ad1e70. Each admitted LDK send keeps its own in-flight entry until cleanup, and a send refused before admission leaves that entry alone.

Low: The overlap test at BitkitTests/ChannelFreeRailTests.swift:481 enters and leaves the count through test helpers. It would still pass if production admission stopped incrementing the count or a refused send decremented it. Extend the test through the LDK send path and check that both a completed send and a send refused before admission leave another same-invoice send holding Spark.

Pre-existing Low candidate: The history-based releases at Bitkit/Services/LightningService.swift:1538 and Bitkit/Services/LightningService.swift:1566 can remove a newer same-invoice attempt because they do not revalidate the listing's generation under the removal lock. If the newer send returns before the other rail's final admission, that admission can lose both the in-flight check and the recorded-attempt check. This requires a narrow overlap between payments of the same invoice on both rails. Whether both can settle remains unverified.

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 24, 2026

Copy link
Copy Markdown
Collaborator Author

@pwltr

Low: The overlap test at BitkitTests/ChannelFreeRailTests.swift:481 enters and leaves the count through test helpers.

A follow-up PR, opened once this one merges, admits both sends through admitSend and checks that the last one to return clears the count. The case of a send refused before admission stays untested here, because the LDK send path needs a node and unit tests run without one.

Pre-existing Low candidate: The history-based releases at Bitkit/Services/LightningService.swift:1538 and Bitkit/Services/LightningService.swift:1566 can remove a newer same-invoice attempt

Confirmed. In the same follow-up, both releases skip a hash that is in flight or was recorded since the listing's mark, as releaseRailAttempt(since:) already does for SparkManager.

@ovitrif

ovitrif commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

Merging: approved at 8ad1e70 with all checks green. The node-side release guard and its admitSend-based test move to the iOS follow-up of #1355, together with #1407.

@ovitrif
ovitrif merged commit cdfbca1 into master Nov 24, 2026
22 checks passed
@ovitrif
ovitrif deleted the fix/ldk-send-in-flight-count branch November 24, 2026 17:24
@ovitrif

ovitrif commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

The node release guard (releaseLdkAttempt skipping a hash in flight or recorded since the listing's mark) is in #1411. The in-flight count test stays with this PR, as it needs this PR's count.

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 finishing ldk send clears another in-flight send of the same invoice

3 participants