Skip to content

lnd, electrs, clightning: start again when bitcoind restarts - #62

Merged
prusnak merged 1 commit into
bitcoinix:masterfrom
ostermayer:lnd-electrs-wants
Oct 23, 2026
Merged

prusnak merged 1 commit into
bitcoinix:masterfrom
ostermayer:lnd-electrs-wants

Conversation

@ostermayer

@ostermayer ostermayer commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

Refs #16 (section 3)

Problem

modules/lnd.nix, modules/electrs.nix and modules/clightning.nix declare requires = [ "bitcoind.service" ], so systemd propagates a stop of bitcoind to them. A deploy that changes the bitcoind unit stops bitcoind and starts it again, and on production nodes lnd, electrs and lightningd stayed down after such a switch. For lnd that's a funds risk, since an offline node can't react to a stale-state force close. The activation script also starts multi-user.target, so the exact mechanism isn't pinned down, but the fix below doesn't depend on it.

Change

Keep requires and add "bitcoind.service" to wantedBy in all three modules.

  • Starting bitcoind now pulls the three units in through bitcoind's own start job, ordered after it.
  • An explicit systemctl stop bitcoind still stops them, and because a stop job never triggers Restart=, nothing pulls bitcoind back up. The documented bitcoind data directory migration, which stops bitcoind and copies the data dir, keeps working.
  • systemctl restart bitcoind propagates a restart to them, as before, and after is unchanged.

The first revision of this PR switched to wants instead, but @prusnak's review found that this lets a restarting dependent start bitcoind again after an operator stopped it on purpose: electrs exits without its backend, Restart=on-failure restarts it, and that start job pulls bitcoind in. That revision is gone.

Testing

The restart-bitcoind test is rewritten and also runs in the regtest scenarios, where electrs is live and the Lightning daemons are synced. It first starts bitcoind and the dependents explicitly, because the backups and stop-electrs tests leave them down, and then:

  1. systemctl stop bitcoind. Every dependent must reach inactive or failed (clightning with clboss has a 500 ms stop timeout in the test scenarios), and their InactiveEnterTimestampMonotonic is recorded. After 25 s, longer than every RestartSec, bitcoind and the dependents must still be down with the same timestamps. This is the case the first revision got wrong.
  2. systemctl start bitcoind. Every dependent must come back and still be up 15 s later. On master's modules this step fails, because nothing starts them.
  3. systemctl restart bitcoind. Everything must be up afterwards.
Run (KVM host, fresh clone) Result
run-tests.sh -s default build PASS, restart-bitcoind in 49 s
run-tests.sh -s regtest build PASS
run-tests.sh -s netnsRegtest build PASS

Models used on this PR: Fable 5.1 for the design and the review, and Opus 5.5 for the change. Kimi K3 and GLM 5.3, both at max thinking, reviewed the first revision. GPT-6 Astra at xhigh reasoning reviewed this revision twice: it found that a dependent can end in failed instead of inactive after the stop, which the test now accepts, and that the commit's claim about the deploy mechanism overstated what's known, which the wording now reflects. DeepSeek V4.1 Flash 1017 and GLM 5.3, both at max thinking, reviewed the tree of this revision before the wording change and found nothing in the three modules or the test. Their findings in other files are master code that open issues already track.

@ostermayer
ostermayer force-pushed the lnd-electrs-wants branch 3 times, most recently from 2909289 to 5c665c2 Compare October 16, 2026 23:25
@ostermayer

ostermayer commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator Author

Force-pushed after review on my side: the regtest VM run showed electrs exits with sync failed when bitcoind goes away and relies on Restart=on-failure to come back, so the electrs commit message and module comment now say that instead of claiming it reconnects on its own. The test runs in the regtest scenarios as well now (electrs is live there) and checks lnd's InvocationID instead of the earlier 45 s health-check window, which couldn't prove anything. Description updated.

@ostermayer ostermayer changed the title lnd, electrs: depend on bitcoind with wants, not requires lnd, electrs, clightning: depend on bitcoind with wants, not requires Oct 16, 2026
@ostermayer

Copy link
Copy Markdown
Collaborator Author

Added clightning to this PR: once the restart test ran in the regtest scenarios, the bitcoind stop/start left lightningd down (it was the last module with requires on bitcoind) and the regtest test failed on it. lightningd's bcli plugin retries bitcoind, so wants is the right dependency there too. Test covers it now; VM runs of this revision in progress.

@ostermayer

