Skip to content

tail: stop at the original end when output is appended to the input - #15066

Merged
sylvestre merged 2 commits into
uutils:mainfrom
cboos:tail-self-append-15057
Nov 10, 2026
Merged

sylvestre merged 2 commits into
uutils:mainfrom
cboos:tail-self-append-15057

Conversation

@cboos

@cboos cboos commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

When tail's output is appended to the file it reads (tail -n1 f >> f), tail reads up to the end of file, but its own output keeps moving that end. On a file larger than one block it copies itself until the filesystem is full. On a smaller file it reads part of its own output back (tail -n+2 g >> g on a\nb\nc\n appends b\nc\nb\nc\n).

This change makes tail copy a regular file only up to the size it had when tail opened it, in every mode and on both the seeking and the streaming path. A reported size of 0 is treated as unknown, since files in /proc report 0 whatever they contain. Follow mode is unaffected: it continues from that point.

The regression test appends tail's output to its own input under an RLIMIT_FSIZE cap, so if the fix regresses the test fails instead of filling the disk.

This is an alternative to #15063, which bounds only -n N (counted from the end) on files larger than one block. In the modes that count from the start of the file, GNU also runs away, and this patch stops there as well. That makes it stricter than GNU in those modes; the comparison table is in the issue.

Closes #15057

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/date/resolution (fails in this run but passes in the 'main' branch)

@cakebaker

Copy link
Copy Markdown
Contributor

Closing this PR as #15063 already fixed the same issue. Anyway, thanks for your PR!

@cakebaker cakebaker closed this Oct 5, 2026
@xtqqczze

xtqqczze commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

#15057 (comment)

@xtqqczze xtqqczze reopened this Oct 5, 2026
@xtqqczze

xtqqczze commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

@cboos please convert to draft

@cboos
cboos marked this pull request as draft October 5, 2026 11:59
@cboos
cboos force-pushed the tail-self-append-15057 branch from 549584e to 14cc64d Compare October 5, 2026 11:59
@xtqqczze xtqqczze self-assigned this Oct 5, 2026
@cboos

cboos commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor Author

The macOS fix is available, but I'll wait until the end of the current CI cycle before pushing it.

@codspeed

codspeed Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will regress 2 benchmarks

⚡ 6 improved benchmarks
❌ 2 regressed benchmarks
✅ 389 untouched benchmarks
⏩ 54 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Simulation three_39_bit_primes 377.6 ms 748.3 ms -49.54%
❌ Simulation five_38_bit_primes 1.8 s 2.1 s -14.38%
⚡ Simulation rm_recursive_tree 21.3 ms 13.7 ms +55.79%
⚡ Simulation du_summarize_balanced_tree[(5, 4, 10)] 14.7 ms 11.5 ms +28.58%
⚡ Memory du_deep_tree[(100, 3)] 93 KB 76.5 KB +21.61%
⚡ Simulation du_wide_tree[(5000, 500)] 18.2 ms 15.4 ms +18.35%
⚡ Simulation du_deep_tree[(100, 3)] 2.2 ms 1.9 ms +12.07%
⚡ Simulation du_all_wide_tree[(5000, 500)] 37.5 ms 33.8 ms +11%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing cboos:tail-self-append-15057 (799d2da) with main (3f92f51)2

Open in CodSpeed

Footnotes

  1. 54 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on main (95eb7e1) during the generation of this report, so 3f92f51 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@cboos

cboos commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

Well, I don't see how that PR affects uu_factor performance, but otherwise I think I did my best to get a green build!

The macOS failure seems to be a known intermittent one (#14888).

One more nit: I wasn't sure about the dup() on fd 0; it turns out it's there because the File we'd need to create on it for is_stdout(&File) would close it after use.

A version without try_clone_to_owned() is possible, and it's in the new version of the branch (needed after the #15063 revert).

@cboos
cboos force-pushed the tail-self-append-15057 branch from 799d2da to 8a16c77 Compare October 6, 2026 06:56
@xtqqczze

xtqqczze commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

factor benchmarks have high variance, tracked by #14921.

Comment thread tests/by-util/test_tail.rs Outdated
/// when tail behaves: a coverage build writes its profile data on exit, past
/// any small cap.
#[cfg(unix)]
struct SigxfszGuard(libc::sighandler_t);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Since #15131, we have ignore_sigxfsz on UCommand, can you use that?

@cboos
cboos force-pushed the tail-self-append-15057 branch from 8a16c77 to 2f6705d Compare October 7, 2026 12:05
@cboos
cboos marked this pull request as ready for review October 7, 2026 19:02
@cboos
cboos marked this pull request as draft October 7, 2026 19:37
@cboos

cboos commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

On second thought, I realized there were no tests for Windows yet...

Comment thread tests/by-util/test_tail.rs Resolved
@cboos
cboos force-pushed the tail-self-append-15057 branch from 2f6705d to 265674f Compare October 7, 2026 22:05
@cboos
cboos marked this pull request as ready for review October 8, 2026 13:28
@cboos
cboos force-pushed the tail-self-append-15057 branch from 265674f to 220ad89 Compare October 30, 2026 18:36
@cboos

cboos commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

(Claude) Rebased onto current main. Main had meanwhile moved UCommand::limit from rustix's Resource to rlimit::Resource, so the tests were adapted in each commit.

The last commit (220ad89) fixes one more corner case of the same issue: with a header (tail -v -n1 f >> f, or several files), the header is appended to f before tail counts from the end, so the count started past the original end and only the header was printed. Counting from the end now starts at the original end of the file, and on macOS and Windows the size of standard input is taken before the header is printed. Two new test cases (-v -n1 and -v -c5 on a file above one block) cover it, and the standard input test now uses -v.

@cboos

cboos commented Nov 1, 2026

Copy link
Copy Markdown
Contributor Author

The branch update was needed because Vancouver/America switched time zones to MST today; the failing test test_date_tz_with_relative_time was fixed by 7c9d4e7.

@xtqqczze

xtqqczze commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

Please update the branch history as follows:

  • Drop the merge commit and rebase for a clean history.
  • Squash commits into a smaller set of logical commits that reflect distinct changes.
  • Remove the Co-Authored-By trailer from commits, as authorship should only be attributed to human contributors.

@cboos
cboos force-pushed the tail-self-append-15057 branch from 76224c7 to 187f0fa Compare November 5, 2026 00:52
@cboos

cboos commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

Hello @xtqqczze,

Is there anything I can do about this failing job? Manpage Validation / Validate manpages with mandoc (fr_FR.UTF-8) (pull_request). It's been retried 3 times already (not by me). Would one more rebase help?

More radically: do you think it was the wrong approach to try to make this tail-on-itself work in some sensible way? Maybe a simpler approach would have been to just bail out. The code will certainly be simpler that way, and there's precedent (cat does that). To recap, my initial main concern and why I jumped in with a fix was mostly to avoid others being bitten by the current "let's happily fill your disk" pathological behavior. A blunt refusal of a tail f >> f would do that too (à la cat: f: input file is output file).

Comment thread tests/by-util/test_tail.rs Outdated
// Restore in a single write. On Windows, copying sets the final size
// before writing the data, so tail could read zeros there and then wait
// for more data after them.
at.write(source, &at.read(backup));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

unrelated to this fix, could you please move it into a separate pr?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. See #16316 for the separate mini-PR.

hubot pushed a commit to coreutils/coreutils that referenced this pull request Nov 9, 2026
* tests/tail/append-to-input.sh: New test.
* tests/local.mk (all_tests): Reference it.
uutils/coreutils#15066

Link: #455
cboos added 2 commits November 9, 2026 21:50
With `tail f >> f`, tail read on to the end of file while its own output
kept moving that end, so for files above one block it copied itself until
the filesystem was full. Smaller files read part of their own output back.

When standard output is the file being read, recognised by device and
inode as cat does, copy the file only up to the size it had when tail
opened it, on both the seeking and the streaming path. Counting from the
end also starts at that size, since a header (`tail -v`) may already
have been appended beyond it. A file that another process appends to is
still read to its end, as before.

The tests append tail's output to its own input and stop tail once the
file grows past 1 MiB: with a file size cap on unix, where SIGXFSZ is
ignored so that a coverage build can still write its profile data, and
by watching the size of the file on Windows.

Fixes uutils#15057
On macOS and Windows, standard input is not resolved to a path, so
`tail < f >> f` does not go through tail_file and its bound: it is read
like a pipe, until the end of a file that our own output keeps moving.
Apply the same bound there, from the descriptor itself on unix and from
a duplicate of the handle on Windows, which shares its file position.
Its size is taken before the header is printed, which would otherwise
be copied back once.
@cboos
cboos force-pushed the tail-self-append-15057 branch from 187f0fa to 468a474 Compare November 9, 2026 22:07
@sylvestre
sylvestre merged commit d661a0f into uutils:main Nov 10, 2026
141 of 142 checks passed
@sylvestre

Copy link
Copy Markdown
Contributor

Thanks for your PR

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.

tail: tail -n1 FILE >> FILE never terminates when FILE is larger than 4096 bytes

4 participants