event/process.c: send SIGTERM directly (#6644)

Send SIGTERM to processes directly, instead of waiting for ~1s.

- removes TERM_TIMEOUT
- changes KILL_TIMEOUT to milliseconds
- removes Process.term_sent
This commit is contained in:
Daniel Hahler 2017-05-04 16:38:25 +02:00 committed by Justin M. Keyes
parent 052c2d0a0f
commit 34c3f03013
2 changed files with 11 additions and 14 deletions

View File

@ -20,9 +20,9 @@
# include "event/process.c.generated.h" # include "event/process.c.generated.h"
#endif #endif
// Time (ns) for a process to exit cleanly before we send TERM/KILL. // Time for a process to exit cleanly before we send KILL.
#define TERM_TIMEOUT 1000000000 #define KILL_TIMEOUT_MS 2000
#define KILL_TIMEOUT (TERM_TIMEOUT * 2) #define KILL_TIMEOUT_NS (KILL_TIMEOUT_MS * 1000000)
#define CLOSE_PROC_STREAM(proc, stream) \ #define CLOSE_PROC_STREAM(proc, stream) \
do { \ do { \
@ -121,8 +121,6 @@ void process_teardown(Loop *loop) FUNC_ATTR_NONNULL_ALL
// Close handles to process without killing it. // Close handles to process without killing it.
CREATE_EVENT(loop->events, process_close_handles, 1, proc); CREATE_EVENT(loop->events, process_close_handles, 1, proc);
} else { } else {
uv_kill(proc->pid, SIGTERM);
proc->term_sent = true;
process_stop(proc); process_stop(proc);
} }
} }
@ -244,12 +242,16 @@ void process_stop(Process *proc) FUNC_ATTR_NONNULL_ALL
abort(); abort();
} }
ILOG("Sending SIGTERM to pid %d", proc->pid);
uv_kill(proc->pid, SIGTERM);
Loop *loop = proc->loop; Loop *loop = proc->loop;
if (!loop->children_stop_requests++) { if (!loop->children_stop_requests++) {
// When there's at least one stop request pending, start a timer that // When there's at least one stop request pending, start a timer that
// will periodically check if a signal should be send to a to the job // will periodically check if a signal should be send to the job.
DLOG("Starting job kill timer"); DLOG("Starting job kill timer");
uv_timer_start(&loop->children_kill_timer, children_kill_cb, 100, 100); uv_timer_start(&loop->children_kill_timer, children_kill_cb,
KILL_TIMEOUT_MS, 100);
} }
} }
@ -267,11 +269,7 @@ static void children_kill_cb(uv_timer_t *handle)
} }
uint64_t elapsed = now - proc->stopped_time; uint64_t elapsed = now - proc->stopped_time;
if (!proc->term_sent && elapsed >= TERM_TIMEOUT) { if (elapsed >= KILL_TIMEOUT_NS) {
ILOG("Sending SIGTERM to pid %d", proc->pid);
uv_kill(proc->pid, SIGTERM);
proc->term_sent = true;
} else if (elapsed >= KILL_TIMEOUT) {
ILOG("Sending SIGKILL to pid %d", proc->pid); ILOG("Sending SIGKILL to pid %d", proc->pid);
uv_kill(proc->pid, SIGKILL); uv_kill(proc->pid, SIGKILL);
} }

View File

@ -26,7 +26,7 @@ struct process {
Stream *in, *out, *err; Stream *in, *out, *err;
process_exit_cb cb; process_exit_cb cb;
internal_process_cb internal_exit_cb, internal_close_cb; internal_process_cb internal_exit_cb, internal_close_cb;
bool closed, term_sent, detach; bool closed, detach;
MultiQueue *events; MultiQueue *events;
}; };
@ -48,7 +48,6 @@ static inline Process process_init(Loop *loop, ProcessType type, void *data)
.err = NULL, .err = NULL,
.cb = NULL, .cb = NULL,
.closed = false, .closed = false,
.term_sent = false,
.internal_close_cb = NULL, .internal_close_cb = NULL,
.internal_exit_cb = NULL, .internal_exit_cb = NULL,
.detach = false .detach = false