Copy link
Copy Markdown
Collaborator Author

Final revision passed default, regtest and netnsRegtest on a KVM box; results are in the description. On the unsynced mainnet scenario lightningd aborts in getchaininfo when bitcoind goes away and comes back through Restart=on-failure; on regtest (synced) it keeps running with the same InvocationID. The commit message and module comment describe both cases.

@ostermayer

ostermayer commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator Author

One more test adjustment after the CI run: on the unsynced mainnet default scenario the GitHub runner saw lnd itself exit during the outage and come back via Restart=on-failure (a faster KVM box didn't reproduce it). The "not restarted" assertion now applies only in the regtest scenarios, where the daemons are synced and do keep running; elsewhere the test asserts they are back up. All three scenarios pass locally; CI rerunning.

ostermayer added a commit to ostermayer/bitcoinix that referenced this pull request Oct 17, 2026
With `requires = [ "lnd.service" ]`, every lnd stop/start (e.g. a deploy
that touches lnd.conf) stopped loopd and poold, and nothing started them
again. Use `wants` so systemd leaves them alone; `after` keeps the boot
order.

Both daemons exit when lnd goes away. loopd exits with status 1, poold
shuts down cleanly with status 0. `Restart = "on-failure"` restarted only
loopd, so poold stayed down. Use `Restart = "always"` for both.

The new `lnd-restart-dependents` test (regtest scenarios) stops and
starts lnd and checks that loop and pool are active again, right away and
15 s later.

Follow-up to bitcoinix#62.
ostermayer added a commit to ostermayer/bitcoinix that referenced this pull request Oct 17, 2026
When bitcoind stays unreachable, lnd's chain backend health check (3
attempts, 1 min apart by default) requests a graceful shutdown, and lnd
exits with status 0. `Restart = "on-failure"` never restarted it, so lnd
stayed down after bitcoind came back. A Lightning node that is down cannot
react to a stale-state force close. `Restart = "always"` brings lnd back
after `RestartSec`. Trade-off: `lncli stop` now also results in a restart;
an operator who wants lnd down uses `systemctl stop lnd`, which never
triggers `Restart=`.

Test: `lnd-restart-on-clean-exit` (regtest scenarios with lnd) runs
`lncli stop`, waits for a new lnd InvocationID, waits for the unit to be
active and for `lncli getinfo` to succeed.

Follow-up to bitcoinix#62.
ostermayer added a commit to ostermayer/bitcoinix that referenced this pull request Oct 17, 2026
With `requires = [ "lnd.service" ]`, every lnd stop/start (e.g. a deploy
that touches lnd.conf) stopped loopd and poold, and nothing started them
again. Use `wants` so systemd leaves them alone; `after` keeps the boot
order.

Both daemons exit when lnd goes away. loopd exits with status 1, poold
shuts down cleanly with status 0. `Restart = "on-failure"` restarted only
loopd, so poold stayed down. Use `Restart = "always"` for both.

The new `lnd-restart-dependents` test (regtest scenarios) stops and
starts lnd and checks that loop and pool are active again, right away and
15 s later.

Follow-up to bitcoinix#62.
ostermayer added a commit to ostermayer/bitcoinix that referenced this pull request Oct 17, 2026
With `requires = [ "lnd.service" ]`, every lnd stop/start (e.g. a deploy
that touches lnd.conf) stopped loopd and poold, and nothing started them
again. Use `wants` so systemd leaves them alone; `after` keeps the boot
order.

Both daemons exit when lnd goes away. loopd exits with status 1, poold
shuts down cleanly with status 0. `Restart = "on-failure"` restarted only
loopd, so poold stayed down. Use `Restart = "always"` for both.

The new `lnd-restart-dependents` test (regtest scenarios) stops and
starts lnd and checks that loop and pool are active again, right away and
15 s later.

Follow-up to bitcoinix#62.
ostermayer added a commit to ostermayer/bitcoinix that referenced this pull request Oct 17, 2026
With `requires = [ "lnd.service" ]`, every lnd stop/start (e.g. a deploy
that touches lnd.conf) stopped loopd and poold, and nothing started them
again. Use `wants` so systemd leaves them alone; `after` keeps the boot
order.

Both daemons exit when lnd goes away. loopd exits with status 1, poold
shuts down cleanly with status 0. `Restart = "on-failure"` restarted only
loopd, so poold stayed down. Use `Restart = "always"` for both.

The new `lnd-restart-dependents` test (regtest scenarios) stops and
starts lnd and checks that loop and pool are active again, right away and
15 s later.

