Skip to content

Commit 0e21a27

Browse files
committed
Auto merge of #98373 - joshtriplett:bootstrap-locking, r=jyn514
Move locking from bootstrap.py to rust bootstrap, using fd-lock Helps with #94829.
2 parents b04bfb4 + 5a30316 commit 0e21a27

File tree

6 files changed

+123
-45
lines changed

6 files changed

+123
-45
lines changed

src/bootstrap/Cargo.lock

+102
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ version = "0.0.0"
4747
dependencies = [
4848
"cc",
4949
"cmake",
50+
"fd-lock",
5051
"filetime",
5152
"getopts",
5253
"hex",
@@ -201,6 +202,38 @@ version = "1.6.1"
201202
source = "registry+https://github.com/rust-lang/crates.io-index"
202203
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
203204

205+
[[package]]
206+
name = "errno"
207+
version = "0.2.8"
208+
source = "registry+https://github.com/rust-lang/crates.io-index"
209+
checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1"
210+
dependencies = [
211+
"errno-dragonfly",
212+
"libc",
213+
"winapi",
214+
]
215+
216+
[[package]]
217+
name = "errno-dragonfly"
218+
version = "0.1.2"
219+
source = "registry+https://github.com/rust-lang/crates.io-index"
220+
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
221+
dependencies = [
222+
"cc",
223+
"libc",
224+
]
225+
226+
[[package]]
227+
name = "fd-lock"
228+
version = "3.0.6"
229+
source = "registry+https://github.com/rust-lang/crates.io-index"
230+
checksum = "e11dcc7e4d79a8c89b9ab4c6f5c30b1fc4a83c420792da3542fd31179ed5f517"
231+
dependencies = [
232+
"cfg-if",
233+
"rustix",
234+
"windows-sys",
235+
]
236+
204237
[[package]]
205238
name = "filetime"
206239
version = "0.2.16"
@@ -284,6 +317,12 @@ dependencies = [
284317
"winapi-util",
285318
]
286319

320+
[[package]]
321+
name = "io-lifetimes"
322+
version = "0.7.2"
323+
source = "registry+https://github.com/rust-lang/crates.io-index"
324+
checksum = "24c3f4eff5495aee4c0399d7b6a0dc2b6e81be84242ffbfcf253ebacccc1d0cb"
325+
287326
[[package]]
288327
name = "itoa"
289328
version = "1.0.2"
@@ -302,6 +341,12 @@ version = "0.2.126"
302341
source = "registry+https://github.com/rust-lang/crates.io-index"
303342
checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
304343

344+
[[package]]
345+
name = "linux-raw-sys"
346+
version = "0.0.46"
347+
source = "registry+https://github.com/rust-lang/crates.io-index"
348+
checksum = "d4d2456c373231a208ad294c33dc5bff30051eafd954cd4caae83a712b12854d"
349+
305350
[[package]]
306351
name = "log"
307352
version = "0.4.17"
@@ -473,6 +518,20 @@ version = "0.6.26"
473518
source = "registry+https://github.com/rust-lang/crates.io-index"
474519
checksum = "49b3de9ec5dc0a3417da371aab17d729997c15010e7fd24ff707773a33bddb64"
475520

521+
[[package]]
522+
name = "rustix"
523+
version = "0.35.6"
524+
source = "registry+https://github.com/rust-lang/crates.io-index"
525+
checksum = "ef258c11e17f5c01979a10543a30a4e12faef6aab217a74266e747eefa3aed88"
526+
dependencies = [
527+
"bitflags",
528+
"errno",
529+
"io-lifetimes",
530+
"libc",
531+
"linux-raw-sys",
532+
"windows-sys",
533+
]
534+
476535
[[package]]
477536
name = "ryu"
478537
version = "1.0.10"
@@ -657,6 +716,49 @@ version = "0.4.0"
657716
source = "registry+https://github.com/rust-lang/crates.io-index"
658717
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
659718

719+
[[package]]
720+
name = "windows-sys"
721+
version = "0.36.1"
722+
source = "registry+https://github.com/rust-lang/crates.io-index"
723+
checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2"
724+
dependencies = [
725+
"windows_aarch64_msvc",
726+
"windows_i686_gnu",
727+
"windows_i686_msvc",
728+
"windows_x86_64_gnu",
729+
"windows_x86_64_msvc",
730+
]
731+
732+
[[package]]
733+
name = "windows_aarch64_msvc"
734+
version = "0.36.1"
735+
source = "registry+https://github.com/rust-lang/crates.io-index"
736+
checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47"
737+
738+
[[package]]
739+
name = "windows_i686_gnu"
740+
version = "0.36.1"
741+
source = "registry+https://github.com/rust-lang/crates.io-index"
742+
checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6"
743+
744+
[[package]]
745+
name = "windows_i686_msvc"
746+
version = "0.36.1"
747+
source = "registry+https://github.com/rust-lang/crates.io-index"
748+
checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024"
749+
750+
[[package]]
751+
name = "windows_x86_64_gnu"
752+
version = "0.36.1"
753+
source = "registry+https://github.com/rust-lang/crates.io-index"
754+
checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1"
755+
756+
[[package]]
757+
name = "windows_x86_64_msvc"
758+
version = "0.36.1"
759+
source = "registry+https://github.com/rust-lang/crates.io-index"
760+
checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680"
761+
660762
[[package]]
661763
name = "xattr"
662764
version = "0.2.3"

src/bootstrap/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ test = false
3636

3737
[dependencies]
3838
cmake = "0.1.38"
39+
fd-lock = "3.0.6"
3940
filetime = "0.2"
4041
num_cpus = "1.0"
4142
getopts = "0.2.19"

src/bootstrap/bin/main.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,28 @@
77
88
use std::env;
99

10-
use bootstrap::{Build, Config, Subcommand, VERSION};
10+
use bootstrap::{t, Build, Config, Subcommand, VERSION};
1111

1212
fn main() {
1313
let args = env::args().skip(1).collect::<Vec<_>>();
1414
let config = Config::parse(&args);
1515

16+
let mut build_lock;
17+
let _build_lock_guard;
18+
if cfg!(any(unix, windows)) {
19+
build_lock = fd_lock::RwLock::new(t!(std::fs::File::create(config.out.join("lock"))));
20+
_build_lock_guard = match build_lock.try_write() {
21+
Ok(lock) => lock,
22+
err => {
23+
println!("warning: build directory locked, waiting for lock");
24+
drop(err);
25+
t!(build_lock.write())
26+
}
27+
};
28+
} else {
29+
println!("warning: file locking not supported for target, not locking build directory");
30+
}
31+
1632
// check_version warnings are not printed during setup
1733
let changelog_suggestion =
1834
if matches!(config.cmd, Subcommand::Setup { .. }) { None } else { check_version(&config) };

src/bootstrap/bootstrap.py

-41
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,6 @@
1515

1616
from time import time, sleep
1717

18-
# Acquire a lock on the build directory to make sure that
19-
# we don't cause a race condition while building
20-
# Lock is created in `build_dir/lock.db`
21-
def acquire_lock(build_dir):
22-
try:
23-
import sqlite3
24-
25-
path = os.path.join(build_dir, "lock.db")
26-
try:
27-
con = sqlite3.Connection(path, timeout=0)
28-
curs = con.cursor()
29-
curs.execute("BEGIN EXCLUSIVE")
30-
# The lock is released when the cursor is dropped
31-
return curs
32-
# If the database is busy then lock has already been acquired
33-
# so we wait for the lock.
34-
# We retry every quarter second so that execution is passed back to python
35-
# so that it can handle signals
36-
except sqlite3.OperationalError:
37-
del con
38-
del curs
39-
print("Waiting for lock on build directory")
40-
con = sqlite3.Connection(path, timeout=0.25)
41-
curs = con.cursor()
42-
while True:
43-
try:
44-
curs.execute("BEGIN EXCLUSIVE")
45-
break
46-
except sqlite3.OperationalError:
47-
pass
48-
sleep(0.25)
49-
return curs
50-
except ImportError:
51-
print("warning: sqlite3 not available in python, skipping build directory lock")
52-
print("please file an issue on rust-lang/rust")
53-
print("this is not a problem for non-concurrent x.py invocations")
54-
return None
55-
5618
def support_xz():
5719
try:
5820
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
@@ -928,11 +890,8 @@ def bootstrap(help_triggered):
928890

929891
build.build = args.build or build.build_triple()
930892

931-
# Acquire the lock before doing any build actions
932-
# The lock is released when `lock` is dropped
933893
if not os.path.exists(build.build_dir):
934894
os.makedirs(build.build_dir)
935-
lock = acquire_lock(build.build_dir)
936895

937896
# Fetch/build the bootstrap
938897
build.download_toolchain()

src/bootstrap/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ use once_cell::sync::OnceCell;
118118
use crate::builder::Kind;
119119
use crate::config::{LlvmLibunwind, TargetSelection};
120120
use crate::util::{
121-
check_run, exe, libdir, mtime, output, run, run_suppressed, t, try_run, try_run_suppressed,
122-
CiEnv,
121+
check_run, exe, libdir, mtime, output, run, run_suppressed, try_run, try_run_suppressed, CiEnv,
123122
};
124123

125124
mod builder;

src/bootstrap/util.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::config::{Config, TargetSelection};
2222
///
2323
/// This is currently used judiciously throughout the build system rather than
2424
/// using a `Result` with `try!`, but this may change one day...
25+
#[macro_export]
2526
macro_rules! t {
2627
($e:expr) => {
2728
match $e {
@@ -37,7 +38,7 @@ macro_rules! t {
3738
}
3839
};
3940
}
40-
pub(crate) use t;
41+
pub use t;
4142

4243
/// Given an executable called `name`, return the filename for the
4344
/// executable for a particular target.

0 commit comments

Comments
 (0)