-
Notifications
You must be signed in to change notification settings - Fork 279
Advisory locking for metadata and artifacts files #2861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
This likely fails on all platforms right now, but the Windows behaviour cannot be fixed without actual locking. Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
Use get_targetinfo() so that the delegated role loading is tested as well Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
This should prevent issues with multiple processes trying to write at same time. Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
Otherwise another process might delete the file underneath us Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
There does not seem to be a way around a ugly loop over open()... Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
The file locking should make multiple processes safe Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
Will take a look next week
Jussi Kukkonen ***@***.***> schrieb am Fr. 29. Aug. 2025 um
10:05:
… @jku <https://github.com/jku> requested review from
@theupdateframework/python-tuf-maintainers on: #2861
<#2861> Advisory
locking for metadata and artifacts files as a code owner.
—
Reply to this email directly, view it on GitHub
<#2861 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAEP4DGGY4A3AFPOCIZH46L3QACWJAVCNFSM6AAAAACFDY6DBSVHI2DSMVQWIX3LMV45UABCJFZXG5LFIV3GK3TUJZXXI2LGNFRWC5DJN5XDWMJZGQYDMNRUGQ4TQNA>
.
You are receiving this because your review was requested.Message ID:
***@***.***
com>
|
On Windows I get a lot of sequences of the errors shown below. It looks like one process gets to lock the file in each one of the test loops but the other ones all have to wait for quite a while until another one starts locking for a while then. How many files are you locking so that the Windows error 'Resource deadlock avoided' would be justified? I see only one file reported as being cause for 'a deadlock'. Other than that I see no errors, so that's good.
|
Update... All(!!!) 3 of the 3 test processes terminated like this:
|
yield f | ||
return | ||
except FileNotFoundError: | ||
# could be from yield or from open() -- either way we bail |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are opening the file for wring. Should this ever lead to a FileNotFoundError?
Add advisory file locking to make it safer to run multiple updaters (using the same metadata directory) in separate processes. This should help with #2836
<METADATA_DIR>/.lock
: this lock is used inUpdater.__init__()
,Updater.refresh()
andUpdater.get_targetinfo()
-- lock is typically held through the whole method (in other words while the metadata is being downloaded as well)@stefanberger comments welcome
Implementation notes
The implementation has just enough platform specific complexity that I'm not totally happy with it. There's clearly a chance of bugs here... Unfortunately I'm not convinced using a dependency would make that possibility go away
mscvrt.locking()
is the recommended API but 99% of the time it's not useful because we need a file handle to call it, and we can't get one because another process already has the file open. So we need a dumb loop that keeps callingopen()
andsleep()
. It's ugly but no-one seems to have a better solutionfcntl.lockf()
until the lock opens. Both have a tradeoff: On windows the updater might fail if another updater is a bit slow. On posix, if one updater hangs with a lock for some reason, all future updaters will just wait indefinitely until the original hung process is killedTODO
Alternatives
https://github.com/tox-dev/filelock/ looks reasonable -- although artifact locking will be either more complicated or less parallel since you can't use filelock on the actual file you want to write.