mirror of
https://github.com/zsh-users/zsh-autosuggestions.git
synced 2024-11-18 09:51:06 +01:00
Clear async global variables on successful response
If _ZSH_AUTOSUGGEST_ASYNC_FD is not cleared, something else may open the file descriptor pointed to by it. And then the next call into _zsh_autosuggest_async_request will close it causing trouble. It seems like good practice to clean up _ZSH_AUTOSUGGEST_CHILD_PID as well, though it's not directly causing any known problems at the moment. I was able to produce errors with ZSH_AUTOSUGGEST_MANUAL_REBIND active and sourcing a file async-widget-setup.zsh after the first precmd with the plugin active. If manual rebind is not active or if the widgets are created before the first precmd, then zsh-autosuggestions wraps the widgets and for some reason we don't seem to get any fd conflicts. The .zshrc: ``` ZSH_AUTOSUGGEST_MANUAL_REBIND=true source zsh-autosuggestions.zsh ``` and async-widget-setup.zsh: ``` function async-widget-fork() { exec {fd}< <(echo foo) zle -M "opened: $fd, autosuggest fd: $_ZSH_AUTOSUGGEST_ASYNC_FD" } function async-widget-read() { zle -M "reading from $fd: $(cat <&$fd)" } zle -N async-widget-fork zle -N async-widget-read bindkey ^A async-widget-fork bindkey ^B async-widget-read ``` Then run `ZDOTDIR=$PWD zsh` and run `source async-widget-setup.zsh`. At the next prompt, type one character e.g. "a" to trigger an async request/response cycle. This leaves _ZSH_AUTOSUGGEST_ASYNC_FD set to the stale file descriptor number. Then press ^A to activate the fork. This will set the fd parameter to the same number as _ZSH_AUTOSUGGEST_ASYNC_FD. Then type another character e.g. "a" to trigger an async request. This will print a "No handler installed" error and close the file descriptor pointed to by both _ZSH_AUTOSUGGEST_ASYNC_FD and fd. Pressing ^B at this point will fail to read with a "bad file descriptor" error.
This commit is contained in:
parent
61257de667
commit
3391962a15
2 changed files with 6 additions and 0 deletions
|
@ -58,6 +58,7 @@ _zsh_autosuggest_async_request() {
|
||||||
_zsh_autosuggest_async_response() {
|
_zsh_autosuggest_async_response() {
|
||||||
emulate -L zsh
|
emulate -L zsh
|
||||||
|
|
||||||
|
typeset -g _ZSH_AUTOSUGGEST_ASYNC_FD _ZSH_AUTOSUGGEST_CHILD_PID
|
||||||
local suggestion
|
local suggestion
|
||||||
|
|
||||||
if [[ $# == 1 || "$2" == "hup" ]]; then
|
if [[ $# == 1 || "$2" == "hup" ]]; then
|
||||||
|
@ -67,6 +68,8 @@ _zsh_autosuggest_async_response() {
|
||||||
|
|
||||||
# Close the fd
|
# Close the fd
|
||||||
exec {1}<&-
|
exec {1}<&-
|
||||||
|
_ZSH_AUTOSUGGEST_ASYNC_FD=
|
||||||
|
_ZSH_AUTOSUGGEST_ASYNC_PID=
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Always remove the handler
|
# Always remove the handler
|
||||||
|
|
|
@ -813,6 +813,7 @@ _zsh_autosuggest_async_request() {
|
||||||
_zsh_autosuggest_async_response() {
|
_zsh_autosuggest_async_response() {
|
||||||
emulate -L zsh
|
emulate -L zsh
|
||||||
|
|
||||||
|
typeset -g _ZSH_AUTOSUGGEST_ASYNC_FD _ZSH_AUTOSUGGEST_CHILD_PID
|
||||||
local suggestion
|
local suggestion
|
||||||
|
|
||||||
if [[ $# == 1 || "$2" == "hup" ]]; then
|
if [[ $# == 1 || "$2" == "hup" ]]; then
|
||||||
|
@ -822,6 +823,8 @@ _zsh_autosuggest_async_response() {
|
||||||
|
|
||||||
# Close the fd
|
# Close the fd
|
||||||
exec {1}<&-
|
exec {1}<&-
|
||||||
|
_ZSH_AUTOSUGGEST_ASYNC_FD=
|
||||||
|
_ZSH_AUTOSUGGEST_ASYNC_PID=
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Always remove the handler
|
# Always remove the handler
|
||||||
|
|
Loading…
Reference in a new issue