Repository navigation
fix: keep model download lock PID when acquisition fails - #2362
Merged
jaluma merged 1 commit intoSep 8, 2026
Merged
Conversation
FileLock.__enter__ opened the lock file with open(self.lock_file, "w") at private_gpt/components/llm/tokenizers/models/model_discovery.py:150, before the lock was acquired. "w" truncates, so a process that was only waiting for the lock erased the PID the current holder had written at line 155, and .model-download.lock stayed empty for the rest of the download. The same handle leaked on the timeout path. The retry loop raises TimeoutError out of __enter__, and Python only calls __exit__ once __enter__ has returned, so the file object was never closed. An interrupt during the wait, which defaults to LOCK_TIMEOUT of 3600 seconds, leaked it the same way. Open the file in append mode so it is created without being truncated, truncate and write the PID only after _acquire() succeeds, and close the handle when acquisition fails or is interrupted. The content written once the lock is held is unchanged.
jaluma
approved these changes
Sep 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
FileLockcoordinates model downloads across pods and publishes the holder's PID into.model-download.lockso an operator can see which pod is downloading.__enter__opens that file withopen(self.lock_file, "w")atprivate_gpt/components/llm/tokenizers/models/model_discovery.py:150, before the lock is acquired."w"truncates, so a second pod that is only waiting for the lock erases the PID the holder wrote at line 155, and the file stays empty for the rest of the download. The Windows locking change in #2352 (fix: support Windows model download locking) deliberately placed the Windows lock byte at offset 4096 to keep that PID readable while the lock is held; the truncating open removes it anyway, on every platform.The same handle leaks. When the retry loop gives up it raises
TimeoutErrorout of__enter__, and Python only calls__exit__once__enter__has returned, so the file object opened at line 150 is never closed. An interrupt during the wait leaks it the same way, and that wait is long:LOCK_TIMEOUTdefaults to 3600 seconds.This change opens the file in append mode, which creates it without truncating, moves the truncate and PID write into
_write_pid()called only after_acquire()succeeds, and closes the handle when acquisition fails or is interrupted. What the file contains once the lock is held is unchanged, including when the previous holder's PID was longer than the new one.Type of Change
How Has This Been Tested?
Two regression tests were added to
tests/components/llm/tokenizers/models/test_concurrency.py::TestFileLocking, next to the existing lock tests.test_failed_acquisition_keeps_holder_pidholds the lock, lets a secondFileLocktime out, and asserts the holder's PID is still in the file.test_failed_acquisition_closes_lock_fileasserts the timed out waiter left no open handle. Both usetimeout=0so they fail on the first attempt and add no wall clock time.Both tests fail on
mainand pass with the fix. Running them against the unmodifiedmodel_discovery.py, with only the test file changed:The first failure is
assert '22380' in '': the lock file was empty after the waiter timed out. The second is the waiter's_fdstill being an openTextIOWrapperin modew.With the fix:
make teston the full suite gives 2357 passed, 1 skipped, 9 failed. All 9 failures are intests/components/multimodality/test_describe_audio.pyand reproduce identically onmainat 0658140 (fix: tabular sandbox (#2359)) with the sameassert mock_llm.call_count == 6mismatch against a local ffmpeg, so they are unrelated to this change.Test Configuration:
Checklist:
make check; make testto ensure mypy and tests pass