Repository navigation
fix: keep a late node stop off its replacement - #1417
Conversation
The event loop slot records the node it serves. A stop, a failed start or a node change cancels and waits only for that node's loop, so a stop that finishes after a restart leaves the new node's loop, event callback and channel cache alone. stop() clears the callback and channel cache while its node is still configured, then releases the node and unlocks the lightning lock together, only if the node is still the configured one. A late, node-less or failed stop no longer unlocks the lock under a setup() or a node still running. A restart releases a node whose stop failed only after its loop ended, and a start whose node was replaced does not start it. waitForEnd() no longer reports an ended task as timed out when its timer is cancelled. Closes #1399 Verified: swiftformat --lint; unit suite (CI selection) on iPhone 17 simulator, iOS 26.5, passed
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. |
|
Moved the 3.2.0 milestone off this PR: #1399, which this PR closes, already carries it, and the release milestone tracks issues. |
[Medium impact] The changes affect Lightning node event delivery and cleanup within the wallet workflow. The callback clear needs the same ownership protection as node release before this is safe to merge. Findings
SummaryThe PR ties event loops to their nodes and keeps late stops from releasing a replacement's lock.
Acknowledged scope: DiagramsequenceDiagram
participant Late as Late stop A
participant Restart as Other stop and restart
participant Service as LightningService
Late->>Service: isConfigured(A) returns true
Restart->>Service: Release A and configure B
Restart->>Service: Start B with shared callback
Late->>Service: clearEventCallback()
Note over Service: B remains configured, but shared callback is nil
Reviews (1) · Last reviewed commit: "fix: keep a late node stop off its repla..." · Reviewed by Greptile |
talosmachina
left a comment
There was a problem hiding this comment.
No findings. Scopes the event loop slot to the node it serves, so a late stop(), a failed start() or a node change ends only its own node's loop and releases the node and the .lightning lock together under setupLock; also fixes waitForEnd reporting a finished task as timed out. Reviewed b658529, full tier, static.
What I checked, and 6 candidates I ruled out
Read in full: LightningService.swift lifecycle (configuredNode, setup, restart/restartNode/restartWithPreviousConfig, start, stop, wipeStorage, listenForEvents, endEventLoop, waitForEnd), StateLocker.swift, WalletViewModel.stopNodeLocked/stopForWipe, BitkitNotification/NotificationService.swift, the new tests.
Call sites traced: every stop, restart, StateLocker.lock/unlock(.lightning) caller in Bitkit and BitkitNotification.
Build skipped: iOS does not build on this Linux host, so this is reasoned from the code. CI: SwiftFormat, validate and Greptile green; Run Tests, Run Integration Tests and build-local still pending at review time.
Ruled out
- Lock order deadlock:
listenForEventstakessetupLocktheneventLoopLock, the same order as theconfiguredNodedidSet(always set undersetupLock); nothing takeseventLoopLockthensetupLock, and the loop body readsself.nodeoutsideeventLoopLock. - A failed native stop now keeps
.lightninglocked: the only other context, the notification extension, never starts the node, andStateLocker.locklets the same context re-lock, so a latersetup()or relaunch is not blocked;restartand recovery release it viareleaseAfterFailedStop. - A stop with no node no longer unlocking stale locks: a stale file from a previous process is the same
foregroundAppcontext, whichlock()re-takes. cancelEventLoop(unlessServing: nil)on release cancelling another node's loop:releaseAndUnlockonly clearsconfiguredNodewhen it is the stopping node, so the current loop can only be that node's.- Late
stop(clearEventCallback: true)from a wipe returning early when a concurrentrestart()released the node first, leaving the callback set: needs a wipe concurrent with a directLightningService.restart(), andstopForWipethen fails onhasNodeonce the restart sets up; too narrow to post. - Callback cleared after a replacement was configured: already raised inline by Greptile, not repeated.
The node-scoped ownership itself has no unit test, as the PR body says; manual test 1 is unrun.
Merge confidence: 4/5, the concurrency change has no test that exercises two nodes, and the unit suite had not finished on CI when this was written.
Reviewer model: claude-opus-5-5
Reasoning effort: Unknown (the review run does not record it)
pwltr
left a comment
There was a problem hiding this comment.
Reviewed b658529. The event-loop changes keep a late stop from cancelling the replacement’s listener.
Low: Both restart paths read the configured node separately from stop(). If a wallet lifecycle operation replaces the node between those reads and the replacement’s native stop fails, cleanup targets the old node and setup() skips the rebuild. The primary path can then report a successful Electrum switch while still running on the old server. The recovery path can skip rebuilding against the default server. See Bitkit/Services/LightningService.swift:397 and Bitkit/Services/LightningService.swift:432.
Pre-existing Low candidate: start() can pass the listener ownership check and then submit native startup after a concurrent restart replaces its node. That leaves the old instance without a listener if native startup succeeds alongside the replacement. See Bitkit/Services/LightningService.swift:465 and Bitkit/Services/LightningService.swift:471.
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
@pwltr out of scope: tracked in #1422, because it needs a replacement node built mid-restart whose native stop then fails, and fixing it means changing what
Out of scope: tracked in #1423, because it predates this PR. |
Closes #1399
Refs: #1397
Keeps a node stop, a failed start or a node change from acting on another node's event loop, lock or caches.
Description
start()or a node change cancels and waits for only that node's loop. Before, astop()that finished after a restart cancelled the new node's loop, leaving it running with no listener, and a failedstart()could cancel a loop another start installed.stop()clears the event callback and channel cache only while its node is still configured, then releases the node and unlocks.lightningtogether undersetupLock. A stop that finishes after its node was released or replaced leaves the replacement's loop, lock, callback and channels alone..lightning: duringsetup()that lock is the setup's. A native stop that fails, other thanNotRunning, leaves the node configured and keeps the lock while that node may still run;restart()and its recovery release it only after its loop ended.start()throwsnodeNotSetupwhen its node was replaced before its loop started, instead of starting a node nothing holds. A new loop also waits for a loop of the same node that a stop is still ending.waitForEndno longer reports a finished task as timed out: a timer whose sleep was cancelled reports nothing.Out of Scope
Bitkit/Services/LightningService.swift: restart paths can release a stale node when a replacement's native stop fails; tracked in bug: electrum switch keeps the old server after a failed replacement stop #1422 (review).Bitkit/Services/LightningService.swift:start()can start a node a concurrent restart replaced, pre-existing; tracked in bug: a replaced node can start without an event listener #1423 (review).Design
N/A — no UI changes.
Preview
N/A
QA Notes
Setup
Regtest build.
Journeys
N/A — no user-visible behaviour change.
Manual Tests
Automated Checks
LightningServiceTestscases:waitForEndreports an ended task as ended over 200 runs, and astop()with no node leaves the.lightninglock to its holder. The node-scoped loop ownership needs a realNodeand has no unit test.Evidence
Gaps
Models used
Claude Opus 5.5(reasoning:medium)Claude Opus 5.5(reasoning:medium)gemini-4-neon-high(reasoning:Not exposed)The pre-PR review run records only its model id.