Skip to content

fix: keep a stopped node's late writes off its rebuilt node - #2148

Merged
ovitrif merged 7 commits into
masterfrom
fix/retry-server-change-busy-store
Nov 27, 2026
Merged

ovitrif merged 7 commits into
masterfrom
fix/retry-server-change-busy-store

Conversation

@ovitrif

@ovitrif ovitrif commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #2147
Twin: synonymdev/bitkit-ios#1521
Companion: synonymdev/bitkit-ios#1529
Refs: #2004

Description

  • Fixes a valid Electrum or RGS server being rejected while the stopped node's VSS writes are still in flight. Since ldk-node 0.7.0-rc.82 (fix: keep a stopped node's late writes off its rebuilt node #2004) such a build fails with KvStoreSetupFailed after waiting up to 60 s, which has nothing to do with the new server. The change now retries its own start up to 5 times, never past a requested stop, before falling back to the previous config, matching fix: keep a stopped node's late writes off its rebuilt node bitkit-ios#1521.
  • Keeps a stop requested during the change's first build: the change reads the stop counter right after its own stop, and the recovery of the previous config skips when a stop arrived since, after a failed change and after a cancelled one (leaving the screen mid-change). A stop requested while a build succeeds also wins: the change rechecks the counter after setup and does not start the node or save the server.
  • Releases a node built with a changed server when its start yields to a stop, fails or is cancelled, so a later start (for example a foreground start that cancelled a background stop) rebuilds from the saved server instead of reusing the rejected one.
  • A config-change attempt builds its own node: it releases any handle another start left before it builds, and fails if it cannot. A cancelled change stops through the repository's stop transition, so Stopping is published before its node is released.
  • Renames RECOVERY_START_ATTEMPTS to BUSY_STORE_START_ATTEMPTS, since the server change uses it too.

Out of Scope

None.

Design

N/A — no UI changes.

Preview

N/A

QA Notes

Setup

Dev (regtest) debug build on a Pixel 10 Pro emulator, Android 16, against staging, with a new wallet.

Journeys

  • J1: updated cancelled-node-restart.xml — Back during an RGS change's start releases its node, recovery restarts the saved server, and a Lightning payment still arrives

Manual Tests

  • 1. Settings → Advanced → Rapid-Gossip-Sync → append / to the URL → Connect → the log shows Node stopped, Starting node with new RGS server, Node started and Successfully changed RGS server.
  • 2. Same screen → Reset To Default → Connect → the node restarts on the default URL the same way.

Automated Checks

  • added restartWithElectrumServer retries its own start while the vss store is busy to LightningRepoTest: the new server's first two builds fail on a busy store, the third starts, the change succeeds and the previous config never starts.
  • added restartWithElectrumServer keeps a stop requested during its first build (a direct stop during the first build means no retry, no recovery start and no running node), restartWithElectrumServer cancelled after a stop does not recover the previous config, restartWithElectrumServer keeps a stop requested during a build that succeeds, restartWithElectrumServer releases its node when a cancelled background stop arrived during setup, restartWithElectrumServer cancelled after setup releases its node, leaves no node on the rejected server after a cancelled background stop for both servers, restartWithElectrumServer retry builds its own node over a handle another start left, restartWithElectrumServer cancelled while running publishes Stopping before releasing its node, restartWithElectrumServer recovery keeps a stop requested during its build, start keeps the node running when a cancelled background stop arrived during the graph reset rebuild and restartWithRgsServer retries its own start while the vss store is busy.
  • ran just compile, just test and just lint.

Evidence

  • 1 and 2 passed at 6059cd2: each change stopped the node, rebuilt it on the same VSS store and started it on the first attempt; recording in the QA comment. Later commits only change how a stop during the change is honoured, a path 1 and 2 do not reach.

Gaps

  • A VSS write still in flight past the 60 s wait cannot be triggered on demand in the app, so the busy-store retry is covered by the unit test only.

An Electrum or RGS change whose build fails because the stopped node's VSS writes are still in flight now retries its own start, up to 5 times and never past a requested stop, before falling back to the previous config.

