Skip to content

Copy tee output when there is more than one destination - #16061

Closed
kprobe0 wants to merge 1 commit into
uutils:mainfrom
kprobe0:tee-copy-multiple-outputs
Closed

kprobe0 wants to merge 1 commit into
uutils:mainfrom
kprobe0:tee-copy-multiple-outputs

Conversation

@kprobe0

@kprobe0 kprobe0 commented Oct 30, 2026 •

Copy link
Copy Markdown

Summary

  • tee FILE on Linux splices the input, so standard output and FILE can end up with different bytes if the input pages change before the pipe is read. Fixes tee: zero-copy lets stdout and FILE differ if input changes after tee exits #15803.
  • Splice stays in place when there is only one output. Two or more outputs use the existing read/write loop, which writes the same buffer to each of them.
  • A test overwrites the input after tee exits and checks that standard output and the file both still hold the original bytes.

Test plan

  • cargo test --test tests test_tee on macOS (16 tests, including the new one)
  • cargo check -p uu_tee --target x86_64-unknown-linux-gnu
  • On Linux, the same test, plus tee snapshot < upload | cat after an in-place rewrite of upload, with both outputs identical

Splice keeps references to the input pages, so a pipe and a file can observe different bytes if those pages change before the pipe is read. Keep splice for a single output. With two or more outputs, read one buffer and write that buffer to each of them.
@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

GNU test failed: tests/tee/write-eagain. tests/tee/write-eagain is passing on 'main'. Maybe you have to rebase?

@oech3

oech3 commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

One write code is more simple. Would you do something like

        if let [writer] = &mut self.writers[..] {
            match uucore::pipes::splice_unbounded_auto(&input, writer) {
                Ok(Ok(())) => return Ok(()),
                Ok(Err(())) => {} // no splice support
                Err(e) => {
                    return process_error(
                        self.output_error_mode,
                        e.into(),
                        writer,
                        &mut self.ignored_errors,
                    )
                    .map_err(|_| ());
                }
            }
    }

instead?

Comment thread src/uu/tee/src/tee.rs
Comment on lines +162 to +165
// One output can be spliced: there is no second copy for a later change
// of the input pages to disagree with. Two or more outputs must share
// one userspace buffer, because splice and tee keep references to the
// input pages and each output would materialise them at a different time.

@oech3 oech3 Oct 31, 2026 •

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.

Suggested change
// One output can be spliced: there is no second copy for a later change
// of the input pages to disagree with. Two or more outputs must share
// one userspace buffer, because splice and tee keep references to the
// input pages and each output would materialise them at a different time.
// splice is restricted to one writer since input's page caches can be changed after writing to 1st writer.

Please make the comment simpler.

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.

By the way, if kernel added SPLICE_F_FORCE_COPY to splice, or there is a low-cost way to reflesh memfd, we can use it for draining input and keep zero-copy at other places to change 2 memcpy to 1 memcpy.

Comment thread tests/by-util/test_tee.rs
Comment on lines +846 to +851
/// Splice keeps references to the input pages. With a file and a pipe, those
/// pages can change after tee exits and before the pipe is read, so the two
/// outputs would disagree. Both must keep the bytes tee actually read.
#[test]
#[cfg(unix)]
fn test_tee_file_and_stdout_keep_the_bytes_that_were_read() {

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.

Suggested change
/// Splice keeps references to the input pages. With a file and a pipe, those
/// pages can change after tee exits and before the pipe is read, so the two
/// outputs would disagree. Both must keep the bytes tee actually read.
#[test]
#[cfg(unix)]
fn test_tee_file_and_stdout_keep_the_bytes_that_were_read() {
#[test]
#[cfg(unix)]
fn test_tee_file_and_stdout_keep_the_bytes() {

Different reason an be added in the future.

Comment thread tests/by-util/test_tee.rs

let scene = TestScenario::new("tee");
let at = &scene.fixtures;
let original = vec![b'a'; 32 * 1024];

@oech3 oech3 Oct 31, 2026 •

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.

Would you use 1 MiB 'aaa...' + 64KiB 'bbb...' (then original is 1024 + 64 KiB).

@kprobe0 kprobe0 closed this Oct 31, 2026
@kprobe0
kprobe0 deleted the tee-copy-multiple-outputs branch October 31, 2026 10:13
@oech3

oech3 commented Oct 31, 2026 via email

Copy link
Copy Markdown
Contributor

@oech3

oech3 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

I Co-authored your noreply addr in #16204 .

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.

tee: zero-copy lets stdout and FILE differ if input changes after tee exits

2 participants