Skip to content

tail: follow new data from character devices - #16671

Open
rleungx wants to merge 1 commit into
uutils:mainfrom
rleungx:fix-tail-character-device-follow
Open

rleungx wants to merge 1 commit into
uutils:mainfrom
rleungx:fix-tail-character-device-follow

Conversation

@rleungx

@rleungx rleungx commented Nov 18, 2026 •

Copy link
Copy Markdown

tail -f and tail -F stop picking up terminal input after the first EOF. They wait for file-change events, but typing into a terminal doesn't produce those events.

This makes tail poll character devices instead. Reads are nonblocking so a terminal with nothing to read won't hold up other files or --pid. The tests reproduce this with a pseudo-terminal.

Related to #16643. #16670 covers the debug output.

Copilot AI balanced review requested due to automatic review settings November 18, 2026 04:11

Copilot AI left a comment

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

1 open issue
What changed in this PR

Adds robust tail -f/-F support for character devices by switching follow mode to polling when needed and performing nonblocking reads so quiet devices don’t stall other inputs or PID checks.

Changes:

  • Add regression test covering follow behavior on PTYs (character devices), including multiple inputs and partial lines.
  • Update follow watcher to enable polling when any input is a character device.
  • Refactor follow file handling to store concrete BufReader<File> and perform nonblocking reads for character devices.
File Description
tests/​by-util/​test_tail.rs Adds PTY/character-device follow regression test cases.
src/​uu/​tail/​src/​tail.rs Adapts add_path call to new concrete reader type.
src/​uu/​tail/​src/​follow/​watch.rs Detects character-device inputs and forces polling mode.
src/​uu/​tail/​src/​follow/​mod.rs Updates WASI stubs to match new reader type signature.
src/​uu/​tail/​src/​follow/​files.rs Switches reader storage to BufReader<File> and adds nonblocking char-device read path.

Review effort: Lite

Comment thread src/uu/tail/src/follow/files.rs Resolved
Copilot AI balanced review requested due to automatic review settings November 18, 2026 04:31

Copilot AI left a comment

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

5 open issues

Review effort: Lite

Comment thread src/uu/tail/src/follow/files.rs Resolved
Comment thread src/uu/tail/src/follow/files.rs Resolved
Comment thread src/uu/tail/src/follow/watch.rs Resolved
Comment thread tests/by-util/test_tail.rs Resolved
Copilot AI balanced review requested due to automatic review settings November 18, 2026 04:49

Copilot AI left a comment

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.

Comment thread src/uu/tail/src/follow/files.rs Resolved
Comment thread src/uu/tail/src/follow/watch.rs Resolved
Comment thread tests/by-util/test_tail.rs Resolved
Copilot AI balanced review requested due to automatic review settings November 18, 2026 05:44

Copilot AI left a comment

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

12 open issues

Review effort: Lite

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/files.rs Resolved
Comment thread src/uu/tail/src/follow/watch.rs Resolved
Copilot AI balanced review requested due to automatic review settings November 18, 2026 06:19

Copilot AI left a comment

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

16 open issues

Review effort: Lite

Comment thread src/uu/tail/src/follow/files.rs Resolved
Comment thread src/uu/tail/src/follow/files.rs Resolved
Comment thread tests/by-util/test_tail.rs Resolved
Comment thread src/uu/tail/src/follow/files.rs Resolved
Copilot AI balanced review requested due to automatic review settings November 18, 2026 07:50

Copilot AI left a comment

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

20 open issues

Review effort: Lite

Comment thread src/uu/tail/src/follow/watch.rs Resolved
Comment thread src/uu/tail/src/follow/files.rs Resolved
Comment thread tests/by-util/test_tail.rs Resolved
Comment thread tests/by-util/test_tail.rs Resolved
@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.49 MB (+104 KB, +0.07%)

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

