Skip to content

tail: fix following redirected stdin - #9093

Open
mattsu2020 wants to merge 1 commit into
uutils:mainfrom
mattsu2020:tail_f_fix
Open

mattsu2020 wants to merge 1 commit into
uutils:mainfrom
mattsu2020:tail_f_fix

Conversation

@mattsu2020

@mattsu2020 mattsu2020 commented Oct 30, 2025 •

Copy link
Copy Markdown
Contributor

tail -f < file exits after the initial read instead of following further appends. Resolve and watch redirected stdin at read time and reuse its open descriptor, preserving the input offset and following the same file after renames, replacements, or truncation.

Add regression coverage for redirected stdin and descriptor following, with unsupported cases skipped on WASI.

Closes #7614

@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Skipping an intermittent issue tests/tail/overlay-headers (passes in this run but fails in the 'main' branch)

@anastygnome

Copy link
Copy Markdown
Contributor

The CI fail seems unrelated.

Could you squash your commits, for us to review your work better, and prepare for the future merge?

@github-actions

github-actions Bot commented Nov 1, 2025

Copy link
Copy Markdown

GNU testsuite comparison:

Skipping an intermittent issue tests/misc/tee (passes in this run but fails in the 'main' branch)

@mattsu2020

Copy link
Copy Markdown
Contributor Author

The CI fail seems unrelated.

Could you squash your commits, for us to review your work better, and prepare for the future merge?

Done

@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/tail/overlay-headers (fails in this run but passes in the 'main' branch)

1 similar comment
@github-actions

github-actions Bot commented Dec 1, 2025

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/tail/overlay-headers (fails in this run but passes in the 'main' branch)

@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Skipping an intermittent issue tests/misc/tee (passes in this run but fails in the 'main' branch)

