Skip to content

document remove_dir_all returning DirectoryNotEmpty on concurrent write #139958

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
trinity-1686a opened this issue Apr 17, 2025 · 1 comment · May be fixed by #140062
Open

document remove_dir_all returning DirectoryNotEmpty on concurrent write #139958

trinity-1686a opened this issue Apr 17, 2025 · 1 comment · May be fixed by #140062
Assignees
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools A-filesystem Area: `std::fs` T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@trinity-1686a
Copy link
Contributor

trinity-1686a commented Apr 17, 2025

Location

https://doc.rust-lang.org/std/fs/fn.remove_dir_all.html

Summary

remove_dir_all can fail with DirectoryNotEmpty. This is somewhat implied by this sentence:

remove_dir_all will fail if remove_dir or remove_file fail on any constituent paths, including the root path.

but when diagnosing an issue related to this function, it really didn't seem to me that DirectoryNotEmpty was an error this function could ever return, especially given the detailed error section, and a platform-specific section mentioning being TOCTOU free (note: i wouldn't consider this a TOCTOU vuln by any mean, but it implied to me a stronger kind of handling for concurrent access than there is).

error reproducer:

use std::fs::{create_dir, remove_dir_all, write};
use std::thread::spawn;

fn main() {
    create_dir("testdir").unwrap();

    for i in 0..100 {
        write(format!("testdir/{i}"), b"test_data").unwrap()
    }

    spawn(|| {
        for i in 0.. {
            if write(format!("testdir/new {i}"), b"test_data").is_err() {
                return;
            }
        }
    });

    remove_dir_all("testdir").unwrap();
}

I think the last line of the Error section could be rewritten as:

This function may emit io::ErrorKind::DirectoryNotEmpty if the directory is concurrently written into, but will only return io::ErrorKind::NotFound if no removal occurs.

@trinity-1686a trinity-1686a added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Apr 17, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 17, 2025
@jieyouxu jieyouxu added T-libs Relevant to the library team, which will review and decide on the PR/issue. A-filesystem Area: `std::fs` and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Apr 18, 2025
@xizheyin
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools A-filesystem Area: `std::fs` T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants