fix(core): avoid deadlock on here-documents larger than the pipe buffer - #2
Merged
Merged
Conversation
`setup_open_file_with_contents` creates an anonymous pipe and writes the entire here-document / here-string payload into it before the reading command exists. That write can only complete while the payload still fits in the pipe's buffer, so any larger payload blocks forever. Linux papered over this by growing the buffer to the payload size with F_SETPIPE_SZ. Every other platform kept the default buffer -- 4 KiB on Windows -- so a here-document past that threshold hangs the shell. The Linux path had gaps of its own: the fcntl is propagated with `?`, so it also failed outright for payloads over /proc/sys/fs/pipe-max-size (1 MiB by default for unprivileged processes) and for a zero-length payload. Keep the resize as a best-effort fast path and hand oversized payloads to a helper thread instead, so the write proceeds while the reader drains. The thread ends when the payload is consumed, or earlier with a broken pipe if the read end is dropped by a command that never reads its stdin. Found running an 88k-line bash CLI under brush on Windows: every `cat <<EOF`-based help function hung. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This was referenced Aug 19, 2026
cataggar
added a commit
that referenced
this pull request
Aug 22, 2026
…er (#2) `setup_open_file_with_contents` creates an anonymous pipe and writes the entire here-document / here-string payload into it before the reading command exists. That write can only complete while the payload still fits in the pipe's buffer, so any larger payload blocks forever. Linux papered over this by growing the buffer to the payload size with F_SETPIPE_SZ. Every other platform kept the default buffer -- 4 KiB on Windows -- so a here-document past that threshold hangs the shell. The Linux path had gaps of its own: the fcntl is propagated with `?`, so it also failed outright for payloads over /proc/sys/fs/pipe-max-size (1 MiB by default for unprivileged processes) and for a zero-length payload. Keep the resize as a best-effort fast path and hand oversized payloads to a helper thread instead, so the write proceeds while the reader drains. The thread ends when the payload is consumed, or earlier with a broken pipe if the read end is dropped by a command that never reads its stdin. Found running an 88k-line bash CLI under brush on Windows: every `cat <<EOF`-based help function hung. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Found while running an 88k-line bash CLI (
kenji-cli) under brush on Windows: everycat <<EOF-based help function hung the shell.Root cause
setup_open_file_with_contentscreates an anonymous pipe and writes the entire here-document / here-string payload into it before the reading command exists. That write can only complete while the payload still fits in the pipe's buffer, so any larger payload blocks forever.Linux papered over this by growing the buffer to the payload size with
F_SETPIPE_SZ. Every other platform kept the default buffer — 4 KiB on Windows — so a here-document past that threshold hangs.Bisected to an exact threshold on Windows: OK at 4096 bytes, hangs at 4160.
The Linux path had two latent gaps of its own. The
fcntlresult is propagated with?, so it also failed outright for:/proc/sys/fs/pipe-max-size(1 MiB by default for unprivileged processes)Fix
Keep the resize as a best-effort fast path, and hand oversized payloads to a helper thread so the write proceeds while the reader drains. The thread ends when the payload is consumed, or earlier with a broken pipe if the read end is dropped by a command that never reads its stdin.
Testing
main(CRLF artifacts of a Windows checkout run under WSL), so no regressionscargo clippyandcargo fmt --checkclean