Skip to content

Commit

Permalink
issue-936: fix race during app termination because of signal (#950)
Browse files Browse the repository at this point in the history
* issue-936: fix race during app termination because of signal

* add eol
  • Loading branch information
yegorskii authored Apr 11, 2024
1 parent 79d3300 commit 846fbeb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
13 changes: 7 additions & 6 deletions cloud/storage/core/libs/daemon/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand All @@ -72,7 +73,7 @@ class TMainThread
void ProcessSignal(int signum)
{
if (signum == SIGINT || signum == SIGTERM) {
AppStop(0);
AppStop();
}
}

Expand All @@ -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();
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion cloud/storage/core/libs/daemon/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace NCloud {

void AppCreate();
int AppMain(TProgramShouldContinue& shouldContinue);
void AppStop(int exitCode);
void AppStop();

void ConfigureSignals();

Expand Down
23 changes: 23 additions & 0 deletions cloud/storage/core/libs/daemon/app_ut.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "app.h"

#include <contrib/ydb/library/actors/util/should_continue.h>

#include <library/cpp/testing/unittest/registar.h>


namespace NCloud {

////////////////////////////////////////////////////////////////////////////////

Y_UNIT_TEST_SUITE(TAppMainThreadTest)
{
Y_UNIT_TEST(ShouldHandleStopBeforeAppMain)
{
TProgramShouldContinue shouldContinue;
AppCreate();
AppStop();
AppMain(shouldContinue);
}
}

} // namespace NCloud
7 changes: 7 additions & 0 deletions cloud/storage/core/libs/daemon/ut/ya.make
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
UNITTEST_FOR(cloud/storage/core/libs/daemon)

SRCS(
app_ut.cpp
)

END()
2 changes: 2 additions & 0 deletions cloud/storage/core/libs/daemon/ya.make
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ PEERDIR(
)

END()

RECURSE_FOR_TESTS(ut)

0 comments on commit 846fbeb

Please sign in to comment.