@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Skipping an intermittent issue tests/shuf/shuf-reservoir (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/sort/sort-stale-thread-mem (passes in this run but fails in the 'main' branch)

@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Skipping an intermittent issue tests/tty/tty-eof (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/tail/follow-name is no longer failing!

@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/tail/follow-name (fails in this run but passes in the 'main' branch)
Note: The gnu test tests/cut/bounded-memory is now being skipped but was previously passing.
Note: The gnu test tests/dd/no-allocate is now being skipped but was previously passing.
Note: The gnu test tests/rm/many-dir-entries-vs-OOM is now being skipped but was previously passing.
Congrats! The gnu test tests/tail/pipe-f is now passing!

@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

GNU test failed: tests/date/date-locale-hour. tests/date/date-locale-hour is passing on 'main'. Maybe you have to rebase?
Skip an intermittent issue tests/pr/bounded-memory (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/tail/follow-name (fails in this run but passes in the 'main' branch)
Congrats! The gnu test tests/misc/io-errors is no longer failing!
Note: The gnu test tests/printf/printf-surprise is now being skipped but was previously passing.
Note: The gnu test tests/tail/tail-n0f is now being skipped but was previously passing.

Comment thread src/uu/tail/src/follow/watch.rs Resolved
Comment thread src/uu/tail/src/follow/watch.rs Resolved
Comment thread src/uu/tail/src/follow/watch.rs Resolved
Comment thread src/uu/tail/src/tail.rs Resolved
Comment thread src/uu/tail/src/tail.rs Outdated
return Ok(());
}

if path.metadata().map(|meta| meta.is_dir()).unwrap_or(false) {

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.

tail_file already handles directories, this duplicates it. could we just let it go there?

Comment thread src/uu/tail/src/tail.rs Resolved
Comment thread tests/by-util/test_tail.rs Resolved
@mattsu2020
mattsu2020 marked this pull request as draft October 8, 2026 13:00
@mattsu2020
mattsu2020 marked this pull request as ready for review October 9, 2026 11:15
Comment thread src/uu/tail/src/follow/mod.rs Outdated
}

#[allow(clippy::unnecessary_wraps)]
pub fn set_stdin_key<P: Into<PathBuf>>(&mut self, _path: P) -> UResult<()> {

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.

i am really not a fan of this function.
it probably demonstrates a refactor

@mattsu2020
mattsu2020 marked this pull request as draft October 10, 2026 11:26
@codspeed

codspeed Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 21.05%

1 warning, 1 note

⚠️ 126 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

ℹ️ 1 benchmark has no flamegraph

The flamegraph wasn't generated: most of the time was spent in runtime internals, which would leave nothing useful to analyze.

See affected benchmarks

⚡ 3 improved benchmarks
✅ 396 untouched benchmarks
⏩ 90 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
⚡ Memory timeout_enforced 167.5 KB 124.7 KB +34.26%
⚡ CPU Simulation socket_addrs_direct[100000] 132.1 µs 105.2 µs +25.57%
⚡ CPU Simulation hostname_ip_lookup[100000] 573.3 µs 544.9 µs +5.21%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing mattsu2020:tail_f_fix (939f0b2) with main (2a072db)

Open in CodSpeed

Footnotes

  1. 90 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. ↩

@mattsu2020 mattsu2020 changed the title Fix: tail -f test fails tail: fix following redirected stdin Oct 10, 2026
@mattsu2020
mattsu2020 marked this pull request as ready for review November 14, 2026 12:11
@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

GNU test failed: tests/tail/truncate. tests/tail/truncate is passing on 'main'. Maybe you have to rebase?

@anastygnome

Copy link
Copy Markdown
Contributor

You need to annotate the test not to run on wasi

Comment thread src/uu/tail/src/tail.rs Resolved
Comment thread src/uu/tail/src/chunks.rs Resolved
Comment thread src/uu/tail/src/follow/watch.rs Resolved
Comment thread src/uu/tail/src/follow/watch.rs Resolved
@sylvestre

Copy link
Copy Markdown
Contributor

thanks for picking this up!
But it is a large PR :(

This does more than fix tail -f < file: please split the offset fixes into another PR (see inline) and get CI green on wasm and macOS.
Then please squash into one commit.

@sylvestre

Copy link
Copy Markdown
Contributor

sorry but it needs to be rebased, and a bunch of jobs are failing

@github-actions

Copy link
Copy Markdown

Binary size comparison:

Individual binary size comparison VS main (threshold: >=5% AND >=4 KB).

Total size of compared binaries: 155.48 MB (+100 KB, +0.06%)

Significant per-binary changes:
  env     1.20 MB ->    1.28 MB  (+80 KB, +6.51%)


if observer.use_polling && settings.follow.is_some() {
if settings.follow.is_some() && observer.use_polling {
// Consider all files to potentially have new content.

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.

this also changes plain tail -f file when the file is moved (orphans re-read every loop, the follow_name() guard above, test_follow_descriptor_moved_while_other_file_changes). could you please split it into its own 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.

#17002
I've opened a PR for this

Comment thread src/uu/tail/src/paths.rs Outdated
return None;
}

String::from_utf16(&buffer[..len]).ok().map(PathBuf::from)

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.

please use OsString::from_wide here, otherwise a non UTF-16 path is silently not followed.
same thing for paths longer than MAX_PATH

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.

fixedit

1,
translate!("tail-error-recv-timeout-error", "error" => e),
));
}

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.

why swap the two conditions?

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.

fixed it

Comment thread tests/by-util/test_tail.rs Outdated
#[uutests::wasi_ignore(TailFollowDisabled)]
#[test]
#[cfg(unix)]
fn test_follow_descriptor_moved_while_other_file_changes() {

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.

a writer thread plus atomics is a lot for a test, could it be simpler?

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.

fixed it

@mattsu2020
mattsu2020 marked this pull request as draft November 29, 2026 22:14
@mattsu2020
mattsu2020 marked this pull request as ready for review November 29, 2026 23:19
EventKind::Modify(ModifyKind::Any | ModifyKind::Metadata(MetadataKind::Any | MetadataKind::WriteTime) | ModifyKind::Data(_) | ModifyKind::Name(RenameMode::To)) |
EventKind::Create(CreateKind::File | CreateKind::Folder | CreateKind::Any) => {
if let Ok(new_md) = event_path.metadata() {
if follows_open_file {

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.

this branch is also taken for plain tail -f file: with polling, a replaced file is no longer reopened, and a file replaced by something untailable no longer resets the reader.
could you please move that to #17002, or add tests for plain tail -f file here?

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.

I updated it to add more test cases.

Comment thread src/uu/tail/src/tail.rs Outdated
} else {
open_file(path, settings.use_nonblocking())
};
let open_result = open_file(path, settings.use_nonblocking());

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.

both cfg branches are the same now, could we drop the cfg?

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.

sorry fixed it

Resolve and watch redirected stdin at read time and reuse its open descriptor, preserving the input offset and following the same file after renames, replacements, or truncation.

Add regression tests for redirected stdin and descriptor following.

This branch has not been deployed

No deployments
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: -f option doesn't work with stdin

3 participants