Skip to content

Commit

Permalink
Fix async_start_worker when noclobber is set
Browse files Browse the repository at this point in the history
This commit fixes a clobbering issue of errfd because it was initialized
as an integer (and thus zero value). Instead of always allowing
clobbering, we initialize it as -1 which makes zsh happy and protects us
from accidentally clobbering something in the future.

The error fixed was:

	can't clobber parameter errfd containing file descriptor 0

Possible fix for clobbering issue mentioned in #35.
  • Loading branch information
mafredri committed Sep 19, 2020
1 parent 490167c commit c7f35ec
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions async.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ async_flush_jobs() {
# -p pid to notify (defaults to current pid)
#
async_start_worker() {
setopt localoptions noshwordsplit
setopt localoptions noshwordsplit noclobber

local worker=$1; shift
local -a args
Expand All @@ -542,13 +542,6 @@ async_start_worker() {
typeset -h REPLY
typeset has_xtrace=0

# Make sure async worker is started without xtrace
# (the trace output interferes with the worker).
[[ -o xtrace ]] && {
has_xtrace=1
unsetopt xtrace
}

if [[ -o interactive ]] && [[ -o zle ]]; then
# Inform the worker to ignore the notify flag and that we're
# using a ZLE watcher instead.
Expand All @@ -567,17 +560,27 @@ async_start_worker() {
# reassigned to /dev/null by the reassignment done inside the async
# worker.
# See https://github.com/mafredri/zsh-async/issues/35.
integer errfd
integer errfd=-1
exec {errfd}>&2
zpty -b $worker _async_worker -p $$ $args 2>&$errfd || {
exec {errfd}>& -
async_stop_worker $worker
return 1

# Make sure async worker is started without xtrace
# (the trace output interferes with the worker).
[[ -o xtrace ]] && {
has_xtrace=1
unsetopt xtrace
}
exec {errfd}>& -

zpty -b $worker _async_worker -p $$ $args 2>&$errfd
local ret=$?

# Re-enable it if it was enabled, for debugging.
(( has_xtrace )) && setopt xtrace
exec {errfd}>& -

if (( ret )); then
async_stop_worker $worker
return 1
fi

if ! is-at-least 5.0.8; then
# For ZSH versions older than 5.0.8 we delay a bit to give
Expand Down

0 comments on commit c7f35ec

Please sign in to comment.