Skip to content

Commit 3613dfd

Browse files
committed
ngclient: Move bootstrap root loading inside lock
Otherwise another process might delete the file underneath us Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
1 parent c5df98a commit 3613dfd

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

tuf/ngclient/updater.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
try:
7979
# advisory file locking for posix
8080
import fcntl
81-
8281
def _lock_file(f: IO) -> None:
8382
if f.writable():
8483
fcntl.lockf(f, fcntl.LOCK_EX)
@@ -92,6 +91,7 @@ def _lock_file(f: IO) -> None:
9291
f.write(b"\0")
9392
f.flush()
9493
f.seek(0)
94+
9595
msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, 1)
9696

9797

@@ -152,18 +152,19 @@ def __init__(
152152
f"got '{self.config.envelope_type}'"
153153
)
154154

155-
if not bootstrap:
156-
# if no root was provided, use the cached non-versioned root.json
157-
bootstrap = self._load_local_metadata(Root.type)
158-
159-
# Load the initial root, make sure it's cached
160-
self._trusted_set = TrustedMetadataSet(
161-
bootstrap, self.config.envelope_type
162-
)
163155
with self._lock_metadata():
156+
if not bootstrap:
157+
# if no root was provided, use the cached non-versioned root.json
158+
bootstrap = self._load_local_metadata(Root.type)
159+
160+
# Load the initial root, make sure it's cached
161+
self._trusted_set = TrustedMetadataSet(
162+
bootstrap, self.config.envelope_type
163+
)
164164
self._persist_root(self._trusted_set.root.version, bootstrap)
165165
self._update_root_symlink()
166166

167+
167168
@contextlib.contextmanager
168169
def _lock_metadata(self) -> Iterator[None]:
169170
"""Context manager for locking the metadata directory."""

0 commit comments

Comments
 (0)