Comment thread src/uu/tail/src/follow/files.rs Resolved
Comment thread tests/by-util/test_tail.rs Resolved
Comment thread src/uu/tail/src/follow/files.rs Resolved
Comment thread src/uu/tail/src/follow/watch.rs Resolved
Comment thread src/uu/tail/src/follow/watch.rs Resolved
Copilot AI balanced review requested due to automatic review settings November 19, 2026 05:35

Copilot AI left a comment

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

22 open issues

And 2 more that still need to be addressed.

Review effort: Lite

Comment thread src/uu/tail/src/follow/watch.rs Resolved
Comment thread src/uu/tail/src/follow/files.rs Resolved
Copilot AI balanced review requested due to automatic review settings November 19, 2026 06:41

Copilot AI left a comment

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

25 open issues

And 5 more that still need to be addressed.

Review effort: Lite

Comment thread src/uu/tail/src/follow/watch.rs Resolved
Comment thread src/uu/tail/src/follow/watch.rs Resolved
Comment thread tests/by-util/test_tail.rs Resolved
Copilot AI balanced review requested due to automatic review settings November 19, 2026 07:40

Copilot AI left a comment

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

29 open issues

And 9 more that still need to be addressed.

Review effort: Lite

Comment thread src/uu/tail/src/follow/files.rs Resolved
Comment thread src/uu/tail/src/follow/watch.rs Resolved
Comment thread tests/by-util/test_tail.rs Resolved
Comment thread tests/by-util/test_tail.rs Resolved
Copilot AI balanced review requested due to automatic review settings November 19, 2026 09:11

Copilot AI left a comment

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

18 open issues
14 resolved since last review

Review effort: Lite

Comment thread src/uu/tail/src/follow/watch.rs Resolved
Comment thread tests/by-util/test_tail.rs Resolved
Comment thread tests/by-util/test_tail.rs Resolved
Copilot AI balanced review requested due to automatic review settings November 19, 2026 10:11

Copilot AI left a comment

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

4 open issues
18 resolved since last review

Review effort: Lite

Comment on lines +438 to +441
&& !pd.reader.as_ref().unwrap().get_ref().metadata()?.file_id_eq(&new_md))
|| (self.use_polling && !old_md.file_id_eq(&new_md)) {
// Compare with the open reader: a device-name check may
// have handled the replacement before its native event.
Comment on lines +57 to +66
#[cfg(unix)]
fn wait_for_output(child: &mut uutests::util::UChild, expected: &str) -> bool {
for _ in 0..500 {
if child.stdout_all() == expected {
return true;
}
child.delay(10);
}
child.stdout_all() == expected
}
Comment on lines +302 to +306
use rustix::event::{PollFd, PollFlags, Timespec, poll};

let mut fds = [PollFd::new(file, PollFlags::IN)];
rustix::io::retry_on_intr(|| poll(&mut fds, Some(&Timespec::default())))
.map(|ready| ready != 0)
Comment thread tests/by-util/test_tail.rs Outdated
);
// A quiet device must not block output from the other input.
ts.fixtures.append("other", "other file\n");
wait_for_output(&mut child, "new dataother file\n");
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

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

@codspeed

codspeed Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will regress 28 benchmarks

1 warning, 2 notes

⚠️ 122 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

ℹ️ This run is excluded from the branch history

This commit was force-pushed, so this run no longer appears in the branch's performance history.

ℹ️ 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

