Skip to content

cache: fix race condition in cache cleanup or similar.#5047

Merged
MichaelEischer merged 1 commit intorestic:masterfrom
damoclark:patch-1
Sep 14, 2024
Merged

cache: fix race condition in cache cleanup or similar.#5047
MichaelEischer merged 1 commit intorestic:masterfrom
damoclark:patch-1

Conversation

@damoclark
Copy link
Copy Markdown
Contributor

@damoclark damoclark commented Sep 10, 2024

Fix for multiple restic processes executing concurrently and racing to remove obsolete snapshots from the local backend cache.

What does this PR change? What problem does it solve?

This change tests the error returned from the closure/anonymous function, and if the error is that the given file does not exist, then skip the missing file in the results by returning nil.

The purpose is to silence the error generated from a race condition where multiple restic processes are executing concurrently and one unlinks a snapshot file, after another retrieves the file's path from a readdir, but before it attempts an lstat. It is in response to the following error message:

error clearing snapshot files in cache: Walk: lstat /home/<username>/.cache/restic/23fa6bedac5c6d4...a6079eea2b3e3/snapshots/3f/3f4e3de8f8...38f8de1c995e3d: no such file or directory

Was the change previously discussed in an issue or on the forum?

I didn't raise an issue, as its quite trivial. It is related to #4760, that was resolved in an identical way.

I'm not a go developer, so please review this PR carefully. I modelled the patch on #4760 but may have made errors.

Checklist

  • I have read the contribution guidelines.
  • I have enabled maintainer edits.
  • I have added tests for all code changes.
  • I have added documentation for relevant changes (in the manual).
  • There's a new file in changelog/unreleased/ that describes the changes for our users (see template).
  • I have run gofmt on the code in all commits.
  • All commit messages are formatted in the same style as the other commits in the repo.
  • I'm done! This pull request is ready for review.

err := filepath.Walk(dir, func(name string, fi os.FileInfo, err error) error {
if err != nil {
// ignore ErrNotExist to gracefully handle multiple processes clearing the cache
if(errors.Is(err, os.ErrNotExist)
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.

the brackets are somewhat messed up. It should be

if errors.Is(err, os.ErrNotExist) {
    return nil
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oops, missed the closing brace. Sorry about that. Made the change directly on github as I don't have a dev environment for Go. Would have benefited from some IDE syntax highlighting there. :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've fixed the syntax error.

Let me know if anything else is needed.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

__

@MichaelEischer
Copy link
Copy Markdown
Member

Thanks for the bugfix. Can you add a minimal changelog entry? (or should I take that over?)

@damoclark damoclark changed the title Backend cache race condition fix cache: fix race condition in cache cleanup or similar. Sep 12, 2024
@damoclark
Copy link
Copy Markdown
Contributor Author

Thanks for the bugfix. Can you add a minimal changelog entry? (or should I take that over?)

No problem. I had a go with the changelog entry. I think it is correct.

D.

Fix multiple restic processes executing concurrently and racing to remove obsolete snapshots.

Co-authored-by: Michael Eischer <michael.eischer@fau.de>
Copy link
Copy Markdown
Member

@MichaelEischer MichaelEischer left a comment

Choose a reason for hiding this comment

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

LGTM. I've taken the liberty to fix the remaining syntax error and squashed all commits.

@MichaelEischer MichaelEischer added this pull request to the merge queue Sep 14, 2024
Merged via the queue into restic:master with commit 4105e4a Sep 14, 2024
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.

3 participants