Follow-up to bitcoinix#62.
ostermayer added a commit to ostermayer/bitcoinix that referenced this pull request Oct 17, 2026
With `requires = [ "lnd.service" ]`, every lnd stop/start (e.g. a deploy
that touches lnd.conf) stopped loopd and poold, and nothing started them
again. Use `wants` so systemd leaves them alone; `after` keeps the boot
order.

Both daemons exit when lnd goes away. loopd exits with status 1, poold
shuts down cleanly with status 0. `Restart = "on-failure"` restarted only
loopd, so poold stayed down. Use `Restart = "always"` for both.

The new `lnd-restart-dependents` test (regtest scenarios) stops and
starts lnd and checks that loop and pool are active again, right away and
15 s later.

Follow-up to bitcoinix#62.
ostermayer added a commit to ostermayer/bitcoinix that referenced this pull request Oct 17, 2026
With `requires = [ "lnd.service" ]`, every lnd stop/start (e.g. a deploy
that touches lnd.conf) stopped loopd and poold, and nothing started them
again. Use `wants` so systemd leaves them alone; `after` keeps the boot
order.

Both daemons exit when lnd goes away. loopd exits with status 1, poold
shuts down cleanly with status 0. `Restart = "on-failure"` restarted only
loopd, so poold stayed down. Use `Restart = "always"` for both.

The new `lnd-restart-dependents` test (regtest scenarios) stops and
starts lnd and checks that loop and pool are active again, right away and
15 s later.

Follow-up to bitcoinix#62.
ostermayer added a commit to ostermayer/bitcoinix that referenced this pull request Oct 17, 2026
With `requires = [ "lnd.service" ]`, every lnd stop/start (e.g. a deploy
that touches lnd.conf) stopped loopd and poold, and nothing started them
again. Use `wants` so systemd leaves them alone; `after` keeps the boot
order.

Both daemons exit when lnd goes away. loopd exits with status 1, poold
shuts down cleanly with status 0. `Restart = "on-failure"` restarted only
loopd, so poold stayed down. Use `Restart = "always"` for both.

The new `lnd-restart-dependents` test (regtest scenarios) stops and
starts lnd and checks that loop and pool are active again, right away and
15 s later.

Follow-up to bitcoinix#62.
@ostermayer ostermayer changed the title lnd, electrs, clightning: depend on bitcoind with wants, not requires lnd, electrs, clightning: use wants instead of requires for bitcoind Oct 17, 2026
ostermayer added a commit to ostermayer/bitcoinix that referenced this pull request Oct 18, 2026
With `requires = [ "lnd.service" ]`, every lnd stop/start (e.g. a deploy
that touches lnd.conf) stopped loopd and poold, and nothing started them
again. Use `wants` so systemd leaves them alone; `after` keeps the boot
order.

Both daemons exit when lnd goes away. loopd exits with status 1, poold
shuts down cleanly with status 0. `Restart = "on-failure"` restarted only
loopd, so poold stayed down. Use `Restart = "always"` for both.

The new `lnd-restart-dependents` test (regtest scenarios) stops and
starts lnd and checks that loop and pool are active again, right away and
15 s later.

Follow-up to bitcoinix#62.
ostermayer added a commit to ostermayer/bitcoinix that referenced this pull request Oct 18, 2026
With only `requires = [ "lnd.service" ]`, a NixOS deploy that touches
lnd.conf stopped loopd and poold for good. The deploy stops lnd and
starts the new unit instead of restarting it. `requires` passes the stop
on to loopd and poold, but nothing passes the start on.

Plain `wants` with `Restart = "always"` is wrong too. When lnd is stopped
on purpose, loopd and poold exit, and their automatic restart pulls lnd
back up after 10 s. Through lnd's `requires`, it pulls a stopped bitcoind
back up as well. That breaks the stop-before-copy workflow in
docs/configuration.md.

So keep `requires` and add `lnd.service` to `wantedBy`:

- `requires`: an explicit stop or restart of lnd (or a stop of bitcoind)
  stops or restarts loopd and poold. They no longer start a stopped lnd.
- `wantedBy` lnd: every start of lnd, including the one in a deploy,
  starts loopd and poold again.
- `Restart = "always"`: when lnd exits on its own there's no stop job, so
  the daemons must come back. loopd exits with status 1 then, and poold
  exits cleanly with status 0, which `on-failure` didn't restart.

