Skip to content

Issue with write_all_at on GitHub Action #140867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
allevo opened this issue May 9, 2025 · 3 comments
Open

Issue with write_all_at on GitHub Action #140867

allevo opened this issue May 9, 2025 · 3 comments
Labels
C-external-bug Category: issue that is caused by bugs in software beyond our control needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. O-linux Operating system: Linux

Comments

@allevo
Copy link
Contributor

allevo commented May 9, 2025

write_all_at doesn't write the buffer at the given index. The content is written at the end of the file.
I created a repo on my github https://github.com/allevo/rust-fs-write-all-bug to show the error.

Locally on Mac M2, the test on that repo passes, but on CI it doesn't.

NB:

  • write_all_at and read_exact_at doesn't change the cursor position
  • flush or/and sync_all doesn't fix

I tried this code:

use std::{io::Write, os::unix::fs::FileExt, path::PathBuf};

pub fn run(file_path: PathBuf) {
    let mut page_file = std::fs::File::options()
        .create(true)
        .append(true)
        .read(true)
        .open(&file_path)
        .unwrap();

    let n: u32 = 10;
    let buf: [u8; 4] = n.to_be_bytes();
    page_file
        .write_all_at(&buf, 0).unwrap();

    page_file.flush().unwrap();
    page_file
        .sync_all().unwrap();

    let mut buf: [u8; 4] = [0; 4];
    page_file
        .read_exact_at(&mut buf, 0).unwrap();
    let n = u32::from_be_bytes(buf);

    page_file.seek(SeekFrom::Start(0)).unwrap();

    let buf: [u8; 4] = (n + 1).to_be_bytes();
    page_file
        .write_all_at(&buf, 0).unwrap();
}

I expected to see this happen: The file content will be [0, 0, 0, 11]

Instead, this happened: [0, 0, 0, 10, 0, 0, 0, 11]

Meta

rustup --version && rustc --version --verbose:

info: This is the version for the rustup toolchain manager, not the rustc compiler.
rustup 1.2[8](https://github.com/allevo/rust-fs-write-all-bug/actions/runs/14934973609/job/41960164587#step:3:9).1 (f9edccde0 2025-03-05)
info: The currently active `rustc` version is `rustc 1.86.0 (05f[9](https://github.com/allevo/rust-fs-write-all-bug/actions/runs/14934973609/job/41960164587#step:3:10)846f8 2025-03-31)`
rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7
Backtrace

In this case it is not important, the assertion fails

@allevo allevo added the C-bug Category: This is a bug. label May 9, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 9, 2025
@paolobarbolini
Copy link
Contributor

POSIX requires that opening a file with the O_APPEND flag should have no affect on the location at which pwrite() writes data. However, on Linux, if a file is opened with O_APPEND, pwrite() appends data to the end of the file, regardless of the value of offset.

This seems to be a know bug. See the Bugs section https://linux.die.net/man/2/pwrite64

@bjorn3 bjorn3 added the O-linux Operating system: Linux label May 9, 2025
@lolbinarycat lolbinarycat added C-external-bug Category: issue that is caused by bugs in software beyond our control and removed C-bug Category: This is a bug. labels May 9, 2025
@the8472
Copy link
Member

the8472 commented May 10, 2025

Btw, die.net hasn't been updated in a long time, use man7.org or distro documentation for manpages.

Linux 6.9 introduced RWF_NOAPPEND as a flag to say "no really, do a positioned write even on O_APPEND".

https://man7.org/linux/man-pages/man2/pwritev2.2.html

@the8472
Copy link
Member

the8472 commented May 11, 2025

The flags aren't in libc yet, rust-lang/libc#4452

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-external-bug Category: issue that is caused by bugs in software beyond our control needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. O-linux Operating system: Linux
Projects
None yet
Development

No branches or pull requests

6 participants