diff --git a/cloud/storage/core/libs/daemon/app.cpp b/cloud/storage/core/libs/daemon/app.cpp index 4fb834938a4..7474948954a 100644 --- a/cloud/storage/core/libs/daemon/app.cpp +++ b/cloud/storage/core/libs/daemon/app.cpp @@ -58,10 +58,11 @@ class TMainThread return ShouldContinue->GetReturnCode(); } - void Stop(int exitCode) + void Stop() { - if (AtomicCas(&State, Stopped, Started)) { - ShouldContinue->ShouldStop(exitCode); + auto prevState = AtomicSwap(&State, Stopped); + if (prevState == Started) { + ShouldContinue->ShouldStop(0); WaitCondVar.Signal(); } } @@ -72,7 +73,7 @@ class TMainThread void ProcessSignal(int signum) { if (signum == SIGINT || signum == SIGTERM) { - AppStop(0); + AppStop(); } } @@ -97,9 +98,9 @@ int AppMain(TProgramShouldContinue& shouldContinue) return TMainThread::GetInstance()->Run(shouldContinue); } -void AppStop(int exitCode) +void AppStop() { - TMainThread::GetInstance()->Stop(exitCode); + TMainThread::GetInstance()->Stop(); } //////////////////////////////////////////////////////////////////////////////// diff --git a/cloud/storage/core/libs/daemon/app.h b/cloud/storage/core/libs/daemon/app.h index da4b412d272..6316e6d2b46 100644 --- a/cloud/storage/core/libs/daemon/app.h +++ b/cloud/storage/core/libs/daemon/app.h @@ -10,7 +10,7 @@ namespace NCloud { void AppCreate(); int AppMain(TProgramShouldContinue& shouldContinue); -void AppStop(int exitCode); +void AppStop(); void ConfigureSignals(); diff --git a/cloud/storage/core/libs/daemon/app_ut.cpp b/cloud/storage/core/libs/daemon/app_ut.cpp new file mode 100644 index 00000000000..3b7bba369c7 --- /dev/null +++ b/cloud/storage/core/libs/daemon/app_ut.cpp @@ -0,0 +1,23 @@ +#include "app.h" + +#include + +#include + + +namespace NCloud { + +//////////////////////////////////////////////////////////////////////////////// + +Y_UNIT_TEST_SUITE(TAppMainThreadTest) +{ + Y_UNIT_TEST(ShouldHandleStopBeforeAppMain) + { + TProgramShouldContinue shouldContinue; + AppCreate(); + AppStop(); + AppMain(shouldContinue); + } +} + +} // namespace NCloud diff --git a/cloud/storage/core/libs/daemon/ut/ya.make b/cloud/storage/core/libs/daemon/ut/ya.make new file mode 100644 index 00000000000..b91abccbe9e --- /dev/null +++ b/cloud/storage/core/libs/daemon/ut/ya.make @@ -0,0 +1,7 @@ +UNITTEST_FOR(cloud/storage/core/libs/daemon) + +SRCS( + app_ut.cpp +) + +END() diff --git a/cloud/storage/core/libs/daemon/ya.make b/cloud/storage/core/libs/daemon/ya.make index 3b0f01d4b3e..24cb080c8d4 100644 --- a/cloud/storage/core/libs/daemon/ya.make +++ b/cloud/storage/core/libs/daemon/ya.make @@ -18,3 +18,5 @@ PEERDIR( ) END() + +RECURSE_FOR_TESTS(ut)