From 3919de6358c2c21627737be1050cc03b85d77526 Mon Sep 17 00:00:00 2001 From: maia Date: Sat, 28 Nov 2026 05:57:08 +0100 Subject: [PATCH] fix(matrix): queue tuwunel's start after the guest's boot is over The deferred starter was a oneshot that ran `systemctl start --no-block tuwunel` inside the boot transaction. A --no-block start still queues a start job, so the guest's job queue stayed non-empty for as long as tuwunel's start took, and the container (Type=notify, --notify-ready=yes, one minute) never reported ready and was killed at its limit. The starter is now Type=simple, so its own start job ends at the fork. It waits for `systemctl is-system-running --wait` and only then queues the service. The VM test waits for the daemon to reach `activating` instead of asserting it the moment the container's start ends. --- docs/2-handbook/swarm/matrix.md | 6 +++-- nix/container-modules/swarm-container.nix | 32 ++++++++++++++--------- nix/module-eval/swarm-services-switch.nix | 17 +++++++----- nix/vm-tests/container-deferred-start.nix | 8 ++++-- 4 files changed, 40 insertions(+), 23 deletions(-) diff --git a/docs/2-handbook/swarm/matrix.md b/docs/2-handbook/swarm/matrix.md index c7e245f9f..a3984ef19 100644 --- a/docs/2-handbook/swarm/matrix.md +++ b/docs/2-handbook/swarm/matrix.md @@ -39,8 +39,10 @@ Same shape as [`gateway.md::hive-forge container shape`](../../4-implementation/ the journal (`tuwunel-fallocate-probe:`) and leaves the default. - tuwunel starts after the container is up, not during its boot. The container's start job (`container@hive-matrix.service`, one minute) ends when - the guest's boot does, and a oneshot in the guest starts tuwunel without - waiting for it. tuwunel's own start, which replays the database's + the guest's boot does, and a unit in the guest queues tuwunel's start once + the boot is over, without waiting for it. (Queued during the boot, the start + job would hold the container's start for as long as tuwunel's start takes.) + tuwunel's own start, which replays the database's write-ahead log from the beginning at every open, has its own limit of 30 minutes, and extends it while it reports progress. While it starts, the gateway answers for the homeserver with an error, and the swarm's matrix diff --git a/nix/container-modules/swarm-container.nix b/nix/container-modules/swarm-container.nix index 927ed73ca..a8b83d7b5 100644 --- a/nix/container-modules/swarm-container.nix +++ b/nix/container-modules/swarm-container.nix @@ -21,8 +21,8 @@ # # A daemon whose own start can outlast that limit (a database open that replays # a large log) is named in `startAfterReady`: it leaves the boot transaction, so -# the container is up without it, and a oneshot in the transaction starts it -# `--no-block`. +# the container is up without it, and a unit that waits for the boot to end +# starts it `--no-block`. { config, lib, @@ -53,7 +53,8 @@ in Names of this container's services (without `.service`) that start after the container's boot instead of in it. Each stops being wanted by `multi-user.target`, and `swarm-container-deferred-start.service`, - which is, starts them with `systemctl start --no-block`. The host's + which is, starts them with `systemctl start --no-block` once the + guest's boot is over. The host's start job for the container then ends when the guest's boot does, whatever the service's own start takes. The service keeps its own `TimeoutStartSec`, and whatever depends on it has to cope with a @@ -100,18 +101,23 @@ in wantedBy = lib.mkForce [ ]; }) // { - # Its own job ends at the enqueue, so the boot does not wait on the - # services it starts. + # A `--no-block` start still queues the service's start job, and the + # container's start ends when the guest's job queue is empty + # (nixos-containers.nix passes `--notify-ready=yes`, and the host + # unit is `Type=notify`). So the job is queued after the boot, not + # in it: this unit is `Type=simple`, whose own start job ends at the + # fork, and it waits for the manager to leave `starting` before it + # queues the services. swarm-container-deferred-start = { - description = "start the services that outlast the container's start limit"; + description = "start the services that outlast the container's start limit, once its boot is over"; wantedBy = [ "multi-user.target" ]; - serviceConfig = { - Type = "oneshot"; - TimeoutStartSec = 20; - ExecStart = "${pkgs.systemd}/bin/systemctl start --no-block ${ - lib.concatMapStringsSep " " (n: "${n}.service") cfg.startAfterReady - }"; - }; + path = [ pkgs.systemd ]; + serviceConfig.Type = "simple"; + script = '' + # `degraded` (a unit of the boot failed) exits 1 and is still over. + systemctl is-system-running --wait >/dev/null || true + systemctl start --no-block ${lib.concatMapStringsSep " " (n: "${n}.service") cfg.startAfterReady} + ''; }; }; }) diff --git a/nix/module-eval/swarm-services-switch.nix b/nix/module-eval/swarm-services-switch.nix index 09bc57f1b..814b06fa7 100644 --- a/nix/module-eval/swarm-services-switch.nix +++ b/nix/module-eval/swarm-services-switch.nix @@ -990,9 +990,14 @@ let # the beginning every time) can outlast the container's one-minute limit, # and a start killed on it makes no progress. So tuwunel is out of the # guest's boot: not wanted by `multi-user.target`, started `--no-block` by - # a oneshot that is, with a limit of its own. The sync and the render stay - # in the boot wait, the presence arms of the walk that finds tuwunel - # absent from it. + # a unit that is, with a limit of its own. A `--no-block` start still + # queues a job and the container's start ends when the guest's queue is + # empty (a oneshot starter, which queued it during the boot, held the + # container's start for the daemon's 90 s in the nightly VM test), so the + # starter is `Type=simple`, holds no job, and queues the daemon only after + # `is-system-running --wait` says the boot is over. The sync and the + # render stay in the boot wait, the presence arms of the walk that finds + # tuwunel absent from it. name = "the homeserver starts after the container is up, under a limit of its own, and the container's boot does not wait on it"; ok = let @@ -1006,9 +1011,9 @@ let && unit.wantedBy == [ ] && bootWait.spanSeconds unit.serviceConfig.TimeoutStartSec == 1800 && starter.wantedBy == [ "multi-user.target" ] - && starter.serviceConfig.Type == "oneshot" - && starter.serviceConfig.TimeoutStartSec == 20 - && lib.hasSuffix "/bin/systemctl start --no-block tuwunel.service" starter.serviceConfig.ExecStart + && starter.serviceConfig.Type == "simple" + && !(bootWait.holdsJob starter) + && lib.hasInfix "systemctl is-system-running --wait >/dev/null || true\nsystemctl start --no-block tuwunel.service" starter.script && !(lib.elem "tuwunel.service" booted) && lib.elem "swarm-container-deferred-start.service" booted && lib.elem "swarm-matrix-appservice-sync.service" booted diff --git a/nix/vm-tests/container-deferred-start.nix b/nix/vm-tests/container-deferred-start.nix index 2a3e4f164..b833a0adf 100644 --- a/nix/vm-tests/container-deferred-start.nix +++ b/nix/vm-tests/container-deferred-start.nix @@ -104,8 +104,12 @@ pkgs.testers.runNixOSTest { out = host.succeed("journalctl -u container@new.service --no-pager -o cat") assert "start operation timed out" not in out, out host.succeed("systemctl is-active container@new.service") - state = inside("new", "systemctl show -p ActiveState --value slow.service") - assert state == "activating", state + # The guest queues the daemon's start job once its boot is over, which is + # just after the container's start ends. + host.wait_until_succeeds( + "test \"$(nixos-container run new -- systemctl show -p ActiveState --value slow.service)\" = activating", + timeout=60, + ) with subtest("the dependant fails while the daemon starts"): assert inside("new", "systemctl show -p ActiveState --value client.service") != "active"