Skip to content

Read a shared handle's position without setting it - #23

Merged
hex2f merged 1 commit into
hex2f:mainfrom
ExpectedArgument:shared-handle-position
Nov 7, 2026
Merged

hex2f merged 1 commit into
hex2f:mainfrom
ExpectedArgument:shared-handle-position

Conversation

@ExpectedArgument

Copy link
Copy Markdown

066_msys_append_concurrent.sh failed 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::Append checks whether the position is behind the end of the file, in case another handle appended since (#6). It read the position with stream_position(), which on Windows is SetFilePointerEx(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_append has the same race. When gash's stdout or stderr was opened append-only (Node's 'a', see make_append_only_stdio_writable), it moved the handle to the end with SetFilePointerEx(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_behind reads the length, then the position with NtQueryInformationFile(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:

writers before after
/usr/bin/echo "job $i" >> m & (a redirection per job) 9 of 900 rounds 0 of 900
/usr/bin/echo "job $i" & in a loop with one >> m 10 of 900 0 of 900
cmd.exe /d /c "echo job $i" & with one >> m 6 of 600 0 of 600

Native writers lost lines too, so it isn't MSYS-specific. The missing line never turned up later, so wait wasn't returning early, and the file was short by exactly that line's length: another line had been written over it.

Tests

  • New compat script 074_append_while_spawning.sh, with expected output from Git Bash 5.3.15 via regen_expected.ps1. An MSYS awk writes 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.
  • New 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 test passes: unit tests, 73/73 compat scripts, msys_tmp and the new test. Clippy reports nothing in the changed code.

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 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 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.

@hex2f
hex2f merged commit bf6a77a into hex2f:main Nov 7, 2026
1 check passed
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