The new `lnd-restart-dependents` test (regtest scenarios) stops and
starts lnd and checks that loop and pool come back and stay up. It
restarts lnd and checks that both restart with it. It kills lnd and
checks that both come back. It then stops lnd, and later bitcoind, and
checks that nothing starts them again within 25 s. charge-lnd's timer
requires lnd too, so the test pauses it meanwhile.

Follow-up to bitcoinix#62.
@prusnak

prusnak commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Astra found this issue:

An electrs restart can undo an intentional stop of bitcoind

The change to wants = [ "bitcoind.service" ]; in modules/electrs.nix introduces a maintenance concern:

  1. An operator runs systemctl stop bitcoind while electrs is running.
  2. electrs exits when its backend disappears, as described in this PR.
  3. Its existing Restart=on-failure schedules a restart after RestartSec=10s.
  4. Starting electrs pulls in its wanted units, starting bitcoind again without an operator requesting it.

systemd's automatic restart handling schedules a start transaction, which also starts wanted dependencies. This finding is based on the unit configuration and systemd source; I have not reproduced it in a VM.

This matters for the documented bitcoind migration procedure, which stops bitcoind and then copies its data directory. With electrs enabled, bitcoind could start writing to that directory during the copy.

The new restart test explicitly starts bitcoind immediately after stopping it. Its 15-second wait happens after that start, so it does not exercise this case.

Before merging, please add a VM test that leaves bitcoind stopped long enough for electrs to fail and attempt its automatic restart. Either preserve intentional-stop behavior or document and test a safe maintenance procedure that keeps bitcoind stopped throughout the copy. If the changed behavior is retained, it also needs an affected-user entry in modules/versioning.nix under the repository's breaking-change policy.

@prusnak prusnak added the status: changes needed Cannot proceed until suggested changes are implemented label Oct 21, 2026
ostermayer added a commit to ostermayer/bitcoinix that referenced this pull request Oct 22, 2026
When bitcoind stays unreachable, lnd's chain backend health check (3
attempts, 1 min apart by default) requests a graceful shutdown, and lnd
exits with status 0. `Restart = "on-failure"` never restarted it, so lnd
stayed down after bitcoind came back. A Lightning node that is down cannot
react to a stale-state force close. `Restart = "always"` brings lnd back
after `RestartSec`. Trade-off: `lncli stop` now also results in a restart;
an operator who wants lnd down uses `systemctl stop lnd`, which never
triggers `Restart=`.

Test: `lnd-restart-on-clean-exit` (regtest scenarios with lnd) runs
`lncli stop`, waits for a new lnd InvocationID, waits for the unit to be
active and for `lncli getinfo` to succeed.

Follow-up to bitcoinix#62.
@github-actions github-actions Bot added area: ci .github/, test/, dev/ area: modules modules/ and pkgs/lib.nix labels Oct 22, 2026
@ostermayer
ostermayer force-pushed the lnd-electrs-wants branch 2 times, most recently from ea39adf to bafed96 Compare October 23, 2026 01:01
@prusnak prusnak removed the status: changes needed Cannot proceed until suggested changes are implemented label Oct 23, 2026
@prusnak prusnak changed the title lnd, electrs, clightning: use wants instead of requires for bitcoind lnd, electrs, clightning: start again when bitcoind restarts Oct 23, 2026
The three units require bitcoind. A deploy that changes the bitcoind unit
stops bitcoind and starts it again. systemd propagates the stop to the
dependents. On production nodes they stayed down after such a switch,
although the activation script also starts multi-user.target, so the
exact mechanism isn't pinned down. For lnd that is a funds risk: an
offline node can't react to a stale-state force close.

Add bitcoind.service to their wantedBy. Starting bitcoind now pulls them
in through bitcoind's own start job, ordered after it, which doesn't
depend on the target restart. requires stays, so an explicit
`systemctl stop bitcoind` still stops them and nothing restarts them
while bitcoind is down on purpose, which the bitcoind data directory
migration relies on. A plain `wants` instead of `requires` would let a
dependent's Restart= pull bitcoind back up after such a stop.

The restart-bitcoind test first starts bitcoind and the dependents
explicitly, because the backups and stop-electrs tests leave them down.
It then stops bitcoind, checks that the dependents stop with it and stay
down for 25 s, starts bitcoind, and checks that they come back and stay
up. It also runs in the regtest scenarios, where electrs is live and the
Lightning daemons are synced.

