Skip to content

Share one >> handle per file, and append at the end atomically - #6

Merged
hex2f merged 1 commit into
hex2f:mainfrom
ExpectedArgument:fix-msys-append-heredoc-shebang
Nov 6, 2026
Merged

hex2f merged 1 commit into
hex2f:mainfrom
ExpectedArgument:fix-msys-append-heredoc-shebang

Conversation

@ExpectedArgument

@ExpectedArgument ExpectedArgument commented Nov 6, 2026 •

Copy link
Copy Markdown

A follow-up to #5 for the case it leaves open: MSYS programs appending to one file through separate >> redirections at the same time.

On main each >> redirection opens its own handle, with its own file position, so programs started from separate redirections write at the same offsets. 30 background /usr/bin/echo "job $i" >> log & jobs kept 5 to 7 lines in six runs, where Git Bash keeps all 30. With builtins and MSYS programs appending together from 20 jobs, 23 to 32 of 40 lines were left, some of them garbled.

  • One handle per file: a >> redirection now reuses the handle that another one, or a program started with one, still has open for the same file (sys::same_file, now pub), so they all share one file position. An entry stays while a redirection holds the handle or a program it was given to still runs, since cmd >> log & drops its redirection right away. After that it is closed, so nothing keeps the file open: /usr/bin/echo x >> f; rm f; echo y > f still works.
  • Atomic appends: sharing one handle made a race visible. Seeking to the end and then writing isn't atomic, so a write could land at a stale end and overwrite what another writer had just appended. The shell's own writes to a >> file now append with FILE_WRITE_TO_END_OF_FILE (sys::write_at_end), which also leaves the position at the end. Before the handle goes to a child, it moves to the end only when another handle has appended since, so it no longer jumps back over output that a program still writing through it adds meanwhile.

Still not covered: programs in different gash processes appending to the same file at the same moment.

Tests

  • New compat script 066_msys_append_concurrent.sh, with expected output from Git Bash 5.3.15 via regen_expected.ps1. It covers 30 MSYS programs appending at the same time through separate redirections and through one, builtins and MSYS programs appending together from 20 jobs, and removing and recreating the file afterwards. On main it fails in every run. With this branch it passed 10 runs out of 10.
  • cargo test passes: unit tests, 66/66 compat scripts and msys_tmp.

@ExpectedArgument ExpectedArgument changed the title Fix >> with MSYS programs, unread here-doc input and #! options Let MSYS programs write to files opened with >>, sharing one handle per file Nov 6, 2026
@ExpectedArgument
ExpectedArgument force-pushed the fix-msys-append-heredoc-shebang branch from 074f6ac to 1fdb7bf Compare November 6, 2026 18:45
@hex2f

hex2f commented Nov 6, 2026

Copy link
Copy Markdown
Owner

Hello! Happy to review and potentially merge this. But first, could you please attribute your git commits to which model (if any) were used.

@ExpectedArgument
ExpectedArgument force-pushed the fix-msys-append-heredoc-shebang branch 2 times, most recently from df2d0b2 to 4e16e8a Compare November 6, 2026 19:25
@ExpectedArgument ExpectedArgument changed the title Let MSYS programs write to files opened with >>, sharing one handle per file Share one >> handle per file, and append at the end atomically Nov 6, 2026
@ExpectedArgument

Copy link
Copy Markdown
Author

Done: the commit now carries Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>. Claude Opus 5.5 was used for all of my PRs here, and I'm adding the same trailer to the others (#9, #10, #11, #12, #13, #15). I've also rebased this onto main now that #5 is in. What's left is the case #5 doesn't cover, separate >> redirections appending at the same time; the description has the details and numbers. I'm closing #8, since #14 covers it.

Each >> redirection opened its own handle, with its own file position,
so MSYS programs appending to one file through separate redirections at
the same time (cmd >> log & in a loop) wrote at the same offsets and
overwrote each other: 30 background /usr/bin/echo jobs kept 5 to 7
lines in six runs. A >> redirection now reuses the handle that another one, or a
program started with one, still has open for the same file, so they
all share one file position.

Sharing one handle made a race visible: seeking to the end and then
writing isn't atomic, so a write could land at a stale end and
overwrite what another writer appended in between. The shell's own
writes now append with FILE_WRITE_TO_END_OF_FILE, which also leaves
the position at the end, and a handle moves to the end before going to
a child only when another handle has appended since.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ExpectedArgument
ExpectedArgument force-pushed the fix-msys-append-heredoc-shebang branch from 4e16e8a to 338b870 Compare November 6, 2026 19:37

@hex2f hex2f left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, and for the rebase. I checked whether main already covers it. Main's fix from #5 handles many writers through one >> redirection. Separate redirections to the same file (cmd >> log & in a loop) still lose lines on main, so this is still needed.

The rebased branch passes cargo test: all 66 compat scripts, including the new 066_msys_append_concurrent.

PID reuse (minor): children stores bare PIDs and checks them with pid_alive. If a child exits and Windows gives its PID to an unrelated process, the entry stays, and so does the open file, until that process exits. Then rm log would fail for a while. Keeping a process handle instead of the PID would avoid this. It's rare, so not blocking.

Looks good to merge.

@hex2f
hex2f merged commit d814d82 into hex2f:main Nov 6, 2026
1 check passed
hex2f pushed a commit that referenced this pull request Nov 6, 2026
AppendFile kept the process ids of programs started with a shared >> handle and checked them with pid_alive. If one exited and Windows gave its id to an unrelated process, the entry, and the open file, stayed until that process exited, so rm log could fail for a while. Keep a handle to each process instead and check whether it has exited, as suggested in the review of #6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants