Skip to content

Use File::*lock*() in rustc_data_structures::flock when possible - #162584

Open
bjorn3 wants to merge 3 commits into
rust-lang:mainfrom
bjorn3:incr_comp_prefer_flock
Open

Use File::*lock*() in rustc_data_structures::flock when possible#162584
bjorn3 wants to merge 3 commits into
rust-lang:mainfrom
bjorn3:incr_comp_prefer_flock

Conversation

@bjorn3

@bjorn3 bjorn3 commented Sep 10, 2026

Copy link
Copy Markdown
Member

And only fallback to fcntl(F_SETLK) on Unix when flock() is not supported. flock() is a lot more sane than fcntl(F_SETLK) as it is fd scoped rather than process-scoped and reentrant. fcntl(F_SETLK) risks multiple instances of rustc inside the same process acquiring a lock on the same file even when an exclusive lock is requested. Using File::*lock*() also allows removing a fair amount of platform specific code for the file locking.

Fixes #161361

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

rustbot commented Sep 10, 2026

Copy link
Copy Markdown
Collaborator

r? @adwinwhite

rustbot has assigned @adwinwhite.
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: compiler
  • compiler expanded to 76 candidates
  • Random selection from 22 candidates

@bjorn3

bjorn3 commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

cc @jackpot51 this enables File::*lock*() on Redox OS to avoid regressing it.

@bjorn3
bjorn3 force-pushed the incr_comp_prefer_flock branch from df34325 to 67532d3 Compare September 10, 2026 11:36
And only fallback to fcntl(F_SETLK) on Unix when flock() is not supported.
@bjorn3
bjorn3 force-pushed the incr_comp_prefer_flock branch from 67532d3 to 2fe3696 Compare September 10, 2026 11:38
impl Lock {
pub fn new(p: &Path, wait: bool, create: bool, exclusive: bool) -> io::Result<Lock> {
let mut open_options = OpenOptions::new();
open_options.read(true).write(true).create(create);

@bjorn3 bjorn3 Sep 10, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

On Windows .write(true) was only used when create is true. Seems harmless to me to always set it. On Windows .share_mode() was also used, but with a value matching the default, so I removed that one.

View changes since the review

knka983

This comment was marked as spam.

@knka983

This comment was marked as spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. 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.

Use flock rather than fcntl based locks for incr comp when supported

4 participants