Repository navigation
fix: show cached connections while node restarts - #1756
Conversation
Codex Review SummaryThis 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; no actionable issues were found. SummaryKeeps the last known Lightning channels visible while the node restarts.
Diagram%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Read channels from running node] --> B[Update lastKnownChannels]
B --> C[Show connections and details]
D[Node stops] --> E[Clear lightningState channels]
D --> F[Keep lastKnownChannels]
F --> G[Show cached connections and syncing row]
H[Wallet wipe] --> I[Clear lastKnownChannels]
Reviews (1) · Last reviewed commit: "fix: show cached connections while node ..." · Reviewed by Greptile |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 18d82ef058
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
Milestone 3.2.0: opened after the 3.1.0 cut and not on its must-have list. It can still join a 3.1.0 RC2 if picked for one. |
talosmachina
left a comment
There was a problem hiding this comment.
No findings. Keeps Lightning Connections, Connection details and Close Connection on a lastKnownChannels snapshot while the node stops and restarts, with an inline syncing row instead of SyncNodeView. Reviewed 6d91c99, full tier, static: per this repo's track rules an auto review does not build an Android branch here, so the review is reasoned from the code and from CI.
Merge confidence: 4/5, no findings, but the unit-test build job and the e2e matrix were still running when this was posted and nothing was built locally.
What I checked, and 6 candidates I ruled out
Read in full: LightningConnectionsViewModel.kt, LightningConnectionsScreen.kt (Content and the new row), CloseConnectionViewModel.kt, the changed paths of ChannelDetailViewModel.kt, and in LightningRepo.kt start, stopLocked, wipeStorage, syncState, refreshChannelsAndPeers, executeWhenNodeRunning, getChannels.
Writers of lastKnownChannels: only readChannels(), reached from syncState() and refreshChannelsAndPeers(), both only with the node running; cleared in wipeStorage before lightningService.wipeStorage. stopLocked resets lightningState to a fresh LightningState and leaves the snapshot, as the PR intends.
Other readers of lightningState.channels (send sheet, receive QR, savings, health, WalletRepo, ActivityRepo): unchanged, still empty during a restart, as the body says.
CI at review time: build-local, detekt, lint and detect-changes green; unit-test build and e2e pending.
Ruled out
- Close on a stale snapshot:
closeChannelgoes throughexecuteWhenNodeRunning, which fails fast withNodeNotRunningErrorwhile Stopping/Stopped (canRun()is false) and waits for Running while Starting, so a cachedChannelDetailsnever reaches LDK with the node down; the failure path shows the existing toast. - Start fails and the syncing row stays forever: on
ErrorStartingthe old screen showed the full-screen spinner forever too; the PR changes what is shown, not whether it resolves. - Pull to refresh while the node is down:
refreshObservedStatecallslightningRepo.sync(), which returns a failedResultrather than throwing, so the coroutine and the refresh indicator end normally. - Snapshot leaking across a wallet wipe: cleared under
lifecycleMutexinwipeStorage, covered by the newlastKnownChannels should be empty after storage wipetest. - Empty state with only closed channels:
hasChannelsignores closed channels, so that user still getsSyncNodeViewrather than the onboarding, which is the stated intent. - Changelog fragment (Codex P1 on
18d82ef): added in6d91c99aschangelog.d/next/1756.fixed.md.
Reviewer model: claude-opus-5-5
Reasoning effort: Not exposed (the run metadata available to this reviewer does not carry an effort setting)
Regtest APKDownload bitkit-dev-debug universal APK (expires in 30 days). |
coreyphillips
left a comment
There was a problem hiding this comment.
Two independent reviews, nothing blocking a merge.
worth doing, does not block
- Channel read moved outside
_lightningState.updatecan publish a stale list (app/src/main/java/to/bitkit/repositories/LightningRepo.kt:2136-2146) - Concurrent refreshes can overwrite newer channel state (app/src/main/java/to/bitkit/repositories/LightningRepo.kt:2137-2143)
jvsena42
left a comment
There was a problem hiding this comment.
Code scan at 6d91c99: no findings. I reviewed it as a funds-adjacent change, because Close Connection now looks the channel up in the cache.
Checked and clean:
- Close Connection on a cached channel. While Stopped or Stopping,
closeChannelfails fast throughexecuteWhenNodeRunningwithNodeNotRunningError: a toast, no transfer record, nothing partial. While Starting it waits for Running with the button spinner. Back during that wait cancels before LDK is called. The close usesuserChannelId+counterpartyNodeId, which don't change for a channel, so a cached entry resolves to the same channel. Closing a channel that closed while the node was down returns an LDK error, andcreateTransferruns only on success. lastKnownChannelslifetime. It is written only byreadChannels()from a running node and cleared inwipeStorageunderlifecycleMutexafter the stop. Create and restore only run after a wipe or on a fresh process, so it can't outlive its wallet. A failed restart shows the cached list with "Syncing..." where master showed "Connecting to network". That isn't worse.- Back and cancel paths. List → Detail → Back while the node comes up; Detail → Close → Back; a channel that disappears on restart. Each settles without a stale row: the detail screen falls back to closed channels or pops.
- State uses
MutableStateFlowwithupdate {}andImmutableList. - iOS: no change needed.
LightningConnectionsView.swifthas no node-running gate, and iOS doesn't stop the node on background. The cross-platform row injourneys/README.mdrecords this. - Not re-raised: coreyphillips' two threads on the
readChannels()/ CAS ordering inLightningRepo. The same window also covers a wipe that completes between the read and the commit. I'll leave that on his thread.
Device gate (partial): 6d91c99 — not run yet. Pending: connections-during-restart.xml; Close tapped while Stopping and while Starting, plus Back during Starting; time to the Connections list after a >5 s background, compared with master, on a multi-channel wallet. Device results will follow.
|
Pushed 18d2226: channel reads go back inside |
|
Pushed 559a670: |
coreyphillips
left a comment
There was a problem hiding this comment.
Rechecked after 2 new commits (18d2226, 559a670). Nothing blocking remains.
addressed
- Concurrent refreshes can overwrite newer channel state (app/src/main/java/to/bitkit/repositories/LightningRepo.kt:2137-2143). Fixed: Same lines as point 1.
stopLocked()commits Stopping at :708 beforelightningService.stop(), which makes any in-flightsyncState()retry, and on retrygetChannels()returns null, so the Stopped reset at :711-713 can no longer be repopulated with the old channels. The publish at :2253-2256 keeps the cached list while the node is not Running.
still open
- Channel read moved outside
_lightningState.updatecan publish a stale list (app/src/main/java/to/bitkit/repositories/LightningRepo.kt:2136-2146).syncState()andrefreshChannelsAndPeers()now callreadChannels()before_lightningState.update { ... }, so a stale channel snapshot can overwrite a fresher one. On master,getChannels()ran inside the update lambda. If another writer committed first, the CAS failed and the lambda re-read the node. Now the captured list is reused on retry. The same applies to_lastKnownChannels, which is set before the commit. From reading the code, a failing interleave is: caller X reads channels C_old. LDK appliesChannelReady. Caller Y reads C_new, publishes it and commits. X then publishes C_old tolastKnownChannelsand commits C_old tolightningState.channels. Concurrent callers exist:handleLdkEventlaunchessyncState()onChannelReady,WalletRepo.refreshBip21ForEventLockedcallssyncState()for the same event,SendSheetpollsrefreshChannelsAndPeers()every 1 s, andsync()ends insyncState(). The two flows can also end up holding different lists until the next read. The window is short: X's channel read has to come before Y's, and X's commit after Y's. It corrects itself on the next read. It also affectslightningState.channels(canSend, therefreshMaxSendLightningonRefreshedcallback, receive readiness), not just the new screen. I did not reproduce this; it comes from comparing the two versions ofupdate. One way to restore the old behaviour is to keep the read inside the lambda and publish the committed value:kotlin val state = _lightningState.updateAndGet { it.copy(channels = getChannels().orEmpty().toImmutableList(), ...) } if (state.nodeLifecycleState.isRunning()) _lastKnownChannels.update { state.channels }. Codex still sees it: The live-state reads are fixed, but cache publication at app/src/main/java/to/bitkit/repositories/LightningRepo.kt:2253-2255 can still publish stale channels. A controlled test of the extracted update body repopulated the cache after stop and wipe because clearing an already empty cache did not force the delayed publisher to retry.
new since the last review
worth doing, does not block
- Publishing from committed state can replace the cache with an empty list at start (app/src/main/java/to/bitkit/repositories/LightningRepo.kt:2253-2256)
|
Device gate (partial): 6d91c99 (CI APK), on Pixel_9 with a fresh wallet and 5 channels to the staging LSP. All times are UTC.
Close Connection mid-restart:
One observation, not caused by this PR: on one background cycle the debounced stop didn't start for 2 min 47 s. Still running: time to the Connections list after a >5 s background, compared with master, and idle sync counts. |
|
Pushed 93e906b: |
coreyphillips
left a comment
There was a problem hiding this comment.
Rechecked after 1 new commit (93e906b). Nothing blocking remains.
addressed
- Channel read moved outside
_lightningState.updatecan publish a stale list (app/src/main/java/to/bitkit/repositories/LightningRepo.kt:2136-2146). Fixed:syncState()andrefreshChannelsAndPeers()now callgetChannels()inside theupdateAndGetlambda (LightningRepo.kt:2120-2125, :2138-2146). A failed CAS reruns the lambda and re-reads the node, as on master.publishLastKnownChannels()(:2254-2257) runs after the commit and publishes_lightningState.value.channelsinside_lastKnownChannels.update, so a list captured earlier can no longer overwrite a newer one. I confirmed this by reading the HEAD code andgit show 18d22262c, which moved the read back inside. - Publishing from committed state can replace the cache with an empty list at start (app/src/main/java/to/bitkit/repositories/LightningRepo.kt:2253-2256). Fixed: LightningRepo.kt:2126 and :2147 now publish only when the caller's committed state from
updateAndGetis Running. A sync that commits while Starting seescommitted.nodeLifecycleStateas Starting and skips the publish, so thestartNodeRunning commit at :411 can no longer let it empty the cache. A Running commit cannot carry an unread empty list:getChannels()reads the current lifecycle, and any change between that read and the CAS fails the CAS and reruns the lambda.adoptRunningNode()(:505) leaves the cache alone. I confirmed this by tracing each Running write (:411, :506, :722) against the new guard.
new since the last review
worth doing, does not block
- New sync-while-stopped test passes with or without 93e906b (app/src/test/java/to/bitkit/repositories/LightningRepoTest.kt:1456-1467)
|
Device gate (partial): perf at 6d91c99, against master cb59453, on the same Pixel_9 wallet (3 open and 2 closed channels; master installed over the same data). All times are UTC.
The head has moved to 93e906b, which changes how |
|
Device gate (partial): 93e906b (CI run 41492179270), the same Pixel_9 wallet with 3 open and 2 closed channels. All times are UTC.
Still running: the PR side of the timing on 93e906b. |
jvsena42
left a comment
There was a problem hiding this comment.
Approving at 93e906b. The device gate found nothing above LOW.
The new commits (18d2226, 559a670 and 93e906b) fill lastKnownChannels from the same updateAndGet commit and publish only while the node is still running. A stop or wipe between the read and the commit can no longer republish a stopped node's channels, which covers coreyphillips' two threads. They read clean, and I re-ran every device check whose path they touch.
Device gate: 93e906b (app code; the close-while-Starting and Back checks ran at 6d91c99, a delta that doesn't touch them) — journeys: connections-during-restart.xml 9/9 passed at both heads; Close while Stopping refused with no transfer; Close while Starting held its spinner, then closed with COOP_CLOSE; Back during Starting closed nothing; after "Node started" the list matches the node (channels='3'); perf: Connections rows in the first layout dump after a >5 s background (+35–87 ms, with "Syncing...") vs master 12.0–15.4 s ("Connecting to network" until "Node started"), stop 1.1–6.4 s on both, idle Home calls equal (0 syncs/min on both); Figma: no frame for the syncing row; l10n: no new strings; iOS: not applicable, iOS doesn't stop the node on background.
The Close-while-Stopping toast is the generic "Transfer Failed / Unable to transfer your funds back to savings. Please try again." The PR body says Close "reports the node-not-running error"; the log does, the toast doesn't. That's wording, not a finding.
Two things seen during the gate, both on master as well and not caused by this PR:
- A background stop can be held for minutes.
eventHandledfor aSyncCompletedevent blocks while backgrounded on a failing network, so "Stopping node…" only runs when the app returns. - A background during the initial sync keeps
start()holding the lifecycle lock until the sync times out.
|
Merged: approved by @coreyphillips and @jvsena42 on 93e906b, checks green, every review thread answered and resolved, and the device run of |
This PR keeps Lightning Connections usable while the node restarts, showing the last known connections with an inline syncing row instead of the full-screen "Connecting to network" view.
Description
LightningRepo.lastKnownChannels, the channels from the last read of a running node, so screens can keep showing them while every return from the background stops and restarts the node (the stop alone takes 35 to 42 s on devices). It is published after each channel read whose committed state was Running, and cleared on wallet wipe;lightningState.channelsstill empties on stop, as its other readers expect.SyncNodeView. The full-screen view stays only when the node is down and nothing is known yet, so a user with channels is never shown the "no connections" onboarding.connections-during-restart.xmljourney.Out of Scope
ui/sheets/SendSheet.kt,ui/screens/wallets/send/*: the send screens keepSyncNodeView, since sending needs the running node.bitkit-ios:LightningConnectionsView.swiftalready has no node-running gate, so no iOS change.lightningState.channels(balances, Advanced settings counts) still see an empty list during a restart.Design
N/A — no design available for the syncing state. The screen's frame is
Settings > Advanced › Lightning Connections overviewon the latest Handoff page; this PR only adds the inline "Syncing..." row (existing spinner and the app status "Syncing..." string).Preview
Return from the background while the node restarts: the list stays with "Syncing...", a channel opens mid-restart, then the row clears (2x speed).
ln-connections-restart.mp4
QA Notes
Setup
Dev debug build (
to.bitkit.dev, regtest against the staging LSP), onboarded, with one open channel opened by Manual Setup to the staging LSP node (journeys/README.md, Backend preconditions): fund with./lspdeposit and mine, then Receive › Spending › Continue › paste the LSP node URI, 200 000 sats, swipe, and mine 6 blocks.Journeys
connections-during-restart.xml— after a return from the background the list stays with a "Syncing..." row, a channel row opens its details mid-restart, and the row goes away once the node runsManual Tests
N/A
Automated Checks
LightningConnectionsViewModelTest.kt— keeps the last known channels through a node stop and restartLightningRepoTest.kt— last known channels survive a stop and a later sync while stopped, and are cleared on wipe; the race of a sync committed during Starting is guarded byupdateAndGetand not covered by a testChannelDetailViewModelTest.kt— resolves the channel from the last known listEvidence
18d82ef05, Android emulator (API 35), dev debug build, regtest with the staging LSP: passed. Logcat shows "Stopping node…" at +5 s after Home, "Node stopped" 6.5 s later, "Node started" 1 s after that; the tap into "Connection 1" landed between "Node stopped" and "Node started" and opened its details. No "Connecting to network" view at any point. Recording under Preview.just compile,just test,just lintpass locally on18d82ef05; on18d22262c,559a6707cand93e906b42compile, the changed test files and lint pass, and CI covers the rest.Gaps
18d22262c,559a6707cand93e906b42, which only move the channel reads back inside the state update and publish the cached list after it, under the node-running check.executeWhenNodeRunningfails with the existing toast), not driven on a device.Models used
claude-opus-5-5(reasoning:10)claude-opus-5-5(reasoning:10)claude-sonnet-5-5(reasoning:10)