Refs bitcoinix#16 (section 3)
@prusnak
prusnak merged commit 140d50e into bitcoinix:master Oct 23, 2026
10 checks passed
prusnak added a commit to ostermayer/bitcoinix that referenced this pull request Oct 23, 2026
When bitcoind stays unreachable, lnd's chain backend health check (3
attempts, 1 min apart by default) requests a graceful shutdown, and lnd
exits with status 0. `Restart = "on-failure"` never restarted it, so lnd
stayed down after bitcoind came back. A Lightning node that is down cannot
react to a stale-state force close. `Restart = "always"` brings lnd back
after `RestartSec`. Trade-off: `lncli stop` now also results in a restart;
an operator who wants lnd down uses `systemctl stop lnd`, which never
triggers `Restart=`.

Test: `lnd-restart-on-clean-exit` (regtest scenarios with lnd) runs
`lncli stop`, waits for a new lnd InvocationID, waits for the unit to be
active and for `lncli getinfo` to succeed.

Follow-up to bitcoinix#62.

Assisted-by: Codex (GPT-6)
prusnak added a commit that referenced this pull request Oct 23, 2026
Follow-up to #62.

## Problem

lnd checks its chain backend at regular intervals, and when bitcoind
stays unreachable the check fails 3 times, 1 minute apart by default,
after which lnd shuts down cleanly and exits with status 0. The lnd unit
has `Restart = "on-failure"`, and systemd doesn't restart a unit that
exits with status 0, so lnd stays down after bitcoind comes back. A
Lightning node that's down can't react to a stale-state force close,
which puts funds at risk.

## Change

`modules/lnd.nix`: `Restart = "always"`, with the reason in the comment.

`systemctl stop lnd` still stops lnd for good, because an explicit stop
never triggers `Restart=`. The trade-off is that `lncli stop` now
results in a restart after 10 s, so an operator who wants lnd down uses
`systemctl stop lnd`.

## Review

Models used on this PR: Fable 5.1 and Opus 5.5 for the changes. Kimi K3
and GLM 5.3, both at max thinking, reviewed the first revisions. GPT-6
Astra at xhigh reasoning found that the first test version could pass on
`master`, because a restarting loopd pulled lnd back up through
`Requires=`. DeepSeek V4.1 Flash 1017 and GLM 5.3 at max thinking, plus
Astra, reviewed the rebased revision and found nothing in
`modules/lnd.nix` or the test. Their findings in other files are master
code that open issues already track.

## Testing

New test `lnd-restart-on-clean-exit` in the regtest scenarios with lnd.
It pauses the charge-lnd timer first, because charge-lnd `requires` lnd
and a timer tick during the test would start lnd by itself and hide a
missing `Restart=`, while RTL only `wants` lnd and keeps running. Then
it runs `lncli stop`, waits for a new lnd `InvocationID`, waits for the
unit to be active, runs `lncli getinfo`, and starts the timer again.

| Run (KVM host) | Result |
|---|---|
| `regtest`, this branch | PASS. The journal shows `lnd.service:
Deactivated successfully` after `lncli stop`, so the exit status is 0,
and then `Scheduled restart job` |
| `master` (red) | not run as such, but with `on-failure` the clean exit
leaves the unit inactive and the `InvocationID` wait times out |
@seberm seberm added the type: bug Behaviour is wrong vs. documented or intended label Oct 23, 2026
msgilligan pushed a commit to msgilligan/bitcoinix that referenced this pull request Nov 8, 2026
When bitcoind stays unreachable, lnd's chain backend health check (3
attempts, 1 min apart by default) requests a graceful shutdown, and lnd
exits with status 0. `Restart = "on-failure"` never restarted it, so lnd
stayed down after bitcoind came back. A Lightning node that is down cannot
react to a stale-state force close. `Restart = "always"` brings lnd back
after `RestartSec`. Trade-off: `lncli stop` now also results in a restart;
an operator who wants lnd down uses `systemctl stop lnd`, which never
triggers `Restart=`.

Test: `lnd-restart-on-clean-exit` (regtest scenarios with lnd) runs
`lncli stop`, waits for a new lnd InvocationID, waits for the unit to be
active and for `lncli getinfo` to succeed.

Follow-up to bitcoinix#62.

Assisted-by: Codex (GPT-6)
@ostermayer
ostermayer deleted the lnd-electrs-wants branch November 8, 2026 22:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: ci .github/, test/, dev/ area: modules modules/ and pkgs/lib.nix type: bug Behaviour is wrong vs. documented or intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants