From 93877f6b765fc25f52d4104a8049783cdc38b418 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Fri, 13 Jul 2018 11:25:59 -0600 Subject: [PATCH 1/2] We also need to remove the handler when cancelling async request Should fix GitHub #353 --- src/async.zsh | 3 ++- zsh-autosuggestions.zsh | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/async.zsh b/src/async.zsh index eefdf4a..296c1c2 100644 --- a/src/async.zsh +++ b/src/async.zsh @@ -10,8 +10,9 @@ _zsh_autosuggest_async_request() { # If we've got a pending request, cancel it if [[ -n "$_ZSH_AUTOSUGGEST_ASYNC_FD" ]] && { true <&$_ZSH_AUTOSUGGEST_ASYNC_FD } 2>/dev/null; then - # Close the file descriptor + # Close the file descriptor and remove the handler exec {_ZSH_AUTOSUGGEST_ASYNC_FD}<&- + zle -F $_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 diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 8169cc8..3e7560a 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -689,8 +689,9 @@ _zsh_autosuggest_async_request() { # If we've got a pending request, cancel it if [[ -n "$_ZSH_AUTOSUGGEST_ASYNC_FD" ]] && { true <&$_ZSH_AUTOSUGGEST_ASYNC_FD } 2>/dev/null; then - # Close the file descriptor + # Close the file descriptor and remove the handler exec {_ZSH_AUTOSUGGEST_ASYNC_FD}<&- + zle -F $_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 From 88fe824ddfe1bb635f93e95098346725d864284a Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Fri, 13 Jul 2018 11:26:57 -0600 Subject: [PATCH 2/2] Add some error handling to async response handler We only want to read data in case of POLLIN or POLLHUP. Not POLLNVAL or select error. We always want to remove the handler, so it doesn't get called in an infinite loop when error is nval or err. In zsh source, see main zle event loop in zle_main.c raw_getbyte function. --- src/async.zsh | 12 ++++++++---- zsh-autosuggestions.zsh | 12 ++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/async.zsh b/src/async.zsh index 296c1c2..5c19a81 100644 --- a/src/async.zsh +++ b/src/async.zsh @@ -41,10 +41,14 @@ _zsh_autosuggest_async_request() { # First arg will be fd ready for reading # Second arg will be passed in case of error _zsh_autosuggest_async_response() { - # Read everything from the fd and give it as a suggestion - zle autosuggest-suggest -- "$(cat <&$1)" + if [[ -z "$2" || "$2" == "hup" ]]; then + # Read everything from the fd and give it as a suggestion + zle autosuggest-suggest -- "$(cat <&$1)" - # Remove the handler and close the fd + # Close the fd + exec {1}<&- + fi + + # Always remove the handler zle -F "$1" - exec {1}<&- } diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 3e7560a..27c42c6 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -720,12 +720,16 @@ _zsh_autosuggest_async_request() { # First arg will be fd ready for reading # Second arg will be passed in case of error _zsh_autosuggest_async_response() { - # Read everything from the fd and give it as a suggestion - zle autosuggest-suggest -- "$(cat <&$1)" + if [[ -z "$2" || "$2" == "hup" ]]; then + # Read everything from the fd and give it as a suggestion + zle autosuggest-suggest -- "$(cat <&$1)" - # Remove the handler and close the fd + # Close the fd + exec {1}<&- + fi + + # Always remove the handler zle -F "$1" - exec {1}<&- } #--------------------------------------------------------------------#