Repository navigation
Read a shared handle's position without setting it - #23
Conversation
Before a program is started with a `>>` handle, gash checks whether the handle is behind the end of the file. It read the position with `stream_position`, which is `SetFilePointerEx(h, 0, FILE_CURRENT)`: that reads the position and then sets it to what it read. When a program started earlier with the handle wrote in between, the position went back over that write, and the next write landed on top of it. `seek_std_append`, for a stdout or stderr that was append-only, had the same race: before every builtin write and every child it moved the handle to the end with `SetFilePointerEx(FILE_END)`, which reads the length and then sets the position. The position is now read with `NtQueryInformationFile`, which doesn't set it, and both places move the handle only when it is behind the end (`sys::seek_end_if_behind`). Writes through the handle only move it forward, so while every writer uses that one handle it is left alone. This was the occasional failure of 066_msys_append_concurrent.sh. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hex2f
left a comment
There was a problem hiding this comment.
Thanks for this, and for tracking down the cause. I reproduced it: with main's code the new 074 test lost between 16 and 49 of the 100,000 lines in each of 5 runs, and one run also lost 2 of the 40 job lines. With this branch, all 5 runs were complete. cargo test passes, and so does the compat suite merged with current main and with #22 and #25.
#21 changed how these programs are tracked, but it still read the position with stream_position(), so main doesn't cover this.
When stdout is opened append-only, the handle that make_append_only_stdio_writable swaps in is reopened with only FILE_GENERIC_WRITE. I checked that seek_end_if_behind still works on it. I started gash from Node with 'a' and had another handle append between builtin writes and MSYS writes. Every line still landed at the end, as on main.
Not for this PR: write_std still seeks and then writes for an append-only stdout, so a write from a different handle between the seek and the write can still be overwritten. Using write_at_end there, as Handle::Append does, would close that gap.
066_msys_append_concurrent.shfailed now and then: about one run in 40 lost a line, and more often under load. The cause is how gash reads a file handle's position while programs are writing through it.Before a program is started with a
>>handle,Handle::Appendchecks whether the position is behind the end of the file, in case another handle appended since (#6). It read the position withstream_position(), which on Windows isSetFilePointerEx(h, 0, FILE_CURRENT). That is two calls: it reads the position and then sets it to what it read. If a program started earlier with the same handle writes in between, setting the position moves it back over that write, and the next write through the handle lands on top of it.sys::seek_std_appendhas the same race. When gash's stdout or stderr was opened append-only (Node's'a', seemake_append_only_stdio_writable), it moved the handle to the end withSetFilePointerEx(h, 0, FILE_END)before every builtin write and before every child that inherits it. That reads the length and then sets the position, so it can also undo a write that a background job makes in between.The new
sys::seek_end_if_behindreads the length, then the position withNtQueryInformationFile(FilePositionInformation), which doesn't set it, and moves the handle to the end only when the position is behind. Writes through the handle only move the position forward, so while every writer uses that one handle it is never behind and is left alone. Both places use it.How I found it
A script that runs 30 background jobs per round, as 066 does, lost a whole line in about 1% of rounds with three copies running at once:
/usr/bin/echo "job $i" >> m &(a redirection per job)/usr/bin/echo "job $i" &in a loop with one>> mcmd.exe /d /c "echo job $i" &with one>> mNative writers lost lines too, so it isn't MSYS-specific. The missing line never turned up later, so
waitwasn't returning early, and the file was short by exactly that line's length: another line had been written over it.Tests
074_append_while_spawning.sh, with expected output from Git Bash 5.3.15 viaregen_expected.ps1. An MSYSawkwrites 100,000 lines through a>>handle, flushing each one, while 40 MSYS programs are started with the same handle. Without the change it lost 7 to 37 lines in each of 10 runs. With it, no run out of 20 lost any.tests/append_only_stdout.rs: gash with stdout and stderr opened append-only, as Node opens them, and the same awk writer together with 40 background programs and 200 builtin lines. Without the change it lost lines in 5 runs out of 5. With it, it passed 10 out of 10. It's skipped when Git's awk isn't installed.cargo testpasses: unit tests, 73/73 compat scripts,msys_tmpand the new test. Clippy reports nothing in the changed code.