diff mbox series

[LEDE-DEV] Fix resetting of "respawn" flag

Message ID 1524839553-14226-1-git-send-email-thement@ibawizard.net
State Not Applicable
Headers show
Series [LEDE-DEV] Fix resetting of "respawn" flag | expand

Commit Message

Jakub Horak April 27, 2018, 2:32 p.m. UTC
This patch stops using instance "respawn" flag as a state variable. This fixes
the bug when instance loses "respawn" flag after it has been restarted (if
instance is started before the process has exited, it will not copy all
parameters).
---
 service/instance.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
diff mbox series

Patch

diff --git a/service/instance.c b/service/instance.c
index a968a0b..5aab23a 100644
--- a/service/instance.c
+++ b/service/instance.c
@@ -506,12 +506,17 @@  instance_timeout(struct uloop_timeout *t)
 
 	in = container_of(t, struct service_instance, timeout);
 
-	if (in->halt) {
-		LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
-				in->srv->name, in->name, in->proc.pid);
-		kill(in->proc.pid, SIGKILL);
-	} else if (in->restart || in->respawn)
-		instance_start(in);
+	if (!in->proc.pending) {
+		if (in->respawn) {
+			instance_start(in);
+		}
+	} else {
+		if (in->halt) {
+			LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
+					in->srv->name, in->name, in->proc.pid);
+			kill(in->proc.pid, SIGKILL);
+		}
+	}
 }
 
 static void
@@ -545,6 +550,7 @@  instance_exit(struct uloop_process *p, int ret)
 			service_stopped(s);
 		}
 	} else if (in->restart) {
+		/* can never happen */
 		instance_start(in);
 	} else if (in->respawn) {
 		if (runtime < in->respawn_threshold)
@@ -554,8 +560,8 @@  instance_exit(struct uloop_process *p, int ret)
 		if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
 			LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
 								in->srv->name, in->name, in->respawn_count, runtime);
-			in->restart = in->respawn = 0;
-			in->halt = 1;
+			in->restart = false;
+			in->halt = true;
 			service_event("instance.fail", in->srv->name, in->name);
 		} else {
 			service_event("instance.respawn", in->srv->name, in->name);
@@ -570,7 +576,7 @@  instance_stop(struct service_instance *in)
 	if (!in->proc.pending)
 		return;
 	in->halt = true;
-	in->restart = in->respawn = false;
+	in->restart = false;
 	kill(in->proc.pid, SIGTERM);
 	uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
 }