923a60
From fe81f6f734ee46a4877df6dda6e31cdc24c00a3c Mon Sep 17 00:00:00 2001
923a60
From: =?UTF-8?q?Michal=20Koutn=C3=BD?= <mkoutny@suse.com>
923a60
Date: Tue, 16 Jan 2018 19:22:46 +0100
923a60
Subject: [PATCH] core/timer: Prevent timer looping when unit cannot start
923a60
923a60
When a unit job finishes early (e.g. when fork(2) fails) triggered unit goes
923a60
through states
923a60
        stopped->failed (or failed->failed),
923a60
in case a ExecStart= command fails unit passes through
923a60
        stopped->starting->failed.
923a60
923a60
The former transition doesn't result in unit active/inactive timestamp being
923a60
updated and timer (OnUnitActiveSec= or OnUnitInactiveSec=) would use an expired
923a60
timestamp triggering immediately again (repeatedly).
923a60
923a60
This patch exploits timer's last trigger timestamp to ensure the timer isn't
923a60
triggered more frequently than OnUnitActiveSec=/OnUnitInactiveSec= period.
923a60
923a60
Steps to reproduce:
923a60
923a60
0) Create sample units:
923a60
923a60
cat >~/.config/systemd/user/looper.service <
923a60
[Service]
923a60
ExecStart=/usr/bin/sleep 2
923a60
EOD
923a60
923a60
cat >~/.config/systemd/user/looper.timer <
923a60
[Timer]
923a60
AccuracySec=5
923a60
OnUnitActiveSec=5
923a60
EOD
923a60
923a60
1) systemctl --user daemon-reload
923a60
923a60
2) systemctl --user start looper.timer
923a60
   # to have first activation timestamp/sentinel
923a60
   systemctl --user start looper.service
923a60
923a60
o  Observe the service is being regularly triggered.
923a60
923a60
3) systemctl set-property user@$UID.service TasksMax=2
923a60
923a60
o  Observe the tight looping as long as the looper.service cannot be started.
923a60
923a60
Ref: #5969
923a60
(cherry picked from commit 204d140c4def364c47d36226e4514a7e077fa196)
923a60
923a60
Resolves: #1710302
923a60
---
923a60
 src/core/timer.c | 2 ++
923a60
 1 file changed, 2 insertions(+)
923a60
923a60
diff --git a/src/core/timer.c b/src/core/timer.c
923a60
index d32b007c7c..1d4868643a 100644
923a60
--- a/src/core/timer.c
923a60
+++ b/src/core/timer.c
923a60
@@ -416,6 +416,7 @@ static void timer_enter_waiting(Timer *t, bool initial) {
923a60
 
923a60
                                 if (base <= 0)
923a60
                                         continue;
923a60
+                                base = MAX(base, t->last_trigger.monotonic);
923a60
 
923a60
                                 break;
923a60
 
923a60
@@ -428,6 +429,7 @@ static void timer_enter_waiting(Timer *t, bool initial) {
923a60
 
923a60
                                 if (base <= 0)
923a60
                                         continue;
923a60
+                                base = MAX(base, t->last_trigger.monotonic);
923a60
 
923a60
                                 break;
923a60