Skip to content
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

Let cache lock fail after 24h and warn after 2s #498

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

hagenw
Copy link
Member

@hagenw hagenw commented Feb 13, 2025

Closes #497

This reduces the default waiting time for acquiring the folder lock in cache from infinity to 24 h (handled by the timeout argument in audb.load(), audb.load_media(), and audb.stream()). This seems to me a better approach as we have the problem of leftover lock files in shared cache, for which audb.load() was stuck forever before.
It also displays a warning if the lock can not be acquired after 2 s, mentioning that the user might need to delete the lock file manually.

To test it locally, you can try:

import audb
import audeer
import tempfile

with tempfile.TemporaryDirectory() as tmpdir:
    audeer.touch(tmpdir, ".lock")
    with audb.core.lock.FolderLock(tmpdir, timeout=10):
        pass

This first returns after 2 s:

.../audb/core/lock.py:74: UserWarning: Lock could not be acquired immediately.
Another user might loading the same database,
or the lock file '/tmp/tmpwqg8kwx1/.lock' is left from a failed job and needs to be manually deleted.
You can check who created it when by running: 'ls -lh /tmp/tmpwqg8kwx1/.lock' in bash.
Still trying for 8.0 more seconds...

and after additional 8 s it fails with

Timeout: The file lock '/tmp/tmpbxai_wos/.lock' could not be acquired.

Summary by Sourcery

Bug Fixes:

  • Fixed a bug where the cache lock could be held indefinitely if the initial lock acquisition failed.

Copy link
Contributor

sourcery-ai bot commented Feb 13, 2025

Reviewer's Guide by Sourcery

This pull request introduces a timeout mechanism for acquiring folder locks in the cache, preventing indefinite waiting. It sets a default timeout of 24 hours and warns the user after 2 seconds if the lock cannot be acquired, suggesting manual deletion of the lock file if necessary. The load, load_media, and stream functions now utilize this timeout. Additionally, tests have been added to verify the new timeout and warning functionalities.

Sequence diagram for acquiring a folder lock with timeout and warning

sequenceDiagram
  participant User
  participant FolderLock
  participant FileLock

  User->>FolderLock: FolderLock(folders, timeout, warning_timeout)
  FolderLock->>FileLock: SoftFileLock(lock_file)
  User->>FolderLock: __enter__()
  alt warning_timeout < timeout
    FolderLock->>FileLock: acquire(timeout=warning_timeout)
    alt Lock acquired within warning_timeout
      FileLock-->>FolderLock: returns
      FolderLock-->>User: returns
    else Lock not acquired within warning_timeout
      FileLock-->>FolderLock: Timeout
      FolderLock->>User: issues warning
      FolderLock->>FileLock: acquire(timeout=remaining_time)
      FileLock-->>FolderLock: returns
      FolderLock-->>User: returns
    end
  else timeout < warning_timeout
    FolderLock->>FileLock: acquire(timeout=timeout)
    FileLock-->>FolderLock: returns
    FolderLock-->>User: returns
  end
Loading

Updated class diagram for FolderLock

classDiagram
  class FolderLock {
    -folders: str | Sequence[str]
    -timeout: float
    -warning_timeout: float
    -lock_files: list
    -locks: list
    +__init__(folders: str | Sequence[str], timeout: float, warning_timeout: float)
    +__enter__() : FolderLock
    +__exit__()
  }
Loading

File-Level Changes

Change Details Files
Modified the FolderLock class to include a warning timeout and to limit the maximum wait time for acquiring a lock.
  • Added a warning_timeout parameter to the FolderLock class.
  • If the lock cannot be acquired within the warning_timeout, a warning message is displayed to the user.
  • The default waiting time for acquiring the folder lock is now 24 hours.
  • If timeout < warning_timeout, it is automatically set to timeout
  • A warning is issued if a timeout value less than 0 is provided, and the timeout is set to the default value.
audb/core/lock.py
tests/test_lock.py
Updated the load, load_media, and stream functions to use the default timeout value for acquiring a lock.
  • The timeout parameter in audb.load(), audb.load_media(), and audb.stream() now defaults to define.TIMEOUT.
  • The description of the timeout parameter was updated to specify that it is in seconds.
audb/core/load.py
audb/core/stream.py
Added tests for the deprecated timeout argument and the warning/failure messages.
  • Added test_lock_load_deprecated_timeout to test the timeout < 0 argument.
  • Added test_lock_warning_and_failure to test the user warning and lock failure messages.
tests/test_lock_db.py
tests/test_lock.py
Defined a default timeout value of 24 hours for cache locks.
  • Added a TIMEOUT constant to audb/core/define.py with a value of 86400 (24 hours).
audb/core/define.py

Assessment against linked issues

Issue Objective Addressed Explanation
#497 Display a warning message if the lock could not be acquired in 1 second, indicating potential issues like another user downloading the same data or a leftover .lock file.
#497 Limit the waiting time for acquiring the folder lock to prevent indefinite blocking.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @hagenw - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a brief explanation in the description why the default timeout was reduced to 24 hours.
  • The warning message in FolderLock is very long; consider shortening it or breaking it into multiple lines for better readability.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

accessing the database. If timeout is reached, ``None`` is
returned. If timeout < 0 the method will block until the
database can be accessed
timeout: maximum wait time in seconds
Copy link
Member

Choose a reason for hiding this comment

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

Would you mention what is being waited for? My understanding is that you want to acquire the lock yourself and are willing to wait for timeout before you give up.

Copy link
Member Author

Choose a reason for hiding this comment

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

The full text is:

        timeout: maximum wait time in seconds
            if another thread or process is already
            accessing the database.
            If timeout is reached,
            ``None`` is returned

So, we are waiting that we can get access to the database. This needs to be blocked if another user is currently accessing it, as this means the cache might be not fully filled yet.

What would be your suggestion as additional text?

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.

Show warning if no lock can be aquired
2 participants