Skip to content

fix: keep a late node stop off its replacement - #1417

Merged
ovitrif merged 1 commit into
masterfrom
fix/event-loop-owner
Nov 25, 2026
Merged

ovitrif merged 1 commit into
masterfrom
fix/event-loop-owner

Conversation

@ovitrif

@ovitrif ovitrif commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

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

  • The event loop slot now records the node its loop serves. A stop, a failed start() or a node change cancels and waits for only that node's loop. Before, a stop() that finished after a restart cancelled the new node's loop, leaving it running with no listener, and a failed start() 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 .lightning together under setupLock. A stop that finishes after its node was released or replaced leaves the replacement's loop, lock, callback and channels alone.
  • A stop with no node no longer unlocks .lightning: during setup() that lock is the setup's. A native stop that fails, other than NotRunning, 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() throws nodeNotSetup when 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.
  • waitForEnd no longer reports a finished task as timed out: a timer whose sleep was cancelled reports nothing.

Out of Scope

Design

N/A — no UI changes.

Preview

N/A

QA Notes

Setup

Regtest build.

Journeys

N/A — no user-visible behaviour change.

Manual Tests

  • 1. Restart the node 5 times (Settings → Dev Settings → LDK → Restart), then receive a Lightning payment → the payment shows once and the node keeps receiving events

Automated Checks

  • added LightningServiceTests cases: waitForEnd reports an ended task as ended over 200 runs, and a stop() with no node leaves the .lightning lock to its holder. The node-scoped loop ownership needs a real Node and has no unit test.

Evidence

  • Unit suite (CI test selection) on an iPhone 17 simulator, iOS 26.5, ldk-node 0.7.0-rc.81: all passed on b658529.

Gaps

    1. not run.

Models used

  • Planning/scoping: Claude Opus 5.5 (reasoning: medium)
  • Implementation: Claude Opus 5.5 (reasoning: medium)
  • Review: gemini-4-neon-high (reasoning: Not exposed)
  • Review rounds: 2

The pre-PR review run records only its model id.

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
@ovitrif ovitrif added this to the 3.2.0 milestone Nov 24, 2026
@ovitrif ovitrif self-assigned this Nov 24, 2026
@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-24T22:31:49.150805Z b658529 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 removed this from the 3.2.0 milestone Nov 24, 2026
@ovitrif

ovitrif commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

Moved the 3.2.0 milestone off this PR: #1399, which this PR closes, already carries it, and the release milestone tracks issues.

@greptile-apps

greptile-apps Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 4/5

[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

  1. P1 Late stop clears replacement callback ▶

Summary

The PR ties event loops to their nodes and keeps late stops from releasing a replacement's lock.

  • Each Lightning node keeps its own event listener through restarts.
  • A late stop leaves the replacement node’s state and lock alone.
  • Event-loop waits report an ended task instead of a timeout.

Acknowledged scope: ovitrif explicitly deferred writes that finish after the five-second wait until the upstream fix is pinned.

Diagram

sequenceDiagram
    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
Loading

Reviews (1) · Last reviewed commit: "fix: keep a late node stop off its repla..." · Reviewed by Greptile

Comment thread Bitkit/Services/LightningService.swift Resolved

@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. 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: listenForEvents takes setupLock then eventLoopLock, the same order as the configuredNode didSet (always set under setupLock); nothing takes eventLoopLock then setupLock, and the loop body reads self.node outside eventLoopLock.
  • A failed native stop now keeps .lightning locked: the only other context, the notification extension, never starts the node, and StateLocker.lock lets the same context re-lock, so a later setup() or relaunch is not blocked; restart and recovery release it via releaseAfterFailedStop.
  • A stop with no node no longer unlocking stale locks: a stale file from a previous process is the same foregroundApp context, which lock() re-takes.
  • cancelEventLoop(unlessServing: nil) on release cancelling another node's loop: releaseAndUnlock only clears configuredNode when 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 concurrent restart() released the node first, leaving the callback set: needs a wipe concurrent with a direct LightningService.restart(), and stopForWipe then fails on hasNode once 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 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 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

@ovitrif

ovitrif commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

Low: Both restart paths read the configured node separately from stop().

@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 stop() returns.

Pre-existing Low candidate: start() can pass the listener ownership check and then submit native startup after a concurrent restart replaces its node.

Out of scope: tracked in #1423, because it predates this PR.

@ovitrif

ovitrif commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

Merging: approved on b658529, CI green, threads resolved; the two Lows are tracked in #1422 and #1423.

@ovitrif
ovitrif merged commit cd906e9 into master Nov 25, 2026
22 checks passed
@ovitrif
ovitrif deleted the fix/event-loop-owner branch November 25, 2026 10:06
@ovitrif ovitrif removed this from the 3.2.0 milestone Nov 25, 2026
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 failed node start can leave the running node without an event loop

3 participants