Skip to content

Commit 93bc795

Browse files
fix(zsh): streaming placeholder not updated when async is enabled (#7443)
* fix: streaming placeholder not updated when async is enabled When both async and streaming are configured on ZSH, the init command becomes 'precmd() { source <script> }', which re-sources the script on every prompt cycle. This unconditionally reset _omp_stream_fd=-1, causing _omp_cleanup_stream to skip closing the previous cycle's stream fd. The stale stream's EOF handler would later call _omp_cleanup_stream using the global _omp_stream_fd (now pointing to the new stream's fd) and kill the new stream before it could send any updates. The placeholder never got replaced. Three fixes: 1. init.go: Skip async sourcing when streaming is also active. Streaming provides its own async update mechanism; re-sourcing on every precmd is not needed and conflicts with the streaming fd lifecycle. 2. omp.zsh (_omp_stream_fd init): Use \ so a re-source does not clobber an in-flight stream fd. 3. omp.zsh (_omp_async_handler): On EOF, close only the specific fd that triggered the handler instead of calling _omp_cleanup_stream. This prevents any stale handler from closing the current stream's fd even if the global variable has diverged. Fixes #7313 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix(zsh): streaming placeholder not updated when async is enabled When both async and streaming are configured in ZSH, the omp.zsh script is re-sourced before every prompt cycle via the async init mechanism (precmd() { source <script> }). Top-level variable assignments in the re-sourced script run unconditionally, so _omp_stream_fd was reset to -1 on every prompt cycle even while a live stream was in flight. This caused two problems: 1. _omp_cleanup_stream skipped cleanup (fd -1 < 0) before starting the new stream, leaking the old fd and its ZLE handler. 2. The stale EOF handler later fired, saw _omp_stream_fd pointing at the NEW stream, and closed it so the placeholder was never replaced. Fix 1: use _omp_stream_fd=\ (ZSH := modifier) so the fd value survives a re-source, initialising to -1 only when the variable is genuinely unset (first source at shell startup). Fix 2: in _omp_async_handler, guard the EOF cleanup with [[ \ -eq \ ]] so a stale handler from a previous prompt cycle can never close the current stream's fd. Reverts the init.go change from the previous commit changing the async sourcing mechanism is unnecessary and caused a regression (exit code 127) when streaming was enabled. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix(zsh): correct misleading comment on stream fd initialization The comment referenced := (ZSH assign-if-unset) but the code uses :- (use-default-if-unset). Update the comment to accurately describe what the expansion does. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent dba37ed commit 93bc795

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/shell/scripts/omp.zsh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ _omp_cursor_positioning=0
1717
_omp_ftcs_marks=0
1818

1919
# streaming support variables
20-
_omp_stream_fd=-1
20+
# Preserve the current fd value if the script is re-sourced mid-session; default to -1 when unset or empty.
21+
_omp_stream_fd=${_omp_stream_fd:--1}
2122
_omp_enable_streaming=0
2223
_omp_primary_prompt=""
2324
_omp_streaming_supported=""
@@ -110,8 +111,9 @@ function _omp_async_handler() {
110111
IFS= read -r -u $fd -d $'\0' _omp_primary_prompt
111112
if [[ $? -ne 0 ]]; then
112113
if [[ -z "$_omp_primary_prompt" ]]; then
113-
# EOF — stream closed normally
114-
_omp_cleanup_stream
114+
# EOF — only clean up if this is still the active stream fd.
115+
# A stale handler from a previous prompt cycle must not close the current stream.
116+
[[ $_omp_stream_fd -eq $fd ]] && _omp_cleanup_stream
115117
return 0
116118
fi
117119
fi

0 commit comments

Comments
 (0)