Repository navigation
fix: report rejected spark exit data as not restored - #1979
Conversation
The wallet's start waits for the restore, so Spark wasn't connected when the restore staged its exit data and the restore reported it restored before Spark could reject it. The restore now connects Spark and imports the data at once, and staging and importing share a lock. Verified: the new SparkRepoTest connect cases fail without the connect and pass with it; just compile, just test, detekt and lintDevDebug pass.
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 changes affect Spark exit-data handling during wallet backup restore. Start collecting Spark events before the restore connects it before merging. FindingsSummaryThe restore now connects Spark before reporting whether its exit data was imported, and shares a lock with startup imports.
Reviews (1) · Last reviewed commit: "fix: report rejected spark exit data as ..." · Reviewed by Greptile |
talosmachina
left a comment
There was a problem hiding this comment.
No findings. Has the restore connect Spark itself and import the staged exit data right away, so a rejection shows up as SPARK_EXIT not restored, and puts staging and import behind one lock. Reviewed d1ecf28, full tier, stacked on #1957 (fix/spark-follow-ups), so only the delta against that base was reviewed.
What I checked, and 5 candidates I ruled out
Read in full: SparkRepo.restoreExitBackup, connectForRestore, importRestoredExitState, start, importStagedExitState, discardStagedExitState, SparkService.connect/disconnect, the SPARK_EXIT step of BackupRepo.performFullRestoreFromLatestBackup, performRestore, uploadSparkExitBackup, and the exitStateGeneration listener
Call sites traced: restoreExitBackup (BackupRepo only), importRestoredExitState (start, restoreExitBackup, importStagedExitState), sparkRepo.start (LightningRepo, SparkExitRepo)
Build: not built here. On this repo the review is reasoned from the code and CI, without running the branch's Gradle build
CI: lint, build-local, detect-changes green; build-and-test and ui-tests still running at review time
Ruled out
- Deadlock on
exitImportMutex: Kotlin'sMutexis not reentrant, butrestoreExitBackupreleases it after the write and before callingimportRestoredExitState, and no other path holds it around that call. - Start and restore both connecting:
SparkService.connectruns under its own mutex and returns false oncesdkis set, so the laterstart()neither reconnects nor bumps_exitStateGenerationa second time;connectForRestorebumped it once, and theStateFlowreplays it whenBackupRepostarts observing. - Start's import deleting data staged after it: the write now waits on the lock until the import's
file.delete()is done, and a rejection thatstart()swallows is hit again by the restore's own import, which then reports it. - Connect failures escaping the restore:
connectForRestorecatchesWipeInProgress,MnemonicNotFoundand SDK errors, so the restore falls back to staging, covered by the new can't-connect test. - Events emitted between the restore's connect and
start(): the collector only starts instart()andSparkService.eventshas no replay. Already raised inline by Greptile, so not repeated here. It looks reachable to me: leaf optimization can start on that connect, andstart()runs only after the remaining restore categories.
Merge confidence: 4/5, the dropped-events point Greptile raised is still open and build-and-test had not finished.
Reviewer model: claude-opus-5-5
Reasoning effort: Not exposed (the review runtime does not report a setting)
Regtest APKDownload bitkit-dev-debug universal APK (expires in 30 days). |
|
Triage: this PR closes #1976, in the 3.2.0 milestone. It fixes the restore result for Spark exit data, which ships in 3.2.0 with #1957. |
coreyphillips
left a comment
There was a problem hiding this comment.
Two independent reviews.
worth doing, does not block
- Spark connected by the restore drops SDK events until start() runs (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:1372-1376).
connectForRestoreis now the only path that connects Spark beforestart()has launched the event collector, so every SDK event in between is lost.SparkService.connect(SparkService.kt:174-199) attaches the listener that emits into_events, aMutableSharedFlowwith no replay. Its only subscriber is launched bystart()(SparkRepo.kt:282-284), and the wallet start runs that only after the restore finishes the WALLET, BLOCKTANK and ACTIVITY categories. With no subscriber,emitdrops the value. The restored wallet's first sync emits payment, deposit-claim,AutoOptimizationandUnilateralExitStateChangedevents in that window. Most of the loss is recovered:start()syncs, refreshes and syncs activities, and the restore's own bump of_exitStateGenerationstill triggers the exit backup. Optimization state is not recovered. A leaf optimization that started in the gap leavesisOptimizingfalse andsparkOptimizationActivityAtunstamped, soawaitOptimizationIdle()andneedsSettling()won't wait for it before an exit quote. Before this change, every connect (start,setExiting,reconnectForExit) ran after the collector existed. I found this by reading the code and did not reproduce it on a device. One fix is to move theisCollectingEvents.compareAndSetlaunch into a helper and call it fromconnectForRestoreas well. - Can't-connect test would pass even if no connect was attempted (app/src/test/java/to/bitkit/repositories/SparkRepoTest.kt:2677-2687).
a restore spark can't connect for keeps its exit data staged for spark's startchecks only that the import never ran and the file stayed staged. If the newisSupported && !isConnectedbranch were removed or never reached, both assertions would still hold, so the test doesn't show that the restore tried to connect and recovered from the failure. Addingverify(sparkService).connect(0, false)would close that gap. The lock that the description says stops a restore and Spark's start from importing at once also has no test.
nits
- Changelog covers the out-of-scope case it doesn't fix (changelog.d/next/1976.fixed.md:1). The fragment reads 'when Spark can't import it'. A restore that can't connect Spark also can't import, and it still reports the exit data as restored, as the PR's Out of Scope says. 'when Spark rejects it' would match what changed.
the reviewers disagree, your call
- Cold restore drops Spark events before startup (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:1366-1375). Objection: Real, but non-blocking rather than blocking: This is a real regression.
connectForRestore(new in this PR) attaches the SDK listener during the SPARK_EXIT restore step, butsparkService.eventshas replay 0 and no collector untilSparkRepo.start()subscribes, andWalletViewModel.startwaits for the whole restore (waitForRestoreIfNeeded), so anyAutoOptimization.Startedin that gap is dropped andisOptimizing/needsSettling()stay false. The impact is narrow: an exit has to be quoted during the first optimization round right after a cold restore, and a later RoundCompleted/Cancelled event corrects the state. The fix is cheap: start collecting events beforeconnectForRestoreconnects. - Spark can suppress a confirmed failure for the current send (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:596-602). Objection: Real, but non-blocking rather than blocking: Confirmed. When
earlierAttemptIdsreturns null (before the first full sync, sincecanListPayments()needshasSynced, or when the list read fails), the taken attempt stores onlysentAt. The id from the PENDINGpaymentis never kept, so the send's own failure dated withinRAIL_CLOCK_SKEW_SECONDSmatchesisEarlierFailureOfUncertainSend. That makesonPaymentreturn early,lightningSendreport PENDING, and retries on both rails stay blocked untildropExpiredTakenAttemptsruns at invoice expiry. No funds are lost and it clears itself at expiry, but it is a reachable correctness bug. Fix: record the returned payment id and never treat a failure carrying that id as an earlier attempt's.
Spark's listener drops events no one collects, and the wallet's start collected them only after the restore, so events after the restore's connect were lost. The restore now starts the collection first, which subscribes before it returns. The can't-connect test also checks the connect, and the changelog says rejects. Verified: the new SparkRepoTest events case fails without the early collection and passes with it; just compile, just test and just lint pass.
|
@coreyphillips all four from your review:
Addressed in a90f5e7. The event collection moved into one helper that
Addressed in a90f5e7: the test now verifies
Addressed in a90f5e7: it now says "when Spark rejects it".
Addressed in b34fa0d on #1957, where that code lives. A taken attempt now keeps the id of the payment Spark returned or listed as pending, and a failure with that id is never read as an earlier attempt's. A new test fails without it. |
coreyphillips
left a comment
There was a problem hiding this comment.
Rechecked after 1 new commit (a90f5e7). 2 blocking findings the reviewers disagree about.
addressed
- Changelog covers the out-of-scope case it doesn't fix (changelog.d/next/1976.fixed.md:1). Fixed: changelog.d/next/1976.fixed.md:1 now reads "when Spark rejects it", which matches the change and leaves out the can't-connect case.
still open
- Spark connected by the restore drops SDK events until start() runs (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:1372-1376).
connectForRestoreis now the only path that connects Spark beforestart()has launched the event collector, so every SDK event in between is lost.SparkService.connect(SparkService.kt:174-199) attaches the listener that emits into_events, aMutableSharedFlowwith no replay. Its only subscriber is launched bystart()(SparkRepo.kt:282-284), and the wallet start runs that only after the restore finishes the WALLET, BLOCKTANK and ACTIVITY categories. With no subscriber,emitdrops the value. The restored wallet's first sync emits payment, deposit-claim,AutoOptimizationandUnilateralExitStateChangedevents in that window. Most of the loss is recovered:start()syncs, refreshes and syncs activities, and the restore's own bump of_exitStateGenerationstill triggers the exit backup. Optimization state is not recovered. A leaf optimization that started in the gap leavesisOptimizingfalse andsparkOptimizationActivityAtunstamped, soawaitOptimizationIdle()andneedsSettling()won't wait for it before an exit quote. Before this change, every connect (start,setExiting,reconnectForExit) ran after the collector existed. I found this by reading the code and did not reproduce it on a device. One fix is to move theisCollectingEvents.compareAndSetlaunch into a helper and call it fromconnectForRestoreas well. Codex still sees it: The sequential restore receives connect-time events. With overlapping calls, I reproducedstart()setting the flag atSparkRepo.kt:1373before subscribing, then the restore connecting with no subscriber. - Can't-connect test would pass even if no connect was attempted (app/src/test/java/to/bitkit/repositories/SparkRepoTest.kt:2677-2687).
a restore spark can't connect for keeps its exit data staged for spark's startchecks only that the import never ran and the file stayed staged. If the newisSupported && !isConnectedbranch were removed or never reached, both assertions would still hold, so the test doesn't show that the restore tried to connect and recovered from the failure. Addingverify(sparkService).connect(0, false)would close that gap. The lock that the description says stops a restore and Spark's start from importing at once also has no test. The connect half is fixed: SparkRepoTest.kt:2685 now verifiesconnect(0, false). The lock is still untested. The reply says one test thread can't force two imports to overlap, but that doesn't hold. A suspendingimportExitStatestub that awaits aCompletableDeferredcan holdstart()'s import insideexitImportMutexwhile a launchedrestoreExitBackupwaits to write. Completing the deferred then shows whether the restore's data survives and gets imported. - Cold restore drops Spark events before startup (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:1366-1375). A cold restore can drop Spark’s leaf optimization start event, so an emergency exit can quote while a leaf swap is still active.
connectForRestore()connects beforestart()subscribes to the SDK event flow, which has no replay. I ran the base and branch connection paths with a service that emitsAutoOptimization.Startedduring connection. The base recorded the event. On HEAD,needsSettling()stayed false. Codex still sees it: I reproduced a connect-time optimization start reaching the collector on the sequential path. When startup paused between setting the flag and subscribing atSparkRepo.kt:1373-1374, the restore dropped that event and left the optimizer flag false. - Spark can suppress a confirmed failure for the current send (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:596-602). A failed Spark send can stay pending until its invoice expires even when Spark reports the failure for that exact payment. If the pre-send history read fails,
payInvoice()keeps the start time and discards the payment ID from thePENDINGresponse. A failure with that same ID can then be treated as an older attempt. I ran the event and lookup code from both revisions with a matching failure timestamped one second after submission. On HEAD, the failure event was suppressed, Activity received no update, and the settled lookup returned null. Codex still sees it: The fix cited inb34fa0ddais absent from this checkout.SparkRepo.kt:599-603still discards the pending payment ID, so the timestamp check at line 1986 can suppress a failure for that payment.
new since the last review
worth doing, does not block
- Restore handles Spark events before held sends are restored (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:1377-1379)
- Early Spark failure events can clear a restored retry (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:1377-1380)
A restore's connect now takes back the sends held before it and waits until Spark's events are subscribed, so a start on another thread can't leave a connect without a collector and an earlier attempt's failure can't end a restored retry. Adds a test for the import lock. Verified: the new lock and held-retry SparkRepoTest cases fail without their fixes and pass with them; just compile, just test and just lint pass.
|
@coreyphillips on your recheck:
Addressed in ae27cfd.
Addressed in ae27cfd: a new test holds the start's import of older staged data inside the lock while a restore stages its own, and checks that the restore's data is kept and imported. Without the lock around the write it fails.
This branch now merges #1957's branch, so |
coreyphillips
left a comment
There was a problem hiding this comment.
Rechecked after 5 new commits (2979510, b34fa0d, 76a7355, and 2 more). Nothing blocking remains.
addressed
- Spark connected by the restore drops SDK events until start() runs (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:1372-1376). Fixed:
connectForRestore(SparkRepo.kt:1398-1405) now callscollectEvents()beforesparkService.connect.collectEvents(1389-1396) suspends oneventsSubscribeduntilonSubscriptionfires, andstart()waits on the same signal (285), so the listener attached inSparkService.connectalways has a subscriber. The test at SparkRepoTest.kt:2748 covers it, and CI unit tests pass at ae27cfd. - Can't-connect test would pass even if no connect was attempted (app/src/test/java/to/bitkit/repositories/SparkRepoTest.kt:2677-2687). Fixed: SparkRepoTest.kt:2685 verifies
connect(0, false), and the new test at 2690-2712 holds the start's import of older data insideexitImportMutexwhile a restore stages its own. I traced that test without the lock on the write: the start'sfile.delete()removes the new data andverify(importExitState("new"))fails. CI unit tests pass at ae27cfd. - Cold restore drops Spark events before startup (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:1366-1375). Fixed: The subscription is now awaited before the restore's connect (SparkRepo.kt:1402-1403), and
_eventsbuffers with a suspendingemit(SparkService.kt:146, 191). AnAutoOptimization.Startedemitted during that connect therefore reachesonOptimization(SparkRepo.kt:451-456) and setsisOptimizing. I confirmed this by reading the code. No test emits during the connect itself. - Spark can suppress a confirmed failure for the current send (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:596-602). Fixed:
payInvoicekeeps the PENDING payment's id asownId(SparkRepo.kt:601) and persists it throughkeepSparkSendAttempt(648).restoreUncertainSendsreads it back after a restart (794-801).isEarlierFailureOfUncertainSendreturns false for that id before the earlier-id and clock-skew rules (2005), soonPaymentandsettledSendEventno longer hide the send's own failure. The tests at SparkRepoTest.kt:3823-3862 cover the live and restart cases and pass in CI. - Restore handles Spark events before held sends are restored (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:1377-1379). Fixed:
connectForRestorecallsrestoreUncertainSends()at SparkRepo.kt:1401, beforecollectEvents(). METADATA is restored before SPARK_EXIT (BackupRepo.kt:1155, 1190), soisOutrunByRetrysees the restored attempts. The test at SparkRepoTest.kt:2714-2746 sends an earlier attempt's failure after the restore's connect, then checks the hold and its saved start survive. It passes in CI. - Early Spark failure events can clear a restored retry (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:1377-1380). Fixed: Same change as point 6: SparkRepo.kt:1401 loads the saved attempts before the collector starts.
onPaymenttherefore treats the earlier id as outrun and returns beforeremoveSparkUncertainSend. SparkRepoTest.kt:2714-2746 checks that noPaymentFailedis emitted and the start is kept.
new since the last review
worth doing, does not block
- A late pending write can recreate a settled Spark send hold (app/src/main/java/to/bitkit/data/CacheStore.kt:209-217). A late write of a pending payment ID can recreate a send hold after Spark has confirmed the failure. Let
onPayment()clear the journal and expiry before the pending path reacheskeepSparkSendAttempt()atSparkRepo.kt:648or:721. I ran the cache and restore methods in isolation and confirmed that the write recreates the attempt on restart with no expiry. The stale entry then makes private Paykit treat the failed invoice as already attempted.
|
Merging this PR into its base, fix/spark-follow-ups, with a merge commit: it is approved and green at ae27cfd, with every thread resolved. That keeps the approval: bringing it up to date with master would have needed a push here. #1957 now carries this change and merges it into master once approved. |
Closes #1976
This PR stacks on bitkit-android#1957, which adds the handling of exit data Spark rejects. Its base moves to
masteronce that PR merges.A wallet restore reported Spark's exit data as restored as soon as it was staged. Spark imported it only later, at its start, so onboarding showed a complete restore even when Spark then rejected the data.
Description
Out of Scope
Design
N/A — no UI changes. The restore screen already lists categories that were not restored.
Preview
N/A
QA Notes
Setup
N/A — no device testing required: the case needs a VSS backup holding exit data Spark rejects, which neither a journey nor the app can stage.
Journeys
N/A — not drivable; see Manual Tests.
Manual Tests
N/A
Automated Checks
SparkRepoTest.kt— a restore made before Spark connects connects it and fails when Spark rejects its exit data, imports data Spark accepts once, keeps the data staged when Spark can't connect, after it tried, handles Spark's events after a restore's connect, keeps a held retry past an earlier attempt's failure during the restore, and keeps and imports a restore's data staged while the start imports older dataEvidence
just compile,just testandjust lintpass.Gaps
Models used
Claude Opus 5.5(reasoning:medium)Claude Opus 5.5(reasoning:medium)Claude Sonnet 5.5(reasoning:high)