Skip to content

fix: keep model download lock PID when acquisition fails - #2362

Merged
jaluma merged 1 commit into
zylon-ai:mainfrom
MaxFreedomPollard:fix/lock-file-pid-truncation
Sep 8, 2026
Merged

jaluma merged 1 commit into
zylon-ai:mainfrom
MaxFreedomPollard:fix/lock-file-pid-truncation

Conversation

@MaxFreedomPollard

Copy link
Copy Markdown
Contributor

Description

FileLock coordinates model downloads across pods and publishes the holder's PID into .model-download.lock so an operator can see which pod is downloading. __enter__ opens that file with open(self.lock_file, "w") at private_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 TimeoutError out 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_TIMEOUT defaults 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

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Added new unit/integration tests
  • I stared at the code and made sure it makes sense

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_pid holds the lock, lets a second FileLock time out, and asserts the holder's PID is still in the file. test_failed_acquisition_closes_lock_file asserts the timed out waiter left no open handle. Both use timeout=0 so they fail on the first attempt and add no wall clock time.

Both tests fail on main and pass with the fix. Running them against the unmodified model_discovery.py, with only the test file changed:

$ python -m pytest tests/components/llm/tokenizers/models/test_concurrency.py::TestFileLocking -q
FAILED tests/components/llm/tokenizers/models/test_concurrency.py::TestFileLocking::test_failed_acquisition_keeps_holder_pid
FAILED tests/components/llm/tokenizers/models/test_concurrency.py::TestFileLocking::test_failed_acquisition_closes_lock_file
2 failed, 3 passed in 5.18s

The first failure is assert '22380' in '': the lock file was empty after the waiter timed out. The second is the waiter's _fd still being an open TextIOWrapper in mode w.

With the fix:

$ python -m pytest tests/components/llm/tokenizers/models -q
62 passed in 5.13s

$ make format
894 files already formatted

$ make lint
All checks passed!

$ make typecheck
All checks passed!

make test on the full suite gives 2357 passed, 1 skipped, 9 failed. All 9 failures are in tests/components/multimodality/test_describe_audio.py and reproduce identically on main at 0658140 (fix: tabular sandbox (#2359)) with the same assert mock_llm.call_count == 6 mismatch against a local ffmpeg, so they are unrelated to this change.

Test Configuration:

  • OS: macOS 26.2 (arm64)
  • Python: 3.11.9
  • uv: 0.11.2

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I ran make check; make test to ensure mypy and tests pass

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
jaluma merged commit 9e9ba08 into zylon-ai:main Sep 8, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants