b43b84abc77850a3734c127c38afdd7cf7739dc6

Author
Loïc Yhuel <loic.yhuel@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

fix(async): avoid blocking the shell while waiting (#12304)

Co-authored-by: Marc Cornellà <marc@mcornella.com>

Diff

 1diff --git a/lib/async_prompt.zsh b/lib/async_prompt.zsh
 2index 384e49d33e9c2626b1b65df614e8ac14051cfbf5..ac95bcd73c031525df9d456e2adbf6717ad92eb1 100644
 3--- a/lib/async_prompt.zsh
 4+++ b/lib/async_prompt.zsh
 5@@ -82,10 +82,8 @@ function _omz_async_request {
 6     exec {fd}< <(
 7       # Tell parent process our PID
 8       builtin echo ${sysparams[pid]}
 9-      # Store handler name for callback
10-      builtin echo $handler
11       # Set exit code for the handler if used
12-      (exit $ret)
13+      () { return $ret }
14       # Run the async function handler
15       $handler
16     )
17@@ -98,8 +96,7 @@ function _omz_async_request {
18     command true
19 
20     # Save the PID from the handler child process
21-    read pid <&$fd
22-    _OMZ_ASYNC_PIDS[$handler]=$pid
23+    read -u $fd "_OMZ_ASYNC_PIDS[$handler]"
24 
25     # When the fd is readable, call the response handler
26     zle -F "$fd" _omz_async_callback
27@@ -114,15 +111,14 @@ function _omz_async_callback() {
28   local err=$2  # Second arg will be passed in case of error
29 
30   if [[ -z "$err" || "$err" == "hup" ]]; then
31-    # Get handler name from first line
32-    local handler
33-    read handler <&$fd
34+    # Get handler name from fd
35+    local handler="${(k)_OMZ_ASYNC_FDS[(r)$fd]}"
36 
37     # Store old output which is supposed to be already printed
38     local old_output="${_OMZ_ASYNC_OUTPUT[$handler]}"
39 
40     # Read output from fd
41-    _OMZ_ASYNC_OUTPUT[$handler]="$(cat <&$fd)"
42+    IFS= read -r -u $fd -d '' "_OMZ_ASYNC_OUTPUT[$handler]"
43 
44     # Repaint prompt if output has changed
45     if [[ "$old_output" != "${_OMZ_ASYNC_OUTPUT[$handler]}" ]]; then