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

Fix race condition in filesystem bucket client Delete() #5103

Merged
merged 3 commits into from
Feb 1, 2022

Conversation

pracucci
Copy link
Contributor

  • I added CHANGELOG entry for this change.
  • Change is not relevant to the end user.

Changes

I found a race condition in the filesystem bucket client Delete(). The issue happen if 2 concurrent deletes occurs on different files in a subfolder containing only these 2 files. Both Delete() will try to delete the empty dir, but one of the two will fail with:

unexpected error: fdopendir /path/to/subfolder: no such file or directory

In this PR I propose to handle the no such file or directory in the isDirEmpty(). isDirEmpty() is used by Iter() and Delete(). In Iter() it's just used to skip empty dirs, so I think this change is fine there too.

Verification

New unit test.

"github.com/thanos-io/thanos/pkg/testutil"
)

func TestDelete_EmptyDirDeletionRaceCondition(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test fails in main.

GiedriusS
GiedriusS previously approved these changes Jan 31, 2022
Copy link
Member

@GiedriusS GiedriusS left a comment

Choose a reason for hiding this comment

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

LGTM 👍

if os.IsNotExist(err) {
// The directory doesn't exist. We don't consider it an error and we treat it like empty.
return true, nil
} else if err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: since the first if returns, we normally style the second if without an else

Suggested change
} else if err != nil {
}
if err != nil {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, addressed it!

squat
squat previously approved these changes Jan 31, 2022
Copy link
Member

@squat squat left a comment

Choose a reason for hiding this comment

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

Lgtm, one small style nit

Signed-off-by: Marco Pracucci <marco@pracucci.com>
Signed-off-by: Marco Pracucci <marco@pracucci.com>
Signed-off-by: Marco Pracucci <marco@pracucci.com>
@pracucci pracucci dismissed stale reviews from squat and GiedriusS via 11beece February 1, 2022 09:41
@pracucci pracucci force-pushed the fix-filesystem-delete-empty-dir-race-condition branch from 59678f7 to 11beece Compare February 1, 2022 09:41
Copy link
Member

@squat squat left a comment

Choose a reason for hiding this comment

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

thank you @pracucci!!

@squat squat merged commit bb5decb into main Feb 1, 2022
@squat squat deleted the fix-filesystem-delete-empty-dir-race-condition branch February 1, 2022 10:16
Nicholaswang pushed a commit to Nicholaswang/thanos that referenced this pull request Mar 6, 2022
* Fix race condition in filesystem bucket client Delete()

Signed-off-by: Marco Pracucci <marco@pracucci.com>

* Added missing copyright header

Signed-off-by: Marco Pracucci <marco@pracucci.com>

* Addressed review comments

Signed-off-by: Marco Pracucci <marco@pracucci.com>
Signed-off-by: Nicholaswang <wzhever@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants