Skip to content

Commit

Permalink
Rollup merge of #72161 - nbdd0121:master, r=cuviper
Browse files Browse the repository at this point in the history
Replace fcntl-based file lock with flock

WSL1 does not support `fcntl`-based lock and will always report success,
therefore creating a race condition when multiple rustc processes are
modifying shared data such as `search-index.js`. WSL1 does however
support `flock`.

`flock` is supported by all unix-like platforms. The only caveat is that
Linux 2.6.11 or earlier does not support `flock` on NFS mounts, but as
the minimum supported Linux version is 2.6.18, it is not an issue.

Fixes #72157
  • Loading branch information
RalfJung authored May 22, 2020
2 parents 2059112 + a05acbf commit a8018e2
Showing 1 changed file with 57 additions and 24 deletions.
81 changes: 57 additions & 24 deletions src/librustc_data_structures/flock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@
#![allow(non_camel_case_types)]
#![allow(nonstandard_style)]

use std::fs::{File, OpenOptions};
use std::io;
use std::path::Path;

cfg_if! {
if #[cfg(unix)] {
use std::ffi::{CString, OsStr};
use std::mem;
// We use `flock` rather than `fcntl` on Linux, because WSL1 does not support
// `fcntl`-style advisory locks properly (rust-lang/rust#72157).
//
// For other Unix targets we still use `fcntl` because it's more portable than
// `flock`.
if #[cfg(target_os = "linux")] {
use std::os::unix::prelude::*;

#[derive(Debug)]
pub struct Lock {
fd: libc::c_int,
_file: File,
}

impl Lock {
Expand All @@ -27,22 +31,55 @@ cfg_if! {
create: bool,
exclusive: bool)
-> io::Result<Lock> {
let os: &OsStr = p.as_ref();
let buf = CString::new(os.as_bytes()).unwrap();
let open_flags = if create {
libc::O_RDWR | libc::O_CREAT
let file = OpenOptions::new()
.read(true)
.write(true)
.create(create)
.mode(libc::S_IRWXU as u32)
.open(p)?;

let mut operation = if exclusive {
libc::LOCK_EX
} else {
libc::O_RDWR
};

let fd = unsafe {
libc::open(buf.as_ptr(), open_flags,
libc::S_IRWXU as libc::c_int)
libc::LOCK_SH
};
if !wait {
operation |= libc::LOCK_NB
}

if fd < 0 {
return Err(io::Error::last_os_error());
let ret = unsafe { libc::flock(file.as_raw_fd(), operation) };
if ret == -1 {
Err(io::Error::last_os_error())
} else {
Ok(Lock { _file: file })
}
}
}

// Note that we don't need a Drop impl to execute `flock(fd, LOCK_UN)`. Lock acquired by
// `flock` is associated with the file descriptor and closing the file release it
// automatically.
} else if #[cfg(unix)] {
use std::mem;
use std::os::unix::prelude::*;

#[derive(Debug)]
pub struct Lock {
file: File,
}

impl Lock {
pub fn new(p: &Path,
wait: bool,
create: bool,
exclusive: bool)
-> io::Result<Lock> {
let file = OpenOptions::new()
.read(true)
.write(true)
.create(create)
.mode(libc::S_IRWXU as u32)
.open(p)?;

let lock_type = if exclusive {
libc::F_WRLCK
Expand All @@ -58,14 +95,12 @@ cfg_if! {

let cmd = if wait { libc::F_SETLKW } else { libc::F_SETLK };
let ret = unsafe {
libc::fcntl(fd, cmd, &flock)
libc::fcntl(file.as_raw_fd(), cmd, &flock)
};
if ret == -1 {
let err = io::Error::last_os_error();
unsafe { libc::close(fd); }
Err(err)
Err(io::Error::last_os_error())
} else {
Ok(Lock { fd })
Ok(Lock { file })
}
}
}
Expand All @@ -79,15 +114,13 @@ cfg_if! {
flock.l_len = 0;

unsafe {
libc::fcntl(self.fd, libc::F_SETLK, &flock);
libc::close(self.fd);
libc::fcntl(self.file.as_raw_fd(), libc::F_SETLK, &flock);
}
}
}
} else if #[cfg(windows)] {
use std::mem;
use std::os::windows::prelude::*;
use std::fs::{File, OpenOptions};

use winapi::um::minwinbase::{OVERLAPPED, LOCKFILE_FAIL_IMMEDIATELY, LOCKFILE_EXCLUSIVE_LOCK};
use winapi::um::fileapi::LockFileEx;
Expand Down

0 comments on commit a8018e2

Please sign in to comment.