Verified: just compile, just test, just lint; emulator run of two RGS server changes (dev debug, Android 16).
@ovitrif ovitrif added this to the 3.2.0 milestone Nov 27, 2026
@ovitrif ovitrif self-assigned this Nov 27, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 27, 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-27T10:00:01.783795Z 6059cd2 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 commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator Author

QA

Tested 6059cd2, a dev (regtest) debug build on a Pixel 10 Pro emulator, Android 16, against staging, with a new wallet.

  • Node start: passed. The new wallet's node started.
  • 1. RGS URL changed to …/rgs/snapshot/: passed. The change stopped the node, rebuilt it on the same VSS store with the new URL, started it on the first attempt and saved the server (Successfully changed RGS server).
  • 2. Reset To Default: passed. The node restarted on the default URL the same way.
  • just compile, just test and just lint pass, including the new busy-store test for the change's own start.

server-change2x.mp4

Not covered: a VSS write still in flight past the 60 s wait cannot be triggered on demand in the app, so the busy-store retry is covered by the unit test only.

@ovitrif
ovitrif requested review from a team, ben-kaufman and piotr-iohk and removed request for a team November 27, 2026 09:58

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6059cd20bd

ℹ️ 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".

@greptile-apps

greptile-apps Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 4/5 · Fix the stop-counter snapshot before merging so retries cannot override a stop requested during the first build.RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change affects Lightning node restarts for Electrum and RGS server settings.

Fix the stop-counter snapshot before merging so retries cannot override a stop requested during the first build.

Findings

  1. P1 Retry overrides a requested stop ▶

Summary

Server changes now retry the requested Electrum or RGS configuration when the VSS store is busy.

  • Electrum and RGS server changes retry when the VSS store is busy.

Reviews (1) · Last reviewed commit: "fix: retry a server change on a busy vss..." · Reviewed by Greptile

@piotr-iohk piotr-iohk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QA review

Scope: full PR diff against its merge base, at 6059cd2.

No new actionable code findings.

No new actionable code findings. startConfigChange retries an Electrum or RGS change's own start only when the build fails with KvStoreSetupFailed. It stops retrying when a debounced background stop is pending (pendingStopJob), and a direct stop() between retries makes the next startNode give way through stopRequestsSeen. The settings value is still saved only after a successful start. The existing recovery tests (debounced stop during the change's build, direct stop during recovery) stay valid with the new loop, and the new test covers the Electrum busy-store retry. The RGS path uses the same helper.

Existing concern (pre-existing, not counted as a PR defect): the open Codex and Greptile threads point out that stopRequestCount is read only after the first build (LightningRepo.kt:1096). A direct stop() that arrives during that build, for example from LightningNodeService (which does not set pendingStopJob), is therefore not respected by the retries. This is accurate, but the base code already loses that stop. At the base, the failed change starts restartWithPreviousConfig, which runs its own stop(), takes a new counter reading and restarts the previous config (LightningRepo.kt:1341-1367, unchanged here). So the node ends up running either way, and only the server it runs on differs. Reading the counter before the first start(), as the threads suggest, would not change that on its own: the change would return a yield error and the recovery would still restart the node. A complete fix means passing the change's counter reading into the recovery. The iOS twin does this: stopsBefore is captured before the change's first attempt and passed into restartWithPreviousConfig. A focused issue search found no existing ticket.

Parity: compared with synonymdev/bitkit-ios#1521 at 3ca8946. Both platforms use 5 attempts, retry only on a busy store, let a requested stop win between attempts, and fall back to the previous config once retries run out. The only difference is when the stop counter is read, as described above.

Recommended before device testing: reply on the two bot threads, either fixing the counter reading in both the change and its recovery or explaining why the base behavior is acceptable.

Validation and limits: this review is source analysis only. The unit tests were read, not run, because no Gradle dependency cache was available. The author reports just compile, just test and just lint passing, and manual cases 1 and 2 passing at this head (QA comment). Those cases only exercise a first-attempt success. The busy-store retry cannot be triggered in the app on demand, so only the unit test covers it. With retries, the Electrum and RGS screens can now show a loading spinner for up to about 5 builds of up to 60 s each before an error appears, versus one build before. This matches iOS and is treated as intended.

Device testing: not performed in this review.