⚡ 104 improved benchmarks
❌ 28 regressed benchmarks
✅ 133 untouched benchmarks
⏩ 224 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ CPU Simulation check_sorted_utf8_locale 19.1 ms 426 ms -95.52%
❌ CPU Simulation mv_directory 588 µs 6,766 µs -91.31%
❌ CPU Simulation dd_copy_1m_blocks 837.6 µs 1,465.5 µs -42.84%
❌ CPU Simulation numfmt_stream_to_si 156.7 ms 260.8 ms -39.9%
❌ CPU Simulation numfmt_stream_to_si_precision 202.7 ms 304.2 ms -33.36%
❌ CPU Simulation cksum_sha512 1.2 s 1.6 s -22.66%
❌ CPU Simulation cksum_sha384 1.2 s 1.6 s -22.66%
❌ CPU Simulation numfmt_to_si_precision[10000] 62.4 ms 78.7 ms -20.69%
❌ CPU Simulation numfmt_round_modes[("towards-zero", 10000)] 60.8 ms 76.4 ms -20.43%
❌ CPU Simulation numfmt_round_modes[("down", 10000)] 60.8 ms 76.4 ms -20.4%
❌ CPU Simulation numfmt_round_modes[("up", 10000)] 60.7 ms 76.2 ms -20.36%
❌ CPU Simulation sort_very_long_lines_utf8_locale 118.8 ms 149.1 ms -20.3%
❌ CPU Simulation numfmt_to_iec[10000] 60.7 ms 75.9 ms -20.02%
❌ CPU Simulation numfmt_to_si[10000] 60.6 ms 75.5 ms -19.72%
❌ CPU Simulation numfmt_padding[(10000, 50)] 64.9 ms 80.6 ms -19.46%
❌ CPU Simulation numfmt_large_numbers_si[10000] 66.8 ms 81.8 ms -18.35%
❌ CPU Simulation factor_batch_of_small_numbers 116.4 ms 138.6 ms -16.04%
❌ CPU Simulation true_consecutive_calls 294.6 ns 350.2 ns -15.88%
❌ CPU Simulation false_consecutive_calls 294.6 ns 350.2 ns -15.88%
❌ CPU Simulation file_localized_names 22.7 ms 26.8 ms -15.29%
... ... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

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


Comparing rleungx:fix-tail-character-device-follow (f2ec773) with main (279cdbf)2

Open in CodSpeed

Footnotes

  1. 224 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 (5383c05) during the generation of this report, so 279cdbf was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

Copilot AI balanced review requested due to automatic review settings November 20, 2026 06:08

Copilot AI left a comment

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.

