Skip to content

Commit

Permalink
Rollup merge of rust-lang#88310 - worldeva:bootstrap-locking, r=Mark-…
Browse files Browse the repository at this point in the history
…Simulacrum

Lock bootstrap (x.py) build directory

Closes rust-lang#76661,  closes rust-lang#80849,
`x.py` creates a lock file at `project_root/lock.db`

r? ``@jyn514`` , because he was one that told me about this~
  • Loading branch information
matthiaskrgr authored Dec 8, 2021
2 parents 4459e72 + 0a84930 commit 4d93b4e
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/bootstrap/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,38 @@
import sys
import tarfile
import tempfile
import sqlite3

from time import time

# Acquire a lock on the build directory to make sure that
# we don't cause a race condition while building
# Lock is created in `build_dir/lock.db`
def acquire_lock(build_dir):
path = os.path.join(build_dir, "lock.db")
try:
con = sqlite3.Connection(path, timeout=0)
curs = con.cursor()
curs.execute("BEGIN EXCLUSIVE")
# The lock is released when the cursor is dropped
return curs
# If the database is busy then lock has already been acquired
# so we wait for the lock.
# We retry every quarter second so that execution is passed back to python
# so that it can handle signals
except sqlite3.OperationalError:
del con
del curs
print("Waiting for lock on build directory")
con = sqlite3.Connection(path, timeout=0.25)
curs = con.cursor()
while True:
try:
curs.execute("BEGIN EXCLUSIVE")
except sqlite3.OperationalError:
pass
return curs

def support_xz():
try:
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
Expand Down Expand Up @@ -1225,6 +1254,12 @@ def bootstrap(help_triggered):
build.set_dist_environment(data["dist_server"])

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

# Acquire the lock before doing any build actions
# The lock is released when `lock` is dropped
if not os.path.exists(build.build_dir):
os.makedirs(build.build_dir)
lock = acquire_lock(build.build_dir)
build.update_submodules()

# Fetch/build the bootstrap
Expand Down

0 comments on commit 4d93b4e

Please sign in to comment.