Suggested additional test cases

  • Android unit (LightningRepoTest): make the new server's first build launch a direct sut.stop() and then fail with KvStoreSetupFailed after a delay. Expected, if the stop is meant to win: no further setup with the new URL, no setup with the previous config, and the node not Running.
  • Android unit: restartWithRgsServer with the first two builds failing on a busy store. Expected: three setups with the new RGS URL, rgsServerUrl saved, and the previous config never started.

Reviewer model: claude-opus-5-5
Reasoning effort: high

@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Regtest APK

Built from a45ca2a (run).

Download bitkit-dev-debug universal APK (expires in 30 days).

@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.

1 finding (1 P1). Retries the Electrum/RGS server change's own start while the build fails with KvStoreSetupFailed, instead of rejecting the server and falling back to the previous config. Reviewed 6059cd20b, full tier, static.

What I checked, and 3 candidates I ruled out

Read in full: startNode/start, stop/stopDebounced/cancelPendingStop, restartWithElectrumServer, restartWithRgsServer, startConfigChange, restartWithPreviousConfig, recoverOnCancellation in LightningRepo.kt; the new test in LightningRepoTest.kt.
Call sites traced: ElectrumConfigViewModel.connectToServer, RgsServerViewModel.onClickConnect; every direct lightningRepo.stop() (LightningNodeService, AppViewModel, WalletViewModel, WipeWalletUseCase).
Build: not built or run here; Android branches are reviewed from code and CI only. CI was still running at review time (build-local green, unit tests, lint and e2e pending).

Ruled out

  • Stop counter captured after the first attempt: already raised inline on line 1096 by two bots; not repeated.
  • Retry loses the first start's cancelPendingStop(): the first attempt goes through start(), which cancels a pending debounced stop as before; retries pass shouldCancelPendingStop = false and check pendingStopJob first, matching recovery.
  • Recovery and change racing: both hold configChangeMutex, and recovery is launched after the change's critical section returns, so it waits for the lock.

Merge confidence: 3/5, the open P1 on a stop during the retry window.

Reviewer model: claude-opus-5-5, cold reader xiaomi/mimo-v2.6-pro
Reasoning effort: Not exposed (the run metadata available to the reviewer does not report it)

@ovitrif ovitrif changed the title fix: retry a server change on a busy vss store fix: keep a stopped node's late writes off its rebuilt node Nov 27, 2026
@ovitrif

ovitrif commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator Author

Renamed to match its twin synonymdev/bitkit-ios#1521.

@ovitrif ovitrif removed this from the 3.2.0 milestone Nov 27, 2026
The change reads the stop counter right after its own stop, so a direct stop during the first build also blocks the retries and the recovery of the previous config.

Verified: just compile, just test, just lint (LightningRepoTest incl. the new stop and RGS busy-store cases).
@ovitrif
ovitrif requested a review from piotr-iohk November 27, 2026 10:25

@piotr-iohk piotr-iohk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QA review

Scope: changes since 6059cd2, including affected code paths and prior findings, at eebf9ca. Baseline: previous review (retaining unchanged coverage for the BUSY_STORE_START_ATTEMPTS rename/docs and changelog.d/next/2147.fixed.md).

1 actionable finding — resolve or provide an evidence-backed rebuttal.

Commit eebf9ca resolves the prior Codex, Greptile, and Talos threads (all marked resolved): restartWithElectrumServer and restartWithRgsServer now capture stopRequestCount immediately after their own stop(), pass it to startConfigChange and .onFailure recovery, and restartWithPreviousConfig skips recovery when a stop arrived since. Both unit tests suggested in the baseline review were added to LightningRepoTest. One cancellation-path gap remains where recoverOnCancellation still launches restartWithPreviousConfig() without the captured stopRequests.

Parity: compared with twin synonymdev/bitkit-ios#1521 at 3ca8946. Reading the stop counter before the first start attempt and passing it into .onFailure recovery now matches iOS (stopsBefore in LightningService.restartNode). Because iOS executes restart() in a detached Task, caller cancellation cannot trigger a separate recovery path without stopsBefore, whereas Android's recoverOnCancellation still drops stopRequests.

