Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for zsh/zpty fd message exchange #45

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Prev Previous commit
Documentation and comments
  • Loading branch information
mafredri committed Jan 6, 2023

Verified

This commit was signed with the committer’s verified signature.
ElDavoo Davide Palma
commit a082cba8489bad470b9c586df294676587b861bb
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -170,6 +170,7 @@ ok ./async_test.zsh 0.070s

## Limitations

* Processing a lot of output (>10240 bytes) can be slow due to the default value of `ASYNC_MAX_BUFFER_SIZE` (default `1020`). The low default value is a safety precaution, raising it is unlikely to cause problems on e.g. Linux, but caution is warranted.
* A NULL-character (`$'\0'`) is used by `async_job` to signify the end of the command, it is recommended not to pass them as arguments, although they should work when passing multiple arguments to `async_job` (because of quoting).
* Tell me? :)

12 changes: 7 additions & 5 deletions async.zsh
Original file line number Diff line number Diff line change
@@ -15,7 +15,6 @@ typeset -g ASYNC_DEBUG=${ASYNC_DEBUG:-0}
typeset -g ASYNC_DEBUG_WORKER_STDERR=${ASYNC_DEBUG_WORKER_STDERR:-/dev/null}

# The maximum buffer size when outputting to zpty.
# Note: Subtract 4 to accommodate "\r\n" times two.
#
# When processing large amounts of data, the limit of 1024 bytes is
# slow. If you're going to output a lot more than that, consider
@@ -24,6 +23,8 @@ typeset -g ASYNC_DEBUG_WORKER_STDERR=${ASYNC_DEBUG_WORKER_STDERR:-/dev/null}
# This value was chosen as a safe limit for macOS and other systems that
# have a low limit (1024) for the buffer, on Linux this can likely be
# raised significantly.
#
# Note: Subtract 4 to accommodate "\r\n" times two.
typeset -g ASYNC_MAX_BUFFER_SIZE=${ASYNC_MAX_BUFFER_SIZE:-$((1024 - 4))}

# Execute commands that can manipulate the environment inside the async worker. Return output via callback.
@@ -76,10 +77,11 @@ _async_job() {

# Chunk up the output so as to not fill up the entire fd.
for ((i = 1; i < $#out; i += ASYNC_MAX_BUFFER_SIZE)); do
# Note: We are surrounding the message in newlines here in an
# attempt to force zpty to behave. Literal newlines will be
# filtered by async_process_results. Any newlines in the job
# output will survive, as they are quoted.
# Note: We are surrounding the (potentially partial) message in newlines
# here in an attempt to flush the file descriptor and prevent behavior
# that could cause zpty to hang. Literal newlines will be filtered by
# async_process_results. Any newlines in the job output will survive, as
# they are quoted.
#
# Return output (<job_name> <return_code> <stdout> <duration> <stderr>).
if ! print -r -n - $'\n'"${out[$i,$((i + ASYNC_MAX_BUFFER_SIZE - 1))]}"$'\n'; then