Skip to content

Should uutils-coreutils respect stdbuf buffering options? #7967

Closed

Description

@Ecordonnier

Context: while trying to implement a regression-test for #6591 I noticed that testing stdbuf with some GNU coreutils utilities work, but fails with uutils coreutils utilities.

  • stdbuf -o works by changing the buffering option. Specifically by preloading libstdbuf.so which contains an overriden version of setvbuf. This function takes precedence over the normal setvbuf function of the libc library.

  • when C programs call IO functions like printf, setvbuf() gets called

  • when Rust programs call IO functions like print or println, setvbuf() does not get called, and thus stdbuf -o has no effect (at least this is what my testing is showing. See also this explanation

  • in theory, the buffering behavior of C programs could be emulated in rust e.g. by reading the value of the environment variable "_STDBUF_O" which is set by stdbuf, or by somehow calling setvbuf for IO.

Should this emulation be implemented in uutils-coreutils? I checked the POSIX specification, and have not found an explicit specification of the buffering behavior for e.g. "cut", and it seems to be rather part of the C standard. So as far as I understand uutils-coreutils could be POSIX compliant without this behavior, but it would be a small difference with GNU coreutils.

Details of the test:
Process 1 doing:

#!/bin/bash

set -x

COREUTILS=./target/debug/coreutils
rm -f log_file2
touch log_file2
$COREUTILS tail -f log_file2 | $COREUTILS stdbuf -o1000000 $COREUTILS cut -d " " -f 1
#tail -f log_file2 | stdbuf -o1000000 cut -d " " -f 1

Process 2 doing:

#!/bin/bash

echo "A 1" >> log_file2

If stdbuf has an effect, you should see nothing in the stdout of process 1 (happens with GNU coreutils). If stdbuf has no effect, you should see "A" in the stdout of process 1 (happens with uutils coreutils).

Activity

  1. sylvestre commented on Jun 2, 2025

    @sylvestre
    SponsorContributor

    I would prefer we are doing the same as GNU. IMHO, it is the gold standard (more than the POSIX specification)
    but I understand that might be super hard to do

  2. Ecordonnier commented on Jun 7, 2025

    @Ecordonnier
    CollaboratorAuthor

    OK. Thanks for your feedback!

  3. added a commit that references this issue on Nov 15, 2025
  4. Ecordonnier commented on Dec 5, 2025

    @Ecordonnier
    CollaboratorAuthor

    See also rust-lang/rust#60673 . I think it may not make sense to reopen this ticket as there is not much we can do at the moment. But rust-lang/rust#60673 discusses making line-buffering configrable in the rust standard library, at which point I hope it would be easy to make it work with uutil stdbuf.

  5. victor-prokhorov commented on Mar 28, 2026

    @victor-prokhorov
    Contributor

    Assuming an ACP was approved and the implementation landed, buffering mode could be changed at runtime in Rust via shared state inside libstd. But this only works when libstdbuf.so and the target binary are both linked against the same libstd instance, i.e. both built with -C prefer-dynamic, so they share a single OnceLock within that process. With static linking each binary embeds its own copy of libstd, so the preloaded library and the target binary end up with separate state and the mode change has no effect.

    Given that matching GNU behavior is the goal, would dynamically linked builds be acceptable, at least optionally, or is the preference to remain on static builds and pursue a different, Rust-native mechanism for buffering control?

    In other words, is matching GNU stdbuf behavior considered important enough to justify relying on shared libstd via dynamic linking? @sylvestre, @Ecordonnier

  6. oech3 commented on Apr 27, 2026

    @oech3
    Contributor

    If we can achieve same performance with buffered I/O without buffering, we don't need stdbuf. Right? I think few utils supporting zero-copy are already doing it by default (at least on Linux) and tail -f can do that at least for.

  7. oech3 commented on May 16, 2026

    @oech3
    Contributor

    If we want to support Windows without cygwin dll dep, we need to parse env vars.

  8. oech3 commented on Sep 30, 2026

    @oech3
    Contributor

    I don't think it is possible. But can we mimic stdbuf's buffering using ptrace? @BAMF0

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions