From 40f4df13909bfcf2622877a5ccd243fc09fb71ff Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Tue, 11 May 2021 15:55:33 +0200 Subject: [PATCH] Add timeout 100ms before every kill restconf iteration --- lib/clixon/clixon_proc.h | 1 - lib/src/clixon_proc.c | 46 +++++++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/lib/clixon/clixon_proc.h b/lib/clixon/clixon_proc.h index ed5954ba..4bf4e66c 100644 --- a/lib/clixon/clixon_proc.h +++ b/lib/clixon/clixon_proc.h @@ -69,7 +69,6 @@ int clixon_process_delete_all(clicon_handle h); int clixon_process_operation(clicon_handle h, const char *name, proc_operation op, const int wrapit); int clixon_process_status(clicon_handle h, const char *name, cbuf *cbret); int clixon_process_start_all(clicon_handle h); -int clixon_process_sched_register(clicon_handle h); int clixon_process_waitpid(clicon_handle h); #endif /* _CLIXON_PROC_H_ */ diff --git a/lib/src/clixon_proc.c b/lib/src/clixon_proc.c index 6e0b9766..090a1651 100644 --- a/lib/src/clixon_proc.c +++ b/lib/src/clixon_proc.c @@ -75,7 +75,7 @@ | stop/| | | restart| | restart | v | - wait(stop) ------- EXITING(dying pid) + wait(stop) ------- EXITING(dying pid) <----> kill after timeout */ #ifdef HAVE_CONFIG_H @@ -151,6 +151,10 @@ struct process_entry_t { proc_cb_t *pe_callback; /* Wrapper function, may be called from process_operation */ }; + +/* Forward declaration */ +static int clixon_process_sched_register(clicon_handle h, int delay); + static void clixon_proc_sigint(int sig) { @@ -598,6 +602,7 @@ clixon_process_operation(clicon_handle h, proc_operation op; int sched = 0; /* If set, process action should be scheduled, register a timeout */ int isrunning = 0; + int delay = 0; clicon_debug(1, "%s name:%s op:%s", __FUNCTION__, name, clicon_int2str(proc_operation_map, op0)); if (_proc_entry_list == NULL) @@ -624,7 +629,7 @@ clixon_process_operation(clicon_handle h, clicon_log(LOG_NOTICE, "Killing old process %s with pid: %d", pe->pe_name, pe->pe_pid); /* XXX pid may be 0 */ kill(pe->pe_pid, SIGTERM); - + delay = 1; } clicon_debug(1, "%s %s(%d) %s --%s--> %s", __FUNCTION__, pe->pe_name, pe->pe_pid, @@ -633,9 +638,8 @@ clixon_process_operation(clicon_handle h, clicon_int2str(proc_state_map, PROC_STATE_EXITING) ); pe->pe_state = PROC_STATE_EXITING; /* Keep operation stop/restart */ - // break; - } - sched++; + } + sched++;/* start: immediate stop/restart: not immediate: wait timeout */ } else{ clicon_debug(1, "%s name:%s op %s cancelled by wrwap", __FUNCTION__, name, clicon_int2str(proc_operation_map, op0)); @@ -644,7 +648,7 @@ clixon_process_operation(clicon_handle h, } pe = NEXTQ(process_entry_t *, pe); } while (pe != _proc_entry_list); - if (sched && clixon_process_sched_register(h) < 0) + if (sched && clixon_process_sched_register(h, delay) < 0) goto done; ok: retval = 0; @@ -741,11 +745,11 @@ clixon_process_start_all(clicon_handle h) goto done; if (op == PROC_OP_START){ pe->pe_operation = op; - sched++; + sched++; /* Immediate dont delay for start */ } pe = NEXTQ(process_entry_t *, pe); } while (pe != _proc_entry_list); - if (sched && clixon_process_sched_register(h) < 0) + if (sched && clixon_process_sched_register(h, 0) < 0) goto done; ok: retval = 0; @@ -793,7 +797,7 @@ clixon_process_sched(int fd, clicon_log(LOG_NOTICE, "Killing old process %s with pid: %d", pe->pe_name, pe->pe_pid); /* XXX pid may be 0 */ kill(pe->pe_pid, SIGTERM); - sched++; + sched++; /* Not immediate: wait timeout */ } default: break; @@ -853,7 +857,7 @@ clixon_process_sched(int fd, } pe = NEXTQ(process_entry_t *, pe); } while (pe != _proc_entry_list); - if (sched && clixon_process_sched_register(h) < 0) + if (sched && clixon_process_sched_register(h, 1) < 0) goto done; ok: retval = 0; @@ -863,23 +867,25 @@ clixon_process_sched(int fd, } /*! Register scheduling of process start/stop/restart - * After a delay t1, schedule the process - * @note The delay is for mitigating a race condition if a process is restarted that is used in the - * session restarting it. In this way, the process "should have" time to exit. - * However, for slow machines, this delay may need to be longer. - * On a Raspberry pi it was measured to need be 1.5ms. - * However, if it is much longer, it may kill restconf as an unrelated session has been opened. + * + * Schedule a process event. There are two cases: + * 1) A process has been killed and is in EXITING, after a delay kill again. + * 2) A process is started, dont delay + * @param[in] h Clixon handle + * @param[in] delay If 0 dont add a delay, if 1 add a delay */ -int -clixon_process_sched_register(clicon_handle h) +static int +clixon_process_sched_register(clicon_handle h, + int delay) { int retval = -1; struct timeval t; - struct timeval t1 = {0, 1500}; /* See discussion ^*/ + struct timeval t1 = {0, 100000}; /* 100ms */ clicon_debug(2, "%s", __FUNCTION__); gettimeofday(&t, NULL); - timeradd(&t, &t1, &t); + if (delay) + timeradd(&t, &t1, &t); if (clixon_event_reg_timeout(t, clixon_process_sched, h, "process") < 0) goto done; retval = 0;