Skip to content

Commit

Permalink
Backport #51908 to 23.5: Fix deadlock on DatabaseCatalog shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
robot-clickhouse committed Aug 29, 2023
1 parent be71693 commit 46dd018
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Interpreters/DatabaseCatalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ namespace ErrorCodes
extern const int DATABASE_ACCESS_DENIED;
extern const int LOGICAL_ERROR;
extern const int HAVE_DEPENDENT_OBJECTS;
extern const int UNFINISHED;
}

TemporaryTableHolder::TemporaryTableHolder(ContextPtr context_, const TemporaryTableHolder::Creator & creator, const ASTPtr & query)
Expand Down Expand Up @@ -196,6 +197,9 @@ void DatabaseCatalog::startupBackgroundCleanup()

void DatabaseCatalog::shutdownImpl()
{
is_shutting_down = true;
wait_table_finally_dropped.notify_all();

if (cleanup_task)
(*cleanup_task)->deactivate();

Expand Down Expand Up @@ -1153,8 +1157,13 @@ void DatabaseCatalog::waitTableFinallyDropped(const UUID & uuid)
std::unique_lock lock{tables_marked_dropped_mutex};
wait_table_finally_dropped.wait(lock, [&]() TSA_REQUIRES(tables_marked_dropped_mutex) -> bool
{
return !tables_marked_dropped_ids.contains(uuid);
return !tables_marked_dropped_ids.contains(uuid) || is_shutting_down;
});

/// TSA doesn't support unique_lock
if (TSA_SUPPRESS_WARNING_FOR_READ(tables_marked_dropped_ids).contains(uuid))
throw Exception(ErrorCodes::UNFINISHED, "Did not finish dropping the table with UUID {} because the server is shutting down, "
"will finish after restart", uuid);
}

void DatabaseCatalog::addDependencies(
Expand Down
2 changes: 2 additions & 0 deletions src/Interpreters/DatabaseCatalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ class DatabaseCatalog : boost::noncopyable, WithMutableContext

Poco::Logger * log;

std::atomic_bool is_shutting_down = false;

/// Do not allow simultaneous execution of DDL requests on the same table.
/// database name -> database guard -> (table name mutex, counter),
/// counter: how many threads are running a query on the table at the same time
Expand Down

0 comments on commit 46dd018

Please sign in to comment.