Validation and limits: source analysis only (no local Android SDK/Gradle test run performed in this review). At eebf9ca, CI build-and-test, lint, and detekt passed; local E2E jobs were still in progress at review time. The author reports manual cases 1 and 2 passed at 6059cd2 (QA comment); those cases exercise first-attempt success only, while busy-store retry and stop-during-build paths are covered by unit tests only.

Device testing: not performed in this review.

Suggested additional test cases

  • Android unit (LightningRepoTest): launch sut.restartWithElectrumServer(newUrl) (or restartWithRgsServer), block lightningService.setup for the new URL, trigger a direct sut.stop() and cancel the change coroutine while setup is suspended, then advance until idle. Expected: CancellationException is rethrown, lightningService.setup is never called with (null, null) for previous-config recovery, and nodeLifecycleState is not Running.

Findings

  • [MEDIUM] Pass stopRequests into recoverOnCancellation so cancelled changes respect a direct stop — inline at app/src/main/java/to/bitkit/repositories/LightningRepo.kt:1124.

Reviewer model: gemini-4-neon
Reasoning effort: high

Cancellation recovery now skips the previous config's start when a stop arrived since the change's own stop, as the failure recovery does.

Verified: just compile, just test, just lint; the new LightningRepoTest case failed before the fix.
@ovitrif
ovitrif requested review from jvsena42, piotr-iohk and pwltr and removed request for ben-kaufman November 27, 2026 11:09
@ovitrif

ovitrif commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator Author

Ben is away for a few days, so I moved this review request to @pwltr and @jvsena42.

@piotr-iohk piotr-iohk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QA review

Scope: changes since eebf9cae, including affected server-change and lifecycle paths, at 407710e6. Unchanged documentation and changelog coverage carry forward from the verified baseline.

1 actionable finding — resolve or provide an evidence-backed rebuttal.

Both server changes retry the requested configuration on a busy VSS store. The inspected tests assert retry counts, no fallback on successful retry, and a running node. Ordinary build failure without an intervening stop still schedules previous-config recovery. Caller cancellation during setup propagates, with recovery when there was no stop and suppression when a direct stop was already recorded. The direct-stop tests use setup that fails or is cancelled, leaving successful completion of a suspended retry uncovered. The RGS ordinary-failure test asserts the returned error alone; asserting that the previous configuration actually starts would strengthen it. The VSS wait and error mapping were checked in LDK Node 1dd2f42e.

The iOS twin at 3ca89464 likewise retries a busy store and checks for stops before each attempt. Its helper tests also cover the retry limit and ordinary failure. On iOS, the service restart task can continue after caller cancellation, and RGS settings are saved before restart, so the service's fallback call alone does not establish restoration of the previous RGS URL. Android saves after success and uses detached recovery on caller cancellation.

The Android CI unit-test step passed for the pinned PR head and base. The local focused run did not reach a test result. Android CI build-and-test

Device testing: not performed in this review. The runtime plan retains the author's two RGS cases and the existing Android cancellation journey. PR QA notes · Cancelled node restart journey

Earlier findings reconciled at this revision: 3 verified fixed.

Findings

  • [MEDIUM] Honor a direct stop before starting a successfully rebuilt node — inline at app/src/main/java/to/bitkit/repositories/LightningRepo.kt:1113.

Reviewer model: gpt-6.2-sol
Reasoning effort: low

@jvsena42 jvsena42 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code scan at 407710e: one LOW, posted as a reply on the resolved recovery-skip thread because it refines that fix: #2148 (comment). A debounced background stop that was cancelled still makes the change skip recovery, and the next start reuses the node built with the rejected server.

Checked and clean:

  • Store protection lives in ldk-node (rc.82 vss_store.rs fences): a new generation starts only once no node is live on the store and no write is in flight, and a fence timeout changes nothing. A retried build never cuts off an in-flight write, and a rebuilt node never coexists with one. Which writes get dropped is unchanged from acc9387.
  • LightningService.setup assigns node only after build returns, so a KvStoreSetupFailed attempt leaves nothing behind and the retry rebuilds from scratch. A stuck release throws NodeReleaseTimeout, which is not retried.
  • Settings are persisted only after a successful start, so an app kill mid-rebuild comes back on the previous server.
  • Two nodes: every build and start runs under lifecycleMutex, and a custom-config start that finds a node running gets NodeConfigNotAppliedError.
  • Lock order is always configChangeMutex → lifecycleMutex; there is no inversion with wipeStorage. Wipe during a change returns WipeInProgressError, which is not retried.
  • Back and cancel at every step (waiting for the mutex, during stop, during builds 1-5, after a direct stop) never write settings.
  • The longer spinner during a VSS outage was already accepted in the QA review.

perf: relevant (adds up to 5 builds on a server change). To measure: Connect → success time on Electrum and RGS changes against master, and the count of Retrying server change start lines.

Device gate (partial): 407710e — not run yet; the server-change timings above. Result to follow.

Reviewer model: Opus 5.5 (claude-opus-5-5) coordinating and verifying; Fable 5.2 (claude-fable-5-2) primary review
Reasoning effort: Not exposed

A server change rechecks the stop counter after setup and yields to a stop requested during the build, instead of starting the node and saving the server.

Verified: just compile, just test, just lint; the new LightningRepoTest case failed before the fix.
@ovitrif
ovitrif requested a review from piotr-iohk November 27, 2026 12:03

@piotr-iohk piotr-iohk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QA review

Scope: Follow-up on Android #2148, covering changes since 407710e and affected paths at c2633b0. Unchanged coverage was inherited from the verified baseline.

1 actionable finding — resolve or provide an evidence-backed rebuttal.

A direct stop that remains queued during successful setup suppresses native start and server persistence, skips the fallback rebuild, and releases the built handle once it acquires the lock. Both Electrum and RGS pass the original stop count through the first attempt and all busy-store retries. The new first-attempt Electrum test asserts failure, no service start, no settings write, and no previous-config setup. Its mock never installs a node handle, so it cannot verify teardown. It also lacks a positive assertion that setup ran. The existing busy-store tests cover successful retries for both servers without a stop. Earlier tests still cover a stop during a failing build, cancellation after a stop, and cancellation without a stop for both servers. The ordinary Electrum failure test observes detached fallback, while the ordinary RGS failure test only asserts the returned failure. Android regression tests

The scoped iOS comparison found matching busy-store retry and ordinary fallback behavior. The iOS twin checks stop requests before an attempt and has no equivalent check between successful setup and native start. LDK source confirmed the VSS build error and the separate native start and stop calls. Companion Appium settings cases cover ordinary server changes and errors without exercising the stop-during-setup interleaving. iOS twin at 3ca8946 · LDK rc.82 source at 1dd2f42 · Appium settings specs at ecfc4c1

Validation used source inspection and a standalone Kotlin coroutine probe of the mutex and cancellation ordering. just test file LightningRepoTest stopped before test execution because GitHub Packages returned HTTP 401. Device testing was not performed in this review. The runtime plan retains author cases 1 and 2 for RGS change and reset on the dev regtest staging build, plus the existing Back-during-rebuild journey for cancellation and successful recovery. Author QA notes · Cancellation journey

Suggested additional test cases

  • Android repository unit tests. For each server type, pause setup on the first attempt and on a retry after a busy-store failure. Queue a direct stop, then finish setup with a non-null node handle. Assert the targeted attempt ran, the service never starts the candidate, the stop clears its handle, the saved URL stays unchanged, and fallback does not build.
  • Android repository unit test. While setup holds the lifecycle mutex, let the debounced stop fire, then request foreground start so it cancels the waiting stop. Complete setup and verify that any node the foreground start brings up uses the persisted server.

Earlier findings reconciled at this revision: 1 still present, 3 verified fixed.

Findings

  • [MEDIUM] Release the abandoned node before foreground start — inline at app/src/main/java/to/bitkit/repositories/LightningRepo.kt:464.

Existing concerns

  • [LOW] Release the rejected node when native start fails after a canceled background stop — The parent thread is marked resolved. This reply describes a stop that queues after native start has already begun. The new check runs before native start, so it cannot intercept that ordering. If native start then fails after foreground cancels the queued stop, the failure leaves the rejected handle installed, recovery skips, and foreground can reuse the handle while the saved URL remains unchanged. Releasing the custom-config handle under the lifecycle lock on a non-running start failure would cover this ordering. Reference the original reply for this existing concern. The author of the reply reported a failing regression at 407710e. The current disposition is based on source inspection.

