Skip to content

Add utility for locking directories - #5039

Merged
jgfouca merged 5 commits into
masterfrom
jgfouca/dir_lock_util
Aug 25, 2026
Merged

Add utility for locking directories#5039
jgfouca merged 5 commits into
masterfrom
jgfouca/dir_lock_util

Conversation

@jgfouca

@jgfouca jgfouca commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

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

  • My code follows the style guidelines of this project (black formatting)
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • I have added tests that exercise my feature/fix and existing tests continue to pass
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding additions and changes to the documentation

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
jgfouca requested review from billsacks and jasonb5 and a lite review from Copilot August 25, 2026 16:53
@jgfouca jgfouca self-assigned this Aug 25, 2026
@jgfouca
jgfouca requested a review from bartgol August 25, 2026 16:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, negative poll_interval / timeout will lead to confusing runtime errors; validate these inputs up front and use time.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.

Comment thread CIME/utils.py
Comment thread CIME/tests/test_unit_utils.py

@billsacks billsacks left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't done a careful review, but I like the idea of this addition... I'll defer to others for a detailed review.

@jgfouca
jgfouca merged commit 2c4d0c7 into master Aug 25, 2026
10 checks passed
@jgfouca
jgfouca deleted the jgfouca/dir_lock_util branch August 25, 2026 19:54
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.

4 participants