diff options
author | arkady-e1ppa <arkady-e1ppa@yandex-team.com> | 2024-05-15 17:10:29 +0300 |
---|---|---|
committer | arkady-e1ppa <arkady-e1ppa@yandex-team.com> | 2024-05-15 17:36:14 +0300 |
commit | bb793583d8882d549c2ff79010b300c33bc0c8eb (patch) | |
tree | 6e70f23af90d4a6e9487147e16e59e757f787679 | |
parent | a6e62482c25dd183da6d34236ee1ad6c3d5fc42e (diff) | |
download | ydb-bb793583d8882d549c2ff79010b300c33bc0c8eb.tar.gz |
YT-21434: Make sure system invokers are active before shutdown
1d45d044e90ad310e6e002f8446320e56dcdf6fb
-rw-r--r-- | yt/yt/core/concurrency/fiber_scheduler_thread.cpp | 8 | ||||
-rw-r--r-- | yt/yt/core/misc/shutdown.cpp | 13 | ||||
-rw-r--r-- | yt/yt/core/misc/shutdown.h | 11 |
3 files changed, 32 insertions, 0 deletions
diff --git a/yt/yt/core/concurrency/fiber_scheduler_thread.cpp b/yt/yt/core/concurrency/fiber_scheduler_thread.cpp index 03a292e3b3..9ee9f7492e 100644 --- a/yt/yt/core/concurrency/fiber_scheduler_thread.cpp +++ b/yt/yt/core/concurrency/fiber_scheduler_thread.cpp @@ -897,6 +897,10 @@ private: //////////////////////////////////////////////////////////////////////////////// +std::atomic<bool> ShutdownManagerPrepared_ = false; + +//////////////////////////////////////////////////////////////////////////////// + TFiberSchedulerThread::TFiberSchedulerThread( TString threadGroupName, TString threadName, @@ -911,6 +915,10 @@ void TFiberSchedulerThread::ThreadMain() // Hold this strongly. auto this_ = MakeStrong(this); + if (!ShutdownManagerPrepared_.exchange(true, std::memory_order::relaxed)) { + EnsureSafeShutdown(); + } + try { YT_LOG_DEBUG("Thread started (Name: %v)", GetThreadName()); diff --git a/yt/yt/core/misc/shutdown.cpp b/yt/yt/core/misc/shutdown.cpp index d509350c07..92a231f870 100644 --- a/yt/yt/core/misc/shutdown.cpp +++ b/yt/yt/core/misc/shutdown.cpp @@ -1,5 +1,7 @@ #include "shutdown.h" +#include <yt/yt/core/concurrency/system_invokers.h> + #include <yt/yt/core/misc/collection_helpers.h> #include <yt/yt/core/misc/proc.h> #include <yt/yt/core/misc/singleton.h> @@ -177,6 +179,12 @@ public: return ShutdownThreadId_.load(); } + void EnsureSystemInvokersRunning() const + { + NConcurrency::GetFinalizerInvoker(); + NConcurrency::GetShutdownInvoker(); + } + private: std::atomic<FILE*> ShutdownLogFile_ = IsShutdownLoggingEnabledImpl() ? stderr : nullptr; @@ -275,6 +283,11 @@ size_t GetShutdownThreadId() return TShutdownManager::Get()->GetShutdownThreadId(); } +void EnsureSafeShutdown() +{ + TShutdownManager::Get()->EnsureSystemInvokersRunning(); +} + //////////////////////////////////////////////////////////////////////////////// static const void* ShutdownGuardInitializer = [] { diff --git a/yt/yt/core/misc/shutdown.h b/yt/yt/core/misc/shutdown.h index 9fdb86f547..88d02de08d 100644 --- a/yt/yt/core/misc/shutdown.h +++ b/yt/yt/core/misc/shutdown.h @@ -76,6 +76,17 @@ FILE* TryGetShutdownLogFile(); //! the id of the thread invoking shutdown callbacks. size_t GetShutdownThreadId(); +//! Some actions that are required for proper shutdown +//! may only happen during the shutdown when they are +//! no longer safe to be executed, e.g. system invokers +//! creation. +//! Call this method before |Shutdown| to make sure everything is going to work safely. +//! This method can be called multiple times. No-op after the first call. +//! If you encounter build timeout during codegen phase or something similar, +//! try calling any other method from this header prior as it would break +//! any possible recursive behaviors of static variables. +void EnsureSafeShutdown(); + //////////////////////////////////////////////////////////////////////////////// } // namespace NYT |