Repository navigation
Description
The original title of this issue was
systemd services are not restarted reliably when package is upgraded
It was renamed after the root cause was identified to be systemd timers in #49415 (comment) and after.
Issue description
I just deployed the upgrade from Consul 0.9.3 -> 1.3.0 (#49165) to my 18.03 server cluster (in my case, I upgraded from 1.0.6).
The machines run identical configurations deployed by nixops, and deploying should have stopped all old consul processes and launched the new version.
Yet I found that on 2 out of 7 machines, the old consul version was still running.
On a node where the deployment worked, the journalctl looks like this:
Oct 29 14:44:42 node-1 nixos[22687]: switching to system configuration /nix/store/fxfk4mfi5b90h1vl0hj99gy57zzpl9hy-nixos-system-node-1-18.03pre-git
Oct 29 14:44:42 node-1 systemd[1]: Stopping rsync CDN files to CDN nodes...
Oct 29 14:44:42 node-1 systemd[1]: Stopping ceph-mount.service...
Oct 29 14:44:42 node-1 systemd[1]: Stopping cephfsRunningCheckWatchdog.service...
Oct 29 14:44:42 node-1 systemd[1]: Stopping Nginx Web Server...
Oct 29 14:44:42 node-1 systemd[1]: Stopping consul-alerts.service...
Oct 29 14:44:42 node-1 systemd[1]: Stopped target Local File Systems.
Oct 29 14:44:42 node-1 systemd[1]: Stopped target All Network Interfaces (deprecated).
Oct 29 14:44:42 node-1 systemd[1]: Stopped target Remote File Systems.
Oct 29 14:44:42 node-1 systemd[1]: Stopped server.service.
Oct 29 14:44:42 node-1 systemd[1]: Stopped consul-alerts.service.
Oct 29 14:44:42 node-1 systemd[1]: Stopping consul.service...
Oct 29 14:44:42 node-1 consul[22232]: 2018/10/29 14:44:42 [INFO] Caught signal: terminated
Oct 29 14:44:42 node-1 systemd[1]: Stopped cephfsRunningCheckWatchdog.service.
Oct 29 14:44:42 node-1 systemd[1]: Stopped Nginx Web Server.
Oct 29 14:44:43 node-1 systemd[1]: Stopped rsync CDN files to CDN nodes.
Oct 29 14:44:43 node-1 systemd[1]: Reloading.
Oct 29 14:44:43 node-1 systemd[1]: consul.service: Current command vanished from the unit file, execution of the command list won't be resumed.
Oct 29 14:44:43 node-1 systemd[1]: Started Create or wait for ceph..
Oct 29 14:44:43 node-1 systemd[1]: Starting ceph-mount.service...
Oct 29 14:44:43 node-1 systemd[1]: Reloading D-Bus System Message Bus.
Oct 29 14:44:43 node-1 systemd[1]: Reloaded D-Bus System Message Bus.
Oct 29 14:44:43 node-1 systemd[1]: Started ceph-mount.service.
Oct 29 14:44:43 node-1 systemd[1]: Starting ceph-mounted.service...
Oct 29 14:44:43 node-1 consul[22232]: 2018/10/29 14:44:43 [INFO] Exit code: 1
Oct 29 14:44:43 node-1 systemd[1]: consul.service: Main process exited, code=exited, status=1/FAILURE
Oct 29 14:44:43 node-1 systemd[1]: consul.service: Failed with result 'exit-code'.
Oct 29 14:44:43 node-1 systemd[1]: Starting consul.service...
Oct 29 14:44:43 node-1 systemd[1]: Started consul.service.
Oct 29 14:44:43 node-1 systemd[1]: Starting consulReady.service...
Oct 29 14:44:43 node-1 systemd[1]: Started ceph-mounted.service.
Oct 29 14:44:43 node-1 systemd[1]: Starting Nginx Web Server...
Oct 29 14:44:43 node-1 systemd[1]: Started Nginx Web Server.
Oct 29 14:44:43 node-1 systemd[1]: Starting ceph-mounted.service...
Oct 29 14:44:43 node-1 systemd[1]: Starting camPostgresRole.service...
Oct 29 14:44:43 node-1 systemd[1]: Reached target Local File Systems.
Oct 29 14:44:43 node-1 systemd[1]: Reached target All Network Interfaces (deprecated).
Oct 29 14:44:43 node-1 systemd[1]: Reached target Remote File Systems.
Oct 29 14:44:43 node-1 systemd[1]: Started ceph-mounted.service.
Oct 29 14:44:43 node-1 systemd[1]: Started camPostgresRole.service.
Oct 29 14:44:43 node-1 systemd[1]: Starting server.service...
Oct 29 14:44:44 node-1 systemd[1]: Started consulReady.service.
Oct 29 14:44:44 node-1 systemd[1]: Started rsync CDN files to CDN nodes.
Oct 29 14:44:44 node-1 systemd[1]: Starting consul-alerts.service...
Oct 29 14:44:44 node-1 systemd[1]: Started server.service.
Oct 29 14:44:44 node-1 systemd[1]: Started consul-alerts.service.
Oct 29 14:44:44 node-1 nixos[22687]: finished switching to system configuration /nix/store/fxfk4mfi5b90h1vl0hj99gy57zzpl9hy-nixos-system-node-1-18.03pre-git
Note the presence of Stopping consul.service....
On a node where it didn't work, it looks like this:
Oct 29 13:52:38 node-2 nixos[27726]: switching to system configuration /nix/store/1x475px1qyk9f9gcdk0pvpbfhip0v1rs-nixos-system-node-2-18.03pre-git
Oct 29 13:52:38 node-2 systemd[1]: Stopping rsync CDN files to CDN nodes...
Oct 29 13:52:38 node-2 systemd[1]: Stopped target Multi-User System.
Oct 29 13:52:38 node-2 systemd[1]: Stopping server.service...
Oct 29 13:52:38 node-2 systemd[1]: Stopping consul-alerts.service...
Oct 29 13:52:38 node-2 systemd[1]: Stopped target Local File Systems.
Oct 29 13:52:38 node-2 systemd[1]: Stopped target All Network Interfaces (deprecated).
Oct 29 13:52:38 node-2 systemd[1]: Stopped target Remote File Systems.
Oct 29 13:52:38 node-2 systemd[1]: Stopped server.service.
Oct 29 13:52:38 node-2 systemd[1]: Stopped rsync CDN files to CDN nodes.
Oct 29 13:52:38 node-2 systemd[1]: Starting ceph-mounted.service...
Oct 29 13:52:38 node-2 systemd[1]: Started ceph-mounted.service.
Oct 29 13:52:51 node-2 systemd[1]: Stopped consul-alerts.service.
Oct 29 13:52:51 node-2 systemd[1]: Starting consulReady.service...
Oct 29 13:52:51 node-2 systemd[1]: Started consulReady.service.
Oct 29 13:52:51 node-2 systemd[1]: Started rsync CDN files to CDN nodes.
Oct 29 13:52:51 node-2 systemd[1]: Reloading.
Oct 29 13:52:51 node-2 systemd[1]: consul.service: Current command vanished from the unit file, execution of the command list won't be resumed.
Oct 29 13:52:51 node-2 systemd[1]: cdn-rsync.service: Current command vanished from the unit file, execution of the command list won't be resumed.
Oct 29 13:52:51 node-2 systemd[1]: nginx.service: Current command vanished from the unit file, execution of the command list won't be resumed.
Oct 29 13:52:51 node-2 systemd[1]: Reloading D-Bus System Message Bus.
Oct 29 13:52:51 node-2 systemd[1]: Reloaded D-Bus System Message Bus.
Oct 29 13:52:51 node-2 systemd[1]: Stopping Nginx Web Server...
Oct 29 13:52:51 node-2 systemd[1]: Stopped Nginx Web Server.
Oct 29 13:52:51 node-2 systemd[1]: Starting ceph-mounted.service...
Oct 29 13:52:51 node-2 systemd[1]: Started ceph-mounted.service.
Oct 29 13:52:51 node-2 systemd[1]: Starting Nginx Web Server...
Oct 29 13:52:51 node-2 systemd[1]: Started Nginx Web Server.
Oct 29 13:52:51 node-2 systemd[1]: Starting ceph-mounted.service...
Oct 29 13:52:51 node-2 systemd[1]: Starting consulReady.service...
Oct 29 13:52:51 node-2 systemd[1]: Starting camPostgresRole.service...
Oct 29 13:52:51 node-2 systemd[1]: Reached target Local File Systems.
Oct 29 13:52:51 node-2 systemd[1]: Reached target Remote File Systems.
Oct 29 13:52:51 node-2 systemd[1]: Starting stolonCreatePostgresDataDir.service...
Oct 29 13:52:51 node-2 systemd[1]: Reached target All Network Interfaces (deprecated).
Oct 29 13:52:51 node-2 systemd[1]: Started ceph-mounted.service.
Oct 29 13:52:51 node-2 systemd[1]: Started stolonCreatePostgresDataDir.service.
Oct 29 13:52:51 node-2 systemd[1]: Started camPostgresRole.service.
Oct 29 13:52:51 node-2 systemd[1]: Starting server.service...
Oct 29 13:52:51 node-2 systemd[1]: Started consulReady.service.
Oct 29 13:52:51 node-2 systemd[1]: Starting stolonClusterDataReady.service...
Oct 29 13:52:51 node-2 systemd[1]: Starting consul-alerts.service...
Oct 29 13:52:51 node-2 systemd[1]: Started stolonClusterDataReady.service.
Oct 29 13:52:51 node-2 systemd[1]: Starting stolonConfigChange.service...
Oct 29 13:52:51 node-2 systemd[1]: Started stolonConfigChange.service.
Oct 29 13:52:52 node-2 systemd[1]: Started consul-alerts.service.
Oct 29 13:52:52 node-2 systemd[1]: Started server.service.
Oct 29 13:52:52 node-2 systemd[1]: Reached target Multi-User System.
Oct 29 13:52:52 node-2 nixos[27726]: finished switching to system configuration /nix/store/1x475px1qyk9f9gcdk0pvpbfhip0v1rs-nixos-system-node-2-18.03pre-git
Note the absence of Stopping consul.service...
This is despite nixops having the following output:
ceph-deployment> closures copied successfully
node-2..............> uploading key ‘secrets’...
node-2..............> uploading key ‘nginxSSLprivkey’...
node-2..............> uploading key ‘cdn-rsync-ssh-privkey’...
node-2..............> updating GRUB 2 menu...
node-2..............> stopping the following units: cdn-rsync.service, server.service, consul-alerts.service, consul.service
node-2..............> Job for consul.service canceled.
node-2..............> activating the configuration...
node-2..............> warning: user ‘postgres’ has unknown group ‘postgres’
node-2..............> setting up /etc...
node-2..............> setting up tmpfiles
node-2..............> reloading the following units: dbus.service
node-2..............> restarting the following units: nginx.service
node-2..............> starting the following units: cdn-rsync.service, server.service, consul-alerts.service, consul.service
node-2..............> activation finished successfully
ceph-deployment> deployment finished successfully
Observe how:
stopping the following units: ..., consul.servicewas printed- but on the machine,
Stopping consul.servicedidn't occur.
Possibly related is the systemd warning consul.service: Current command vanished from the unit file.
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Activity
arianvp commentedon Oct 31, 2018
Did you have
Enabled? Perhaps leaving took to long and systemd bailed out?
If you can tell me whether you had this option enabled or not I can try to reproduce the issue
arianvp commentedon Oct 31, 2018
I actually think that that config is buggy!It should be
ExecStopPre= consul leaveExecStopis responsible for killing the PID whichconsul leavedoesn't do iircedit: Nope
consul leavedoes exit the unit gracefullyarianvp commentedon Oct 31, 2018
Related: #33172
You also seem to have opened that issue :). I think the
Current command vanished from the unit filepart is very relevantnh2 commentedon Oct 31, 2018
@arianvp No, I have no
ExecStopinsystemctl cat consul.I have:
Also, I think that if systemd tried to stop consul at all, even via
ExecStop, it would have printedStopping consul.service...(which it didn't), wouldn't it?nh2 commentedon Oct 31, 2018
The difference to #33172 is that there it at least says
Stopping Nginx Web Server....arianvp commentedon Oct 31, 2018
I just noticed
This is a string that
systemctl stopreturns when it fails to stop the job (See https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=768208 for example)arianvp commentedon Oct 31, 2018
Further research:
What is certain, is that whatever caused the
Job canceled for consul.service, caused the service to not stopedit: Scrap this part below. I just realised that in the logs that failed to have
Stopping consul...consuldidn't exit with a non-zero exit code
Details
`SIGTERM` causes a `consul server` to exit with exit code `1`, which (I think?) could trigger the `Restart` , and thus a race condition occurs between `systemctl stop` and `Restart`, causing the stop job to be canceled. Though I am a bit skeptical about this hypothesis, since you don't have `Stopping consul.service ...` in your logs.Whilst non-servers gracefully exit on
SIGTERM:I think the fix here is to make the the systemd unit send
SIGINTinstead ofSIGTERM, as advised here: https://www.consul.io/docs/agent/basics.html#stopping-an-agentThis should fix the race condition occuring.
I haven't been able to trigger the race condition though, so I might be wrong. on my local machine
systemctl stop consulworks fine ,even in-servermode.nh2 commentedon Nov 1, 2018
I can actually reproduce this part.
If I just type
systemctl stop consulon my deployed system, I immediately getJob for consul.service canceled.Afterwards
systemctl status consulshows the service as running.However,
journalctlalso shows that consul did get a stop command (agent: Caught signal: terminated) and exited 1. But then it restarted (withStarting consul.service...before it).nh2 commentedon Nov 1, 2018
c0bw3b commentedon Nov 1, 2018
From the doc regarding
Restart=:I'm not sure but maybe setting
Restart=on-abnormalin the service unit would allow you to stop the service manually? i.e. the stop job would not be canceled immediatelynh2 commentedon Nov 1, 2018
Related:
The NixOS consul systemd service sets
TimeoutStartSec = "0";, which is likely wrong (at least neither me norboucmanfrom IRC#systemdcan guess what that's for).I asked about that on 9ce0c1cb7113#r23138805 but never got an answer from @wkennington.
osquery: init at 2.5.2
nh2 commentedon Nov 1, 2018
Another service that have a suspicious
TimeoutStartSec=0:nixpkgs/nixos/modules/services/monitoring/osquery.nix
Line 81 in 89ede97
(I commented). (Update: Answer that it's still unclear. I filed #49700 for it.)
nh2 commentedon Nov 2, 2018
I have filed one systemd issue that seems to be at least related: systemd/systemd#10626
I don't think the timer issue explains the full situation, because of the
from the issue description where consul didn't get restarted, only 1 had a systemd timer installed.
nh2 commentedon Nov 2, 2018
I am getting more and more convinced that the
Job for ... canceled.issue and the problem of an old version running are totally separate.In particular, I suspect that the old-version problem has more to do with
Current command vanished from the unit file, execution of the command list won't be resumed..I have done the following experiment, which supports this:
I've started modifying
nixpkgs/nixos/modules/services/networking/consul.nix
Line 181 in c9e1380
adding more and more
-uiswitches and deploying with nixops in between. After the deploy is done, I runps aux | grep consul | grep agentto see what command line is running.After a deploy with
-ui -ui -ui -ui -ui(5-ui),psshows only 4-ui:That is even though
systemctl catshows 5-ui:I had
systemd-analyze log-level debugon, and injournalctl -fcould see that indeed systemd did start consul with the command line showing only 4-ui:And in this case, no
Job for consul.service canceledwas printed in nixops.Trying this experiment more times, I also observe deploys where
Job for consul.service canceledis printed, but consul starts with the correct number of-uis nevertheless.27 remaining items
nh2 commentedon Apr 28, 2024
I keep encountering this; posting a concrete situation again, this time with the
nebulaVPN service (and a timer that starts a service thatrequires[email protected]):Here we see again:
Job for [email protected] canceled.nh2 commentedon Apr 28, 2024
@arianvp
Cannot we just replace
5. startbyrestart?The service would still be
stopin the earlier step.But
restartwould make sure that if a timer triggered a restart duing1. stop,I believe that's what @Gabriella439 meant in #49415 (comment): To replace
1. stop + 5. startby1. stop + 5. restart, not to replace the whole thing by a singlerestart.So, concretely, I propose:
nixpkgs/nixos/modules/system/activation/switch-to-configuration.pl
Lines 944 to 946 in f7ae5ea
Edit:
This doesn't work trivially, it makes the system hang because it restarts the network. Why? Because in this commit the logic was introduced that what's started isn't actually what's printed:
[-]systemd services are not restarted reliably when package is upgraded[/-][+]systemd timers can result in changed services not being restarted reliably[/+]nh2 commentedon Apr 28, 2024
I am reopening this issue to
@flokli That isn't quite correct, at least not as written.
In here,
reloadingisn't involved at all. This issue tracks the fact that evenstart + stopdoes not work, because thestartcan be replaced by a too-earlyrestartin the presence of timers.I am re-opening this issue because I believe we can fix this issue --
stopnot happening at all -- more easily than #49528 ("Interactions ofrestartIfChanged,reloadIfChanged,stopIfChanged").nh2 commentedon Apr 28, 2024
I have created a PR that implements
stop + restart(instead ofstop + start) of the stopped units:#307562
Please help test!
switch-to-configuration.pl: Fix timers resulting in old unit running. F…
nh2 commentedon May 30, 2026
Update:
switch-to-configuration.plwas replaced byswitch-to-configuration-ng, which became default in NixOS 24.11 andswitch-to-configuration.plwas removed with NixOS 25.11.switch-to-configuration-ngstill has the bug: #308801 (comment)PR to fix it for that: