From d4ea3ea045a250c86859ebfcff953b13822f0a23 Mon Sep 17 00:00:00 2001 From: kssenii Date: Tue, 16 May 2023 13:49:32 +0200 Subject: [PATCH] Fix --- src/Interpreters/Cache/Metadata.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Interpreters/Cache/Metadata.cpp b/src/Interpreters/Cache/Metadata.cpp index a97b10ffbfa..01cdc7f1d1b 100644 --- a/src/Interpreters/Cache/Metadata.cpp +++ b/src/Interpreters/Cache/Metadata.cpp @@ -1,6 +1,7 @@ #include #include #include +#include "Common/Exception.h" #include #include @@ -257,8 +258,6 @@ void CacheMetadata::doCleanup() } locked_metadata->markAsRemoved(); - erase(it); - LOG_DEBUG(log, "Key {} is removed from metadata", cleanup_key); try { @@ -272,9 +271,17 @@ void CacheMetadata::doCleanup() } catch (...) { - tryLogCurrentException(__PRETTY_FUNCTION__); + LOG_ERROR(log, "Error while removing key {}: {}", cleanup_key, getCurrentExceptionMessage(false)); chassert(false); } + + /// Remove key from metadata AFTER deleting key directory, because otherwise key lock is + /// released before we delete directory from fs and there might be a race: + /// a key, which we just removed, can be added back to cache before we start removing key directory, + /// which makes key directory either non-empty (and we get exception in try catch above) + /// or we removed directory while another thread thinks it exists. + erase(it); + LOG_DEBUG(log, "Key {} is removed from metadata", cleanup_key); } }