Reviewer model: gpt-6.2-sol
Reasoning effort: low

A node built with a changed server is released when its start yields to a stop, fails or is cancelled, so a foreground start that cancelled a background stop rebuilds from the saved server instead of reusing it.

Verified: just compile, just test, just lint; the new cancelled-background-stop and cancel-after-setup LightningRepoTest cases failed before the fix.

@piotr-iohk piotr-iohk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QA review

Scope: Follow-up on Android PR #2148, covering changes since c2633b059 and affected lifecycle paths at abd015b07. Unchanged retry documentation and changelog coverage were inherited from the baseline.

2 actionable findings — resolve or provide an evidence-backed rebuttal.

The post-setup stop check precedes native start and settings writes. When a foreground return cancels the queued stop, the change itself releases the replacement under the lifecycle lock. Native start failure and cancellation inside startup also take the cleanup path. The regression tests cover the direct-stop outcome, Electrum setup-time foreground return, both servers' start failures, and Electrum cancellation inside the start mock. The foreground cases assert a null override argument on the last build, which is the production signal to read saved settings. They do not assert the effective saved URL or successful foreground startup. The direct-stop test's final handle assertion can also be satisfied by the queued stop, and the cancellation test suspends before the mock reports a running node. Add unit coverage for the retry ownership race and cancellation after native startup while repository initialization is still in flight.

The existing settings-write failure path still leaves a successfully started replacement active if the write fails. Cancellation during the native build handoff can also occur before LightningService.setup stores the returned handle, so the repository cleanup only reaches handles already installed in the service. These paths predate the follow-up cleanup.

Compared busy-store retry, stop ordering, cancellation and saved-setting behavior in the pinned iOS twin at 3ca894644. iOS checks the stop counter at attempt entry and runs service restarts in a separate task. Electrum saves after verification, while RGS saves before restart. The comparison was limited to those contracts. Native start, stop, and VSS build behavior were inspected in LDK Node v0.7.0-rc.82.

Validation: git diff --check passed. The focused LightningRepoTest Gradle task failed before running tests because GitHub Packages returned HTTP 401 for native dependencies. Device testing was not performed in this review. The author reports that manual cases 1 and 2 passed on 6059cd20b. Repeat both RGS Connect and Reset To Default on the pinned Android build.

Suggested additional test cases

  • Android, with a distinct saved server and a usable Lightning channel: start a server change, background long enough to queue the stop during the build, then foreground before it acquires the lifecycle lock. Verify that the saved server remains selected, Lightning returns to Running on that server, and a small incoming Lightning payment succeeds. Use controlled fault injection in unit tests for the busy-store and native-failure timings.
  • Run the Android cancellation journey with Back during RGS startup. Verify cleanup and saved-server recovery directly. The journey's old reconcile messages no longer identify this cancellation phase. Cancelled node restart journey

Earlier findings reconciled at this revision: 4 verified fixed.

Findings

  • [MEDIUM] Verify node ownership before retrying a server change — inline at app/src/main/java/to/bitkit/repositories/LightningRepo.kt:1138.
  • [MEDIUM] Publish Stopping before releasing a running replacement — inline at app/src/main/java/to/bitkit/repositories/LightningRepo.kt:514.

Reviewer model: gpt-6.2-sol
Reasoning effort: low

pwltr
pwltr previously approved these changes Nov 27, 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 abd015b.

Low candidate: The busy-store retry can reuse a non-running handle built by a foreground start. If a foreground start gets the lifecycle lock between attempts, builds with saved settings, and leaves its handle installed after a transient startup failure, the custom retry skips setup at app/src/main/java/to/bitkit/repositories/LightningRepo.kt:445. If native startup then succeeds on that same handle, Electrum or RGS can report success and save the new URL at app/src/main/java/to/bitkit/repositories/LightningRepo.kt:1067 or app/src/main/java/to/bitkit/repositories/LightningRepo.kt:1107 while the live node keeps using the saved configuration it was built with. Check that a successful configuration change starts a handle built with the requested endpoint. The false-success outcome depends on native same-handle recovery.

Low: The RGS cancellation journey at journeys/node-lifecycle/cancelled-node-restart.xml:45 classifies a missing reconcile log as stop-phase cancellation. The custom startup branch at app/src/main/java/to/bitkit/repositories/LightningRepo.kt:510 bypasses all three reconcile messages, so Back can cancel startup as intended and still be recorded as a stop-phase run. Use a marker from the custom cancellation branch to verify which phase was exercised.

Low: Cancelling after native startup can leave Running published while teardown clears the handle. app/src/main/java/to/bitkit/repositories/LightningRepo.kt:514 releases the node before changing the lifecycle state, so a concurrent node-routed invoice request can pass the Running gate and fail with NodeNotSetup. Publish Stopping before teardown, as the normal stop path does.

Review details

Quick review: Claude Opus 5.5, high reasoning effort
General review: GPT-6.2 Sol, high reasoning effort
Verification: GPT-6.2 Sol, high reasoning effort

A config-change attempt releases any handle another start left before it builds, and fails if it cannot. A cancelled change stops through the repository's stop transition, so Stopping is published and the state reset before its node is released. The cancellation journey now expects the release line.

Verified: just compile, just test, just lint; the two new LightningRepoTest cases failed before the fix.
@ovitrif

ovitrif commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator Author

The busy-store retry can reuse a non-running handle built by a foreground start.

The RGS cancellation journey at journeys/node-lifecycle/cancelled-node-restart.xml:45 classifies a missing reconcile log as stop-phase cancellation.

Cancelling after native startup can leave Running published while teardown clears the handle.

@pwltr Addressed all three in 0907e4c: a config-change attempt releases any leftover handle before it builds, the cancelled change logs Released the node of a cancelled config change and the journey asserts that line, and the cancellation stops through stopLocked(), so Stopping is published before the node is released.

@ovitrif
ovitrif requested review from piotr-iohk and pwltr November 27, 2026 14:05

@jvsena42 jvsena42 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review at 0907e4c: one LOW inline.

Fixed since my last review:

  • My earlier LOW, and piotr's "honor a direct stop" and "release the abandoned node" MEDIUMs: the post-setup guard releases the node, and a failed or cancelled custom-config start releases its node. Covered by restartWithElectrumServer keeps a stop requested during a build that succeeds, … releases its node when a cancelled background stop arrived during setup, … cancelled after setup releases its node, and the Electrum and RGS "leaves no node on the rejected server after a cancelled background stop" tests.
  • piotr's ":514 publish Stopping" thread: a cancelled config change now goes through stopLocked(), which publishes Stopping and resets the prepared send and LightningState.
  • piotr's ":1138 node ownership" thread: a config change now releases any handle another start left behind before it builds, and fails with NodeConfigNotAppliedError if the release does not take. A running node is still caught earlier by skipStartForRunningNode.

Checked and clean:

  • LightningService.stop() on a built-but-never-started node swallows NotRunning, sets node = null and joins destroy for at most 1 s. It writes no settings or files, does not bump the VSS generation and does not touch LightningNodeService.
  • Recovery still runs for a failed or cancelled change with no stop counted.
  • Lock order is unchanged.
  • ErrorStarting consumers only read the state.

perf: the delta adds no waits on the server-change path.

Device gate (partial): 0907e4c — not run yet; Electrum and RGS server-change timings against master. Result to follow.

Reviewer model: Opus 5.5 (claude-opus-5-5) coordinating and verifying; Fable 5.2 (claude-fable-5-2) delta review
Reasoning effort: Not exposed

@piotr-iohk piotr-iohk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QA review

Scope: changes since abd015b, including affected code paths, at 0907e4c. Baseline: previous QA review.

No new actionable code findings.

A server-change attempt now releases any node handle another start left behind before it builds, and a cancelled change stops through the repository stop transition so Stopping is published before that node is released. A requested stop still wins, the unsaved server is still not persisted, and recovery of the saved server is unchanged. Inherited baseline coverage remains the busy-store retry and fallback, direct and debounced stops, foreground starts, server persistence, the earlier stop and recovery tests, the retry-constant rename and its documentation, and changelog.d/next/2147.fixed.md.

