You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
tail: tail -n1 FILE >> FILE never terminates when FILE is larger than 4096 bytes #15057
When tail's output is appended to the very file it reads, uutils tail does not terminate once the file is larger than 4096 bytes: it keeps reading back the bytes it has just written, and the file grows until the filesystem (or a quota) is full. GNU tail appends the requested last line(s) once and exits.
I hit this with tail -1 f >> f, meant to duplicate the last line of a log. On an 80 KB file it produced a 250 GB file before it was noticed.
Reproduction
Capped with ulimit -f so that it cannot fill the disk:
export LANG=C
head -c 4096 /dev/zero | tr '\0''x'> f;echo>> f # one line, 4097 bytes
( ulimit -f 1024; timeout 5 tail -n1 f >> f );echo"exit=$?"; stat -c '%s bytes' f
File size limit exceeded (core dumped) timeout 5 tail -n1 f >> f
exit=153
1048576 bytes
GNU coreutils 9.12:
exit=0
8194 bytes
Observations
The trigger is the file size, not the line length. Bisected with tail -n1 f >> f: one line of 4096 bytes is fine and one of 4097 runs away; 200 lines of 20 bytes (4000 bytes) are fine and 200 lines of 21 bytes (4200 bytes) run away. Files of 4096 bytes or less always behave.
A related over-read shows up even on a tiny file, with -n +N:
printf'a\nb\nc\n'> g; tail -n+2 g >> g; od -c g
uutils appends b\nc\nb\nc\n (8 bytes, a b c b c b c), GNU appends b\nc\n (4 bytes, a b c b c). This one terminates, but it already reads past the end the file had when tail started.
Versions
uutils coreutils: 0.10.0 (Ubuntu's rust-coreutils package), 0.12.0 (release binary coreutils-0.12.0-x86_64-unknown-linux-gnu), and main @ e7c9f31 built with cargo build --release -p uu_tail (reports 0.13.0). All three behave identically.
GNU coreutils: 9.12 (the reference tarball from the Canonical PPA linked in CONTRIBUTING.md) and 9.7 (Ubuntu's gnutail). Both append exactly the requested lines in every case above.
Platform: Ubuntu 26.04.1 LTS, x86_64. Reproduced on tmpfs; the original runaway was on ext4.
Investigated and reported with the help of an AI assistant (Claude); every output above comes from an actual run.
(Claude) A correction to the comparison in the report: GNU tail is not clean either. In the modes that count from the start of the file, GNU 9.12 also runs away when its output is appended to its input, and 9.7 gives identical results. GNU stops when it counts from the end (-n N, -c N), or when little is left to print.
Results from the script below, LANG=C. Each run is capped at 1 MiB of growth and 5 s. runaway means it was killed at the cap (SIGXFSZ, exit 153), and ok (+N) means it exited after appending N bytes.
file
how
args
GNU 9.12
uutils 0.12.0
8192 B, 1 line
f >> f
-n1
ok (+8192)
runaway
8192 B, 1 line
f >> f
-n3
ok (+8192)
runaway
8192 B, 1 line
f >> f
-c10
ok (+10)
ok (+10)
8192 B, 1 line
f >> f
-n+1
runaway
runaway
8192 B, 1 line
f >> f
-c+1
runaway
runaway
8192 B, 1 line
f >> f
-c+5
runaway
runaway
8192 B, 1 line
f >> f
-c100000000
runaway
runaway
8192 B, 1 line
< f >> f
-n1
ok (+8192)
runaway
8192 B, 1 line
< f >> f
-c+1
runaway
runaway
8000 B, 100 lines
f >> f
-n1
ok (+80)
runaway
8000 B, 100 lines
f >> f
-n+2
runaway
runaway
8000 B, 100 lines
f >> f
-n+100
ok (+80)
runaway
8000 B, 100 lines
f >> f
-c+1
runaway
runaway
6 B, 3 lines
f >> f
-n+2
ok (+4)
ok (+8)
So the report's point holds for the count-from-the-end modes, which include the common tail -n1 f >> f: GNU appends the requested lines and exits, while uutils loops on any file over 4096 bytes (-c N from the end is the exception: it terminates in both). For the from-the-start modes on files larger than a block, both implementations loop. Strict GNU compatibility therefore doesn't settle what correct behaviour is there. Stopping at the end the file had when tail started would be stricter than GNU in those modes, and that seems like a decision for the maintainers.
Reproduction script (POSIX sh, capped, cleans up after itself)
Run as sh tail-self-append.sh [TAIL...], e.g. sh tail-self-append.sh ./target/release/tail /path/to/gnu/tail. Tested with dash and bash.
#!/bin/sh# Does `tail ARGS f >> f` terminate? (uutils/coreutils#15057)## Usage: sh tail-self-append.sh [TAIL...] (default: tail)# e.g. sh tail-self-append.sh ./target/release/tail /path/to/gnu/tail## Every run is capped, so this cannot fill a disk: the output file may grow by# at most 1 MiB (ulimit -f; the writer is killed by SIGXFSZ, exit 153), and# each run gets 5 seconds. Everything happens in a temporary directory that is# removed on exit.## Prints one line per case: "ok (+N)", where N is the number of bytes# appended, or "RUNAWAY" when the cap or the timeout stopped it.set -u
LANG=C
export LANG
[ $#-eq 0 ] &&set -- tail
dir=$(mktemp -d)||exit 1
trap'rm -rf "$dir"' EXIT INT TERM
cd"$dir"||exit 1
# One line of 8192 bytes (8191 'x' + newline).one_line() { head -c 8191 /dev/zero | tr '\0''x'> f;echo>> f; }
# 100 lines of 80 bytes = 8000 bytes.many_lines() {
i=0;:> f
while [ $i-lt 100 ];doprintf'%079d\n'$i>> f; i=$((i +1));done
}
# 6 bytes: a, b, c.tiny() { printf'a\nb\nc\n'> f; }
run() { # run SHAPE MODE TAIL ARGS... (MODE: file | stdin)
shape=$1 mode=$2 impl=$3;shift 3
$shape
before=$(wc -c < f)if [ "$mode"= stdin ];then
( ulimit -f 1024; timeout 5 "$impl""$@"< f >> f;exit$? ) 2>/dev/null
else
( ulimit -f 1024; timeout 5 "$impl""$@" f >> f;exit$? ) 2>/dev/null
fi
rc=$?
after=$(wc -c < f)if [ $rc-eq 0 ];then result="ok (+$((after - before)))";else result="RUNAWAY (rc=$rc)";fiprintf' %-10s %-6s %-14s %s\n'"$shape""$mode""$*""$result"
}
forimplin"$@";doecho"== $impl: $("$impl" --version 2>/dev/null | head -n 1)"forargsin -n1 -n3 -c10 -n+1 -c+1 -c+5 -c100000000;do run one_line file "$impl"$args;done
run one_line stdin "$impl" -n1
run one_line stdin "$impl" -c+1
forargsin -n1 -n+2 -n+100 -c+1;do run many_lines file "$impl"$args;done
run tiny file "$impl" -n+2
done
tail -n1 FILE >> FILE is a valid POSIX command line, but POSIX does not specify the behaviour when tail reads from a file that is simultaneously used as its output. Therefore, this construct should not be relied upon in portable scripts.
To duplicate the last line of a log, use a temporary variable instead:
line=$(tail -n1 FILE);printf'%s\n'"$line">> FILE
That said, uutils probably should not implement an infinite self-consuming loop here. The POSIX specification for tail does not require such behaviour.
Yes, even if nothing is specified, I think it's a good idea to not let yourself shoot in the foot!
Since posting the issue, I've been studying a fix (sorry, I'm not yet that familiar with Rust, so it took some time to "digest" the solution Claude proposed), and it seems it's a bit more complete than #15063, so I'll propose it as well.
(Claude) Results for the two proposed fixes, #15063 (at b62afbb) and #15066 (at 26e36fe), using the reproduction script from the earlier comment: LANG=C, each run capped at 1 MiB of growth and 5 s. GNU 9.12 and 9.7 give identical results.
tail: stop reading past the original end of file #15063 fixes the common case (-n N counted from the end). It still differs from GNU in two rows: -n+100 on the 100-line file runs away, where GNU stops after the 80 remaining bytes, and -n+2 on the 6-byte file appends 8 bytes, where GNU appends 4. Keeping "read until EOF" in the +N modes therefore doesn't reproduce GNU: GNU stops in +N modes when little is left to print.
tail: stop at the original end when output is appended to the input #15066 matches GNU in every row where GNU terminates. In the rows where GNU runs away it stops at the size the file had when tail opened it, so it is stricter than GNU there. Since POSIX leaves this case unspecified, that seemed the safer choice, but it's the maintainers' call.
These are mostly internal implementation details; it seems to me.
Basically, the cases where it doesn't terminate are when tail doesn't need to perform another read because it reaches EOF before anything is appended to the file. Fixing it in the other cases would require you to stop reading before EOF, which seems wrong since tail should be reading until reaching EOF.
Notably, POSIX cat states: "If the standard output is a regular file, and is the same file as any of the input file operands, the implementation may treat this as an error." However, tail does not contain an equivalent provision.
Maybe this whole "runaway protection" could be conditioned on the specific case of stdout being redirected to the file we're reading from? Seems doable, as cat f >> f already catches it:
For tail, although it's an exotic use case, I think having it work like in #15066 iff FileInformation match would be possible, but even erroring out would be preferable to filling the disk...
In case anyone wonders how I stumbled onto this:
(me) Ugh... "tail f >> f" - you really have to write it down, to not do that? Why would you do that in the first place? Hm, well, I can see why one could imagine that this would append to the file in place... Actually, now we're on Ubuntu 26.04.1 since a few days, and tail is the coreutils rust thing. Maybe that behavior is a "bug" in that? What would have happened with gnutail, the GNU coreutils equivalent? If that "works", maybe it's worth a bug report for the rust coreutils...
(Claude) Fair challenge. Why I did it: I wanted to grow an agent transcript by one line to imitate a running agent. I reached for the shortest idiom, assuming tail reads the last line, writes it, and exits. That holds if tail stops at the end-of-file position it saw when it started, and fails if it copies until EOF, because its own output keeps pushing EOF back.
OKay, I was still working a bit on #15066, fixing the CI errors and making the behavior conditional as discussed above.
PR #15063 only fixes some of the cases in the table above, and the behavior change is unconditional.
If anyone wants to revisit this, they can look at the branch in cboos:coreutils:tail-self-append-15057 (I couldn't add it to the PR because it was closed).
Activity
cboos commentedon Oct 4, 2026
(Claude) A correction to the comparison in the report: GNU
tailis not clean either. In the modes that count from the start of the file, GNU 9.12 also runs away when its output is appended to its input, and 9.7 gives identical results. GNU stops when it counts from the end (-n N,-c N), or when little is left to print.Results from the script below,
LANG=C. Each run is capped at 1 MiB of growth and 5 s. runaway means it was killed at the cap (SIGXFSZ, exit 153), andok (+N)means it exited after appending N bytes.f >> f-n1f >> f-n3f >> f-c10f >> f-n+1f >> f-c+1f >> f-c+5f >> f-c100000000< f >> f-n1< f >> f-c+1f >> f-n1f >> f-n+2f >> f-n+100f >> f-c+1f >> f-n+2So the report's point holds for the count-from-the-end modes, which include the common
tail -n1 f >> f: GNU appends the requested lines and exits, while uutils loops on any file over 4096 bytes (-c Nfrom the end is the exception: it terminates in both). For the from-the-start modes on files larger than a block, both implementations loop. Strict GNU compatibility therefore doesn't settle what correct behaviour is there. Stopping at the end the file had whentailstarted would be stricter than GNU in those modes, and that seems like a decision for the maintainers.Reproduction script (POSIX sh, capped, cleans up after itself)
Run as
sh tail-self-append.sh [TAIL...], e.g.sh tail-self-append.sh ./target/release/tail /path/to/gnu/tail. Tested with dash and bash.Output for the table above:
xtqqczze commentedon Oct 4, 2026
tail -n1 FILE >> FILEis a valid POSIX command line, but POSIX does not specify the behaviour whentailreads from a file that is simultaneously used as its output. Therefore, this construct should not be relied upon in portable scripts.To duplicate the last line of a log, use a temporary variable instead:
That said, uutils probably should not implement an infinite self-consuming loop here. The POSIX specification for
taildoes not require such behaviour.cc: @collinfunk
cboos commentedon Oct 4, 2026
Yes, even if nothing is specified, I think it's a good idea to not let yourself shoot in the foot!
Since posting the issue, I've been studying a fix (sorry, I'm not yet that familiar with Rust, so it took some time to "digest" the solution Claude proposed), and it seems it's a bit more complete than #15063, so I'll propose it as well.
cboos commentedon Oct 4, 2026
(Claude) Results for the two proposed fixes, #15063 (at b62afbb) and #15066 (at 26e36fe), using the reproduction script from the earlier comment:
LANG=C, each run capped at 1 MiB of growth and 5 s. GNU 9.12 and 9.7 give identical results.f >> f-n1-n3-c10-n+1-c+1-c+5-c100000000< f >> f-n1-c+1f >> f-n1-n+2-n+100-c+1f >> f-n+2-n Ncounted from the end). It still differs from GNU in two rows:-n+100on the 100-line file runs away, where GNU stops after the 80 remaining bytes, and-n+2on the 6-byte file appends 8 bytes, where GNU appends 4. Keeping "read until EOF" in the+Nmodes therefore doesn't reproduce GNU: GNU stops in+Nmodes when little is left to print.tailopened it, so it is stricter than GNU there. Since POSIX leaves this case unspecified, that seemed the safer choice, but it's the maintainers' call.collinfunk commentedon Oct 4, 2026
These are mostly internal implementation details; it seems to me.
Basically, the cases where it doesn't terminate are when
taildoesn't need to perform another read because it reachesEOFbefore anything is appended to the file. Fixing it in the other cases would require you to stop reading beforeEOF, which seems wrong sincetailshould be reading until reachingEOF.xtqqczze commentedon Oct 4, 2026
Notably, POSIX
catstates: "If the standard output is a regular file, and is the same file as any of the input file operands, the implementation may treat this as an error." However, taildoes not contain an equivalent provision.collinfunk commentedon Oct 5, 2026
Might be worth a discussion with the POSIX people. You can open a report here: https://austingroupbugs.net
cboos commentedon Oct 5, 2026
Maybe this whole "runaway protection" could be conditioned on the specific case of stdout being redirected to the file we're reading from? Seems doable, as
cat f >> falready catches it:(gnucat yields the same error)
For tail, although it's an exotic use case, I think having it work like in #15066 iff
FileInformationmatch would be possible, but even erroring out would be preferable to filling the disk...In case anyone wonders how I stumbled onto this:
cboos commentedon Oct 5, 2026
OKay, I was still working a bit on #15066, fixing the CI errors and making the behavior conditional as discussed above.
PR #15063 only fixes some of the cases in the table above, and the behavior change is unconditional.
If anyone wants to revisit this, they can look at the branch in cboos:coreutils:tail-self-append-15057 (I couldn't add it to the PR because it was closed).
cakebaker commentedon Oct 5, 2026
@cboos @xtqqczze re-opened your PR. Sorry for closing it.
tail: stop at the original end when output is appended to the input
tail: stop at the original end when output is appended to the input
xtqqczze commentedon Oct 6, 2026
#15063 was reverted.
tail: stop at the original end when output is appended to the input
tail: stop at the original end when output is appended to the input
tail: stop at the original end when the output is the input
tail: stop at the original end when the output is the input
tail: stop at the original end when the output is the input