← Back to Pull requests

tail:Fix tail -f sparse file growth handling #12080

Open
mattsu2020 opened this pull request with 90 lines diff across 2 files
View on GitHub
mattsu2020 mattsu2020 commented on Apr 29, 2026 12:11 UTC
Author

Summary

Fixes tail -f behavior when a followed sparse file grows by a very large amount, such as via truncate -s 1P.

The follow path previously used BytesChunkBuffer::new(u64::MAX), which consumed all newly readable bytes into memory before printing. For sparse file holes, those bytes are read as NULs, so a large logical size increase could cause tail to consume excessive memory and get killed.

This changes follow-mode output to read one chunk to detect whether new data exists and preserve header behavior, then stream the remaining data directly to stdout with io::copy.

Fixes #12015.

Validation

  • cargo fmt --check

  • cargo check -p uu_tail

  • cargo test -p uu_tail

  • cargo build -p coreutils

  • cargo test --test tests test_n0_with_follow --features tail

  • Manual sparse growth check with tail -n0 -f --use-polling and truncate -s 1P
  • Diff: 90 lines (+74 / -16) across 2 files Reviewer: xtqqczze Review duration: 315350m
    All Vouch review confidence checks passed
    Residual risk 0.04 = Change Risk (0.38) × (1 − Review Confidence (0.89)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.38 · 90 lines diff across 2 files
    0.38
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.89 · 5 substantive comment(s)
    0.89
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    88%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.270
    Real PR Conversation Timeline
    12 items
    github-actions[bot] github-actions[bot] commented on Apr 29, 2026 12:19 UTC
    Bot

    <!-- gnu-comment-bot -->
    GNU testsuite comparison:

    Congrats! The gnu test tests/pr/options is no longer failing!
    

    xtqqczze xtqqczze commented on code on Apr 29, 2026 12:23 UTC
    Code Review
    src/uu/tail/src/follow/files.rs:1
    @@ -136,20 +136,32 @@ impl FileHandling {
     
         /// Read new data from `path` and print it to stdout
         pub fn tail_file(&mut self, path: &Path, verbose: bool) -> UResult<bool> {
    -        let mut chunks = BytesChunkBuffer::new(u64::MAX);
    -        if let Some(reader) = self.get_mut(path).reader.as_mut() {
    -            chunks.fill(reader)?;
    -        }
    -        if chunks.has_data() {
    +        let Some(mut reader) = self.get_mut(path).reader.take() else {
    +            return Ok(false);
    +        };
    +
    +        let result = (|| -> UResult<bool> {

    make this a named function?

    codspeed[bot] codspeed[bot] commented on Apr 29, 2026 12:28 UTC
    Bot

    <!-- __CODSPEED_PERFORMANCE_REPORT_COMMENT__ -->

    Merging this PR will degrade performance by 3.24%

    <details>
    <summary>:warning: <b>Different runtime environments detected</b></summary>

    Some benchmarks with significant performance changes were compared across different runtime environments,

    which may affect the accuracy of the results.

    > Open the report in CodSpeed to investigate

    </details>

    ❌ 1 regressed benchmark
    ✅ 338 untouched benchmarks
    ⏩ 46 skipped benchmarks[^skipped]

    [!WARNING]

    Please fix the performance issues or acknowledge them on CodSpeed.

    Performance Changes

    | | Mode | Benchmark | BASE | HEAD | Efficiency |
    | --- | ---- | --------- | ------ | ------ | ---------- |
    | ❌ | Simulation | [` uniq_check_chars[(10000, "1")] ](https://app.codspeed.io/uutils/coreutils/branches/mattsu2020%3Atail_12015?uri=src%2Fuu%2Funiq%2Fbenches%2Funiq_bench.rs%3A%3Auniq_check_chars%5B%2810000%2C+%221%22%29%5D&runnerMode=Simulation&utm_source=github&utm_medium=comment-v2&utm_content=benchmark) | 14.4 ms | 14.9 ms | -3.24% |

    [!TIP]

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

    ---

    <sub>Comparing <code>mattsu2020:tail_12015</code> (8af08a5) with <code>main</code> (bd73c58)</sub>

    <a href="https://app.codspeed.io/uutils/coreutils/branches/mattsu2020%3Atail_12015?utm_source=github&utm_medium=comment-v2&utm_content=button">
    <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://codspeed.io/pr-report/open-in-codspeed-dark.svg">
    <source media="(prefers-color-scheme: light)" srcset="https://codspeed.io/pr-report/open-in-codspeed-light.svg">
    <img alt="Open in CodSpeed" src="https://codspeed.io/pr-report/open-in-codspeed-light.svg" width="169" height="32">
    </picture>
    </a>

    [^skipped]: 46 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.

    Alonely0 Alonely0 commented on code on May 01, 2026 15:08 UTC
    Code Review
    src/uu/tail/src/follow/files.rs:1
    @@ -136,20 +136,32 @@ impl FileHandling {
     
         /// Read new data from `path` and print it to stdout
         pub fn tail_file(&mut self, path: &Path, verbose: bool) -> UResult<bool> {
    -        let mut chunks = BytesChunkBuffer::new(u64::MAX);
    -        if let Some(reader) = self.get_mut(path).reader.as_mut() {
    -            chunks.fill(reader)?;
    -        }
    -        if chunks.has_data() {
    +        let Some(mut reader) = self.get_mut(path).reader.take() else {
    +            return Ok(false);
    +        };
    +
    +        let result = (|| -> UResult<bool> {

    I'm not even sure there was a need for that in the first place... this is a weird JS idiom that popped up after I noped out of there. The new added function looks fine tho.

    xtqqczze xtqqczze commented on code on May 01, 2026 15:48 UTC
    Code Review
    src/uu/tail/src/follow/files.rs:1
    @@ -158,6 +153,30 @@ impl FileHandling {
             }
         }
     
    +    fn print_new_data(
    +        &mut self,
    +        path: &Path,
    +        verbose: bool,
    +        reader: &mut impl BufRead,
    +    ) -> UResult<bool> {
    +        let mut chunk = BytesChunk::new();
    +        if chunk.fill(&mut *reader)?.is_none() {

            if chunk.fill(reader)?.is_none() {
    

    xtqqczze xtqqczze requested changes on Jul 24, 2026 13:39 UTC
    Changes Requested Reviewer

    Requested changes.

    sylvestre sylvestre commented on Jul 24, 2026 15:12 UTC
    Contributor

    Is it possible to add a test? Thanks

    sylvestre sylvestre commented on code on Sep 29, 2026 01:37 UTC
    Code Review
    src/uu/tail/src/follow/files.rs:1
    @@ -158,6 +153,30 @@ impl FileHandling {
             }
         }
     
    +    fn print_new_data(
    +        &mut self,
    +        path: &Path,
    +        verbose: bool,
    +        reader: &mut impl BufRead,
    +    ) -> UResult<bool> {
    +        let mut chunk = BytesChunk::new();

    reader is already a BufRead, could we just check reader.fill_buf()?.is_empty() instead of copying into a BytesChunk?

    sylvestre sylvestre commented on code on Sep 29, 2026 01:37 UTC
    Code Review
    tests/by-util/test_tail.rs:1
    @@ -248,6 +248,46 @@ fn test_n0_with_follow() {
         child.kill();
     }
     
    +#[test]
    +#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
    +fn test_follow_sparse_file_growth_under_memory_limit() {
    +    use rlimit::Resource;
    +
    +    // The old follow implementation buffered the entire newly readable range. Keep the address

    please drop the "old implementation" part, something like "make sure we stream instead of buffering the new data" is enough

    mattsu2020 mattsu2020 commented on code on Sep 29, 2026 06:38 UTC
    Code Review
    tests/by-util/test_tail.rs:1
    @@ -248,6 +248,46 @@ fn test_n0_with_follow() {
         child.kill();
     }
     
    +#[test]
    +#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
    +fn test_follow_sparse_file_growth_under_memory_limit() {
    +    use rlimit::Resource;
    +
    +    // The old follow implementation buffered the entire newly readable range. Keep the address

    I have revised the comment.

    sylvestre sylvestre commented on Nov 28, 2026 07:48 UTC
    Contributor

    it has been conflicting for more than 7 days, sorry! please rebase it

    Reviewers
    Assignees
    mattsu2020 mattsu2020
    Labels
    risk: low confidence: 88.6% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 38.4% (MEDIUM) · Higher = larger blast radius
    38.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 88.6% · Lower = rushed or shallow review
    88.6%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 4.4% · Safe to merge
    4.4%