Skip to content

Commit

Permalink
Fix LogtailMonitor's deadlock when suicide (#2085)
Browse files Browse the repository at this point in the history
  • Loading branch information
Takuka0311 authored Feb 12, 2025
1 parent 89b7311 commit 4d5d938
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
17 changes: 11 additions & 6 deletions core/monitor/Monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ LogtailMonitor* LogtailMonitor::GetInstance() {
bool LogtailMonitor::Init() {
mScaledCpuUsageUpLimit = AppConfig::GetInstance()->GetCpuUsageUpLimit();
mStatusCount = 0;
mShouldSuicide.store(false);

// Reset process and realtime CPU statistics.
mCpuStat.Reset();
Expand Down Expand Up @@ -129,7 +130,7 @@ void LogtailMonitor::Stop() {
return;
}
future_status s = mThreadRes.wait_for(chrono::seconds(1));
if (s == future_status::ready) {
if (s == future_status::ready || mShouldSuicide.load()) {
LOG_INFO(sLogger, ("profiling", "stopped successfully"));
} else {
LOG_WARNING(sLogger, ("profiling", "forced to stopped"));
Expand Down Expand Up @@ -181,7 +182,8 @@ void LogtailMonitor::Monitor() {
LOG_ERROR(sLogger,
("Resource used by program exceeds hard limit",
"prepare restart Logtail")("mem_rss", mMemStat.mRss));
Suicide();
mShouldSuicide.store(true);
break;
}
}

Expand All @@ -206,11 +208,13 @@ void LogtailMonitor::Monitor() {
LOG_ERROR(sLogger,
("Resource used by program exceeds upper limit for some time",
"prepare restart Logtail")("cpu_usage", mCpuStat.mCpuUsage)("mem_rss", mMemStat.mRss));
Suicide();
mShouldSuicide.store(true);
break;
}

if (IsHostIpChanged()) {
Suicide();
mShouldSuicide.store(true);
break;
}

SendStatusProfile(false);
Expand All @@ -221,7 +225,9 @@ void LogtailMonitor::Monitor() {
}
}
}
SendStatusProfile(true);
if (mShouldSuicide.load()) {
Suicide();
}
}

bool LogtailMonitor::SendStatusProfile(bool suicide) {
Expand Down Expand Up @@ -402,7 +408,6 @@ bool LogtailMonitor::IsHostIpChanged() {

void LogtailMonitor::Suicide() {
SendStatusProfile(true);
mIsThreadRunning = false;
Application::GetInstance()->SetSigTermSignalFlag(true);
sleep(60);
_exit(1);
Expand Down
1 change: 1 addition & 0 deletions core/monitor/Monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class LogtailMonitor {
std::future<void> mThreadRes;
std::mutex mThreadRunningMux;
bool mIsThreadRunning = true;
std::atomic_bool mShouldSuicide;
std::condition_variable mStopCV;

// Control report status profile frequency.
Expand Down

0 comments on commit 4d5d938

Please sign in to comment.