Add utility for locking directories - #5039
Merged
Merged
Conversation
This allows clients to achieve a MUTEX for a specific directory. This can be useful for buildnmls that want to make changes to shared directories, allowing them to avoid race conditions.
jgfouca
requested review from
billsacks and
jasonb5
and
a lite review from Copilot
August 25, 2026 16:53
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a new utility context manager in CIME.utils to provide a filesystem-based mutex for a given directory, intended to help clients (e.g., buildnml workflows) avoid race conditions when modifying shared directories.
Changes:
- Replaces the prior file-based “wait for unlocked” helper with a directory-based distributed lock implementation (
distributed_dir_lock). - Adds unit tests covering acquisition/release, exception safety, timeout behavior, retry behavior, and external cleanup edge cases.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| CIME/utils.py | Adds a directory-based lock context manager and removes the old wait_for_unlocked helper. |
| CIME/tests/test_unit_utils.py | Adds unit tests validating the new directory-lock behavior. |
Suppressed comments (1)
CIME/utils.py:2750
- The lock acquisition timeout uses
time.time(), which can jump if the system clock is adjusted (NTP/VM suspend), and the timeout check uses>rather than>=. Also, negativepoll_interval/timeoutwill lead to confusing runtime errors; validate these inputs up front and usetime.monotonic()for elapsed time measurement.
# Check if we have timed out while waiting
if timeout is not None and (time.time() - start_time) > timeout:
raise TimeoutError( # pylint: disable=raise-missing-from
f"Failed to acquire lock at {lock_dir_path} within {timeout} seconds."
)
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
billsacks
approved these changes
Aug 25, 2026
billsacks
left a comment
Member
There was a problem hiding this comment.
I haven't done a careful review, but I like the idea of this addition... I'll defer to others for a detailed review.
bartgol
approved these changes
Aug 25, 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
This allows clients to achieve a MUTEX for a specific directory. This
can be useful for buildnmls that want to make changes to shared
directories, allowing them to avoid race conditions.
Checklist