Repository navigation
Share one >> handle per file, and append at the end atomically - #6
Conversation
074f6ac to
1fdb7bf
Compare
|
Hello! Happy to review and potentially merge this. But first, could you please attribute your git commits to which model (if any) were used. |
df2d0b2 to
4e16e8a
Compare
|
Done: the commit now carries |
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>
4e16e8a to
338b870
Compare
hex2f
left a comment
There was a problem hiding this comment.
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.
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>
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.>>redirection now reuses the handle that another one, or a program started with one, still has open for the same file (sys::same_file, nowpub), 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, sincecmd >> 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 > fstill works.>>file now append withFILE_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
066_msys_append_concurrent.sh, with expected output from Git Bash 5.3.15 viaregen_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 testpasses: unit tests, 66/66 compat scripts andmsys_tmp.