Compared the leftover-handle contract with twin bitkit-ios#1521 at 3ca89464 and companion bitkit-ios#1529 at 4a90137f. At both revisions, iOS setup returns without building when a node is already configured. Only that contract was checked.

Device testing: not performed in this review. The new LightningRepoTest cases were inspected and not executed here.

Earlier findings reconciled at this revision: 1 still present, 3 verified fixed.

Existing concerns

  • [MEDIUM] Keep a cancelled background stop from dropping a foreground graph restart — The unresolved thread is still present in the current source. If a foreground start finds a stale network graph and begins the restart's second build, then the user backgrounds the app for at least five seconds and returns while that build still holds the lifecycle mutex, the debounced stop increments the counter and then gets cancelled. The post-setup guard keys only on that counter, so it also fires for this restart, which has no custom server. It releases the node and returns NodeStartYieldedToStopError. The foreground ON_START already returned because the original start is still marked in progress, and reportStartupFailure drops that error without showing it or starting again. walletViewModel.start() is called only from that ON_START path, so Lightning stays stopped until the next foreground transition. The same guard also covers restartWithPreviousConfig. This is source analysis; the path was not executed. A focused issue search found no matching issue. The consequence is a foreground outage with a later foreground cycle as the recovery, so the severity is MEDIUM.

Reviewer model: grok-4.7
Reasoning effort: xhigh

The post-setup stop check now applies only to server changes and their recovery, so a stale-graph rebuild whose background stop was cancelled still starts, while recovery still yields to a stop.

Verified: just compile, just test, just lint; the two new LightningRepoTest cases failed before the fix.

@jvsena42 jvsena42 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving at a45ca2a. a45ca2a fixes my last LOW: the post-setup stop check now applies only to server changes and recovery, and an ordinary start and its stale-graph rebuild no longer yield to a stop they cancelled. No review threads are open.

Device gate: a45ca2a — journeys: cancelled-node-restart.xml passed up to the payment steps; perf: rebuild median, head vs master: Electrum→alt 7.34 s (master 7.87 s), Electrum→default 8.27 s (9.17 s), RGS→alt 7.61 s (8.44 s), RGS→default 7.85 s (7.90 s); 0 Retrying server change start lines in 24 changes; no ANR; l10n: no UI text changed

  • Rebuild is Node stopped → Node started in the app log, 3 runs per case per build on the same emulator and wallet, with the host's 1-minute load between 3 and 15 on every run. The Electrum alternative was a plain-TCP proxy to the staging Electrum, and the RGS alternative was the default URL with a trailing /.
  • Changing → success, head vs master: E1 11.6–13.2 s (13.1–14.1 s), E2 12.9–13.4 s (13.9–15.6 s), R1 11.8–13.8 s (12.7–13.9 s), R2 11.8–14.0 s (12.2–13.2 s).
  • J1: LDK debug restart passes. Back during an RGS change logs Released the node of a cancelled config change, then recovery and Successfully started node with previous config, and the screen still shows the default URL. Relaunch logs LDK node start skipped, lifecycle state: Running. Receive shows the QR.
  • ⚠️ Not verified: the journey's payment steps need a channel, and Blocktank staging POST /channels still returns 404. The journey's relaunch step expects Collected node support summary, which is only logged when the node actually starts, so that step is stale. Separate from this PR.
  • ⚠️ Not verified: the busy-store retry itself. It cannot be provoked on demand, as the PR body notes; the unit tests cover it.

Skipped — covered by CI at a45ca2a: build-and-test, lint, compose-ui, e2e (all green).

Reviewer model: Opus 5.5 (claude-opus-5-5) coordinating, verifying and runbook; Fable 5.2 (claude-fable-5-2) primary and delta review; Sonnet 5.5 (claude-sonnet-5-5) device driver
Reasoning effort: Not exposed

@ovitrif

ovitrif commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator Author

Merged: approved by @jvsena42 at a45ca2a, checks green, every thread resolved.

@ovitrif
ovitrif merged commit 8bd9b98 into master Nov 27, 2026
23 checks passed
@ovitrif
ovitrif deleted the fix/retry-server-change-busy-store branch November 27, 2026 15:44
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 valid server change fails while the old node's writes are in flight

5 participants