Comment on lines +249 to +257
pub fn from_other_with_path(data: Self, path: &Path, nonblocking: bool) -> UResult<Self> {
// Remove old reader
let old_reader = data.reader;
let reader = if old_reader.is_some() {
// Use old reader with the same file descriptor if there is one
old_reader
} else if let Ok(file) = open_file(path, nonblocking) {
} else if let Ok((file, _)) = open_follow_file(path, nonblocking) {
// Open new file tail from start
Some(Box::new(BufReader::new(file)) as Box<dyn BufRead>)
Some(BufReader::new(file))
}

pub fn contains_key(&self, k: &Path) -> bool {
self.map.contains_key(k)
Comment on lines +323 to +346
#[cfg(target_vendor = "apple")]
fn device_is_readable(file: &File) -> io::Result<bool> {
use rustix::event::{FdSetElement, Timespec, fd_set_insert, fd_set_num_elements, select};
use std::os::fd::AsRawFd;

// Apple's poll reports POLLNVAL for devices such as /dev/null and /dev/zero.
let fd = file.as_raw_fd();
let mut read_fds = vec![FdSetElement::default(); fd_set_num_elements(1, fd + 1)];
rustix::io::retry_on_intr(|| {
fd_set_insert(&mut read_fds, fd);
// SAFETY: file keeps the sole descriptor in the correctly sized set open.
unsafe {
select(
fd + 1,
Some(&mut read_fds),
None,
None,
Some(&Timespec::default()),
)
}
})
.map(|ready| ready != 0)
.map_err(io::Error::from)
}
@xtqqczze

Copy link
Copy Markdown
Collaborator

please squash commits for a clean branch history

@rleungx
rleungx force-pushed the fix-tail-character-device-follow branch from ffa06a1 to a2dd8cb Compare November 23, 2026 13:51
@sylvestre
sylvestre force-pushed the fix-tail-character-device-follow branch from a2dd8cb to 87392e7 Compare November 23, 2026 21:38
Copilot Bot balanced review requested due to automatic review settings November 23, 2026 21:38

ghost left a comment

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.

Comment on lines +361 to +369
if !self.follow_name() || !self.files.character_devices().contains(path) {
return Ok(());
}
let kind = match path.metadata() {
Ok(metadata) => {
// Compare with the open device: cached path metadata may already
// describe a replacement that arrived during the previous read.
let reader = self.files.get(path).reader.as_ref().unwrap();
if reader.get_ref().metadata()?.file_id_eq(&metadata) {
Comment on lines +726 to +730
if observer.files.character_devices().contains(path) {
// Device events can describe the old target after a rename.
// Check the current name in the print loop; -f keeps its FD.
if scheduled.insert(path.clone()) {
paths.push(path.clone());
Comment on lines +5833 to +5840
let deadline = Instant::now() + Duration::from_secs(5);
while marker.len() < b"other file\n".len() && Instant::now() < deadline {
let count = output.read(&mut buffer).unwrap();
if count == 0 {
break;
}
marker.extend(buffer[..count].iter().copied().filter(|byte| *byte != 0));
}
@rleungx
rleungx force-pushed the fix-tail-character-device-follow branch from 87392e7 to 810ca92 Compare December 1, 2026 07:43
Copilot Bot balanced review requested due to automatic review settings December 1, 2026 07:43

ghost left a comment

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

13 open issues

Review effort: Lite

Comment on lines +885 to +895
// The polling backend already includes every input above.
if !observer.use_polling {
paths.extend(
observer
.files
.character_devices()
.iter()
.filter(|path| scheduled.insert((*path).clone()))
.cloned(),
);
}
Comment on lines +58 to +67
#[cfg(unix)]
fn wait_for_output(child: &mut uutests::util::UChild, expected: &str) -> bool {
for _ in 0..500 {
if child.stdout_all() == expected {
return true;
}
child.delay(10);
}
child.stdout_all() == expected
}
Comment thread src/uu/tail/src/paths.rs
Comment on lines +348 to +352
let proc = std::fs::metadata("/proc").unwrap();
let pts = std::fs::metadata("/dev/pts").unwrap();
assert_eq!(proc.ino(), pts.ino());
assert_ne!(proc.dev(), pts.dev());
assert!(proc.file_id_eq(&std::fs::metadata("/proc").unwrap()));
Poll character devices alongside native file watches so tail -f and -F
continue reading after EOF. Keep owned device readers nonblocking, bound
each read batch, and preserve inherited stdin flags.

Watch device parents on native backends, reopen replacements by device
and inode, and cover terminal input, aliases, replacement, and shutdown.

Signed-off-by: rleungx <rleungx@gmail.com>
@sylvestre
sylvestre force-pushed the fix-tail-character-device-follow branch from 810ca92 to fe8ede5 Compare December 1, 2026 18:59
Copilot Bot balanced review requested due to automatic review settings December 1, 2026 18:59

ghost left a comment

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

17 open issues

Review effort: Lite

self.reopen(event_path)?;
} else if event.kind == EventKind::Modify(ModifyKind::Name(RenameMode::To))
} else if (event.kind == EventKind::Modify(ModifyKind::Name(RenameMode::To))
&& !pd.reader.as_ref().unwrap().get_ref().metadata()?.file_id_eq(&new_md))
Comment on lines +725 to +733
let timeout = if read_devices_again {
Duration::ZERO
} else if observer.files.character_devices().is_empty() {
settings.sleep_sec
} else {
settings
.sleep_sec
.saturating_sub(last_device_poll.elapsed())
};
.unwrap()
.receiver
.recv_timeout(settings.sleep_sec);
.recv_timeout(timeout);
Comment on lines +61 to +66
if child.stdout_all() == expected {
return true;
}
child.delay(10);
}
child.stdout_all() == expected

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.

4 participants