Repository navigation
lnd, electrs, clightning: start again when bitcoind restarts - #62
Conversation
2909289 to
5c665c2
Compare
|
Force-pushed after review on my side: the regtest VM run showed electrs exits with |
5c665c2 to
8c4c098
Compare
|
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 |
8c4c098 to
a048441
Compare
|
Final revision passed |
a048441 to
3e54c33
Compare
|
One more test adjustment after the CI run: on the unsynced mainnet |
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
|
Astra found this issue: An electrs restart can undo an intentional stop of bitcoind The change to
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 |
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.
3e54c33 to
c4336ea
Compare
ea39adf to
bafed96
Compare
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)
bafed96 to
b1e8046
Compare
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)
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 |
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)
Refs #16 (section 3)
Problem
modules/lnd.nix,modules/electrs.nixandmodules/clightning.nixdeclarerequires = [ "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 startsmulti-user.target, so the exact mechanism isn't pinned down, but the fix below doesn't depend on it.Change
Keep
requiresand add"bitcoind.service"towantedByin all three modules.systemctl stop bitcoindstill stops them, and because a stop job never triggersRestart=, nothing pulls bitcoind back up. The documented bitcoind data directory migration, which stops bitcoind and copies the data dir, keeps working.systemctl restart bitcoindpropagates a restart to them, as before, andafteris unchanged.The first revision of this PR switched to
wantsinstead, 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-failurerestarts it, and that start job pulls bitcoind in. That revision is gone.Testing
The
restart-bitcoindtest 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 thebackupsandstop-electrstests leave them down, and then:systemctl stop bitcoind. Every dependent must reachinactiveorfailed(clightning with clboss has a 500 ms stop timeout in the test scenarios), and theirInactiveEnterTimestampMonotonicis recorded. After 25 s, longer than everyRestartSec, bitcoind and the dependents must still be down with the same timestamps. This is the case the first revision got wrong.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.systemctl restart bitcoind. Everything must be up afterwards.run-tests.sh -s default buildrun-tests.sh -s regtest buildrun-tests.sh -s netnsRegtest buildModels 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
failedinstead ofinactiveafter 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.