From 7ab212490470870bdf86074becc10d9954bfd5a0 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Fri, 13 Jul 2018 21:48:25 -0600 Subject: [PATCH] Kill async process by id when job control disabled --- src/async.zsh | 15 ++++++++++++--- zsh-autosuggestions.zsh | 15 ++++++++++++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/async.zsh b/src/async.zsh index eefdf4a..814a185 100644 --- a/src/async.zsh +++ b/src/async.zsh @@ -13,9 +13,18 @@ _zsh_autosuggest_async_request() { # Close the file descriptor exec {_ZSH_AUTOSUGGEST_ASYNC_FD}<&- - # Assume the child process created a new process group and send - # TERM to the group to attempt to kill all descendent processes - kill -TERM -$_ZSH_AUTOSUGGEST_CHILD_PID 2>/dev/null + # Zsh will make a new process group for the child process only if job + # control is enabled (MONITOR option) + if [[ -o MONITOR ]]; then + # Send the signal to the process group to kill any processes that may + # have been forked by the suggestion strategy + kill -TERM -$_ZSH_AUTOSUGGEST_CHILD_PID 2>/dev/null + else + # Kill just the child process since it wasn't placed in a new process + # group. If the suggestion strategy forked any child processes they may + # be orphaned and left behind. + kill -TERM $_ZSH_AUTOSUGGEST_CHILD_PID 2>/dev/null + fi fi # Fork a process to fetch a suggestion and open a pipe to read from it diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 8169cc8..191cda1 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -692,9 +692,18 @@ _zsh_autosuggest_async_request() { # Close the file descriptor exec {_ZSH_AUTOSUGGEST_ASYNC_FD}<&- - # Assume the child process created a new process group and send - # TERM to the group to attempt to kill all descendent processes - kill -TERM -$_ZSH_AUTOSUGGEST_CHILD_PID 2>/dev/null + # Zsh will make a new process group for the child process only if job + # control is enabled (MONITOR option) + if [[ -o MONITOR ]]; then + # Send the signal to the process group to kill any processes that may + # have been forked by the suggestion strategy + kill -TERM -$_ZSH_AUTOSUGGEST_CHILD_PID 2>/dev/null + else + # Kill just the child process since it wasn't placed in a new process + # group. If the suggestion strategy forked any child processes they may + # be orphaned and left behind. + kill -TERM $_ZSH_AUTOSUGGEST_CHILD_PID 2>/dev/null + fi fi # Fork a process to fetch a suggestion and open a pipe to read from it