Skip to content

std: use readdir on nearly all UNIX platforms#158727

Open
joboet wants to merge 3 commits into
rust-lang:mainfrom
joboet:more_readdir
Open

std: use readdir on nearly all UNIX platforms#158727
joboet wants to merge 3 commits into
rust-lang:mainfrom
joboet:more_readdir

Conversation

@joboet

@joboet joboet commented Jul 3, 2026

Copy link
Copy Markdown
Member

POSIX.1-2024 formalised what was already guaranteed by a lot of implementations and required readdir to be thread-safe as long as an individual DIR* is not accessed concurrently (which ReadDir::next ensures by taking a mutable reference). But since our read_dir implementation predates that standard, we currently only utilise readdir on the platforms that guarantee thread-safety in their documentation. On other implementations – notably macOS – we use the readdir_r function, which was always required to be thread-safe but is problematic because it cannot handle directory entries with names longer than NAME_MAX.

However, even the first POSIX issue, POSIX.1-1994, specified that the data in the returned dirent

is not overwritten by another call to readdir() on a different directory stream.

and that guarantee together with the requirement that the underlying syscalls need to be thread-safe already because of readdir_r result in readdir being thread-safe on nearly all implementations, even if they predate POSIX.1-2024. Given the now formalised guarantee I think it safe to assume that currently thread-safe implementations will not be changed in a way that violates thread-safety.

CC T-libs, do you agree?
@rustbot label +I-libs-nominated

I thus looked at the readdir implementation of all the UNIXes currently utilising readdir_r to check for thread-safety:

On the implementations where I couldn't confirm thread-safety ReadDir will still use readdir_r, but I've changed the code so that this edge-case is limited to ReadDir::next. All other platforms now use readdir.

@rustbot

rustbot commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

miri is developed in its own repository. If possible, consider making this change to rust-lang/miri instead.

cc @rust-lang/miri

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 3, 2026
@rustbot

rustbot commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

r? @JohnTitor

rustbot has assigned @JohnTitor.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ChrisDenton, libs
  • @ChrisDenton, libs expanded to 13 candidates
  • Random selection from 7 candidates

@rustbot

rustbot commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@jonathanpallant

Copy link
Copy Markdown
Contributor

@joboet - you've linked to VxWorks docs under the QNX entry?

https://www.qnx.com/developers/docs/8.0/com.qnx.doc.neutrino.lib_ref/topic/r/readdir.html says:

The readdir() function is safe to use in multithreaded environments as long as the directory stream, DIR *dirp, isn't shared between threads.

https://www.qnx.com/developers/docs/7.1/com.qnx.doc.neutrino.lib_ref/topic/r/readdir.html says the same thing

@joboet

joboet commented Jul 3, 2026

Copy link
Copy Markdown
Member Author

Whoops, I got the RTOSs confused 😉 ... thanks for the link!

@RalfJung RalfJung left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Miri changes LGTM.

At some point we can probably remove readdir_r from macOS then given that it is generally considered a deprecated API, the only reason we support it is because std used it.

View changes since this review

Comment thread src/tools/miri/tests/pass-dep/libc/libc-fs.rs
Co-authored-by: Ralf Jung <post@ralfj.de>
@rustbot rustbot added the I-libs-nominated Nominated for discussion during a libs team meeting. label Jul 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

I-libs-nominated Nominated for discussion during a libs team meeting. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants