Skip to content

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

Description

@cboos

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

uutils (main @ e7c9f31):

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.

Activity

  1. cboos commented on Oct 4, 2026

    @cboos
    ContributorAuthor

    (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 ]; do printf '%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)"; fi
        printf '  %-10s %-6s %-14s %s\n' "$shape" "$mode" "$*" "$result"
    }
    
    for impl in "$@"; do
        echo "== $impl: $("$impl" --version 2>/dev/null | head -n 1)"
        for args in -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
        for args in -n1 -n+2 -n+100 -c+1; do run many_lines file "$impl" $args; done
        run tiny file "$impl" -n+2
    done

    Output for the table above:

    == /path/to/gnu-9.12/tail: tail (GNU coreutils) 9.12
      one_line   file   -n1            ok (+8192)
      one_line   file   -n3            ok (+8192)
      one_line   file   -c10           ok (+10)
      one_line   file   -n+1           RUNAWAY (rc=153)
      one_line   file   -c+1           RUNAWAY (rc=153)
      one_line   file   -c+5           RUNAWAY (rc=153)
      one_line   file   -c100000000    RUNAWAY (rc=153)
      one_line   stdin  -n1            ok (+8192)
      one_line   stdin  -c+1           RUNAWAY (rc=153)
      many_lines file   -n1            ok (+80)
      many_lines file   -n+2           RUNAWAY (rc=153)
      many_lines file   -n+100         ok (+80)
      many_lines file   -c+1           RUNAWAY (rc=153)
      tiny       file   -n+2           ok (+4)
    == /path/to/uutils-0.12.0/tail: tail (uutils coreutils) 0.12.0
      one_line   file   -n1            RUNAWAY (rc=153)
      one_line   file   -n3            RUNAWAY (rc=153)
      one_line   file   -c10           ok (+10)
      one_line   file   -n+1           RUNAWAY (rc=153)
      one_line   file   -c+1           RUNAWAY (rc=153)
      one_line   file   -c+5           RUNAWAY (rc=153)
      one_line   file   -c100000000    RUNAWAY (rc=153)
      one_line   stdin  -n1            RUNAWAY (rc=153)
      one_line   stdin  -c+1           RUNAWAY (rc=153)
      many_lines file   -n1            RUNAWAY (rc=153)
      many_lines file   -n+2           RUNAWAY (rc=153)
      many_lines file   -n+100         RUNAWAY (rc=153)
      many_lines file   -c+1           RUNAWAY (rc=153)
      tiny       file   -n+2           ok (+8)
    
  2. xtqqczze commented on Oct 4, 2026

    @xtqqczze
    Collaborator

    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.

    cc: @collinfunk

  3. cboos commented on Oct 4, 2026

    @cboos
    ContributorAuthor

    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.

  4. cboos commented on Oct 4, 2026

    @cboos
    ContributorAuthor

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

    file how args GNU 9.12 / 9.7 uutils 0.12.0 #15063 #15066
    8192 B, 1 line f >> f -n1 ok (+8192) runaway ok (+8192) ok (+8192)
    -n3 ok (+8192) runaway ok (+8192) ok (+8192)
    -c10 ok (+10) ok (+10) ok (+10) ok (+10)
    -n+1 runaway runaway runaway ok (+8192)
    -c+1 runaway runaway runaway ok (+8192)
    -c+5 runaway runaway runaway ok (+8188)
    -c100000000 runaway runaway runaway ok (+8192)
    < f >> f -n1 ok (+8192) runaway ok (+8192) ok (+8192)
    -c+1 runaway runaway runaway ok (+8192)
    8000 B, 100 lines f >> f -n1 ok (+80) runaway ok (+80) ok (+80)
    -n+2 runaway runaway runaway ok (+7920)
    -n+100 ok (+80) runaway runaway ok (+80)
    -c+1 runaway runaway runaway ok (+8000)
    6 B, 3 lines f >> f -n+2 ok (+4) ok (+8) ok (+8) ok (+4)
    • 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.
  5. collinfunk commented on Oct 4, 2026

    @collinfunk

    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.

  6. xtqqczze commented on Oct 4, 2026

    @xtqqczze
    Collaborator

    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.

  7. collinfunk commented on Oct 5, 2026

    @collinfunk

    Might be worth a discussion with the POSIX people. You can open a report here: https://austingroupbugs.net

  8. cboos commented on Oct 5, 2026

    @cboos
    ContributorAuthor

    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:

    $ cat file.txt >> file.txt
    cat: file.txt: input file is output file
    $ cat
     --version
    cat (uutils coreutils) 0.10.0
    

    (gnucat yields the same error)

    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.

  9. cboos commented on Oct 5, 2026

    @cboos
    ContributorAuthor

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

  10. cakebaker commented on Oct 5, 2026

    @cakebaker
    Contributor

    @cboos @xtqqczze re-opened your PR. Sorry for closing it.

  11. added 2 commits that reference this issue on Oct 5, 2026
    5f380c8
    cc26f88
  12. xtqqczze commented on Oct 6, 2026

    @xtqqczze
    Collaborator

    #15063 was reverted.

  13. added a commit that references this issue on Oct 7, 2026
    3573a33
  14. added 3 commits that reference this issue on Oct 30, 2026
    198b954
    6d23bce
    cea0665
  15. added a commit that references this issue on Nov 10, 2026
    38a1e35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions