Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve signal handling #2501

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ Server::Server(const ServerConfig &_config)
this->dataPtr->AddRecordPlugin(_config);
}

// If we've received a signal before we create entities, the Stop event
// won't be propagated to them. Instead, we just quit early here.
if (this->dataPtr->signalReceived)
return;

this->dataPtr->CreateEntities();

// Set the desired update period, this will override the desired RTF given in
Expand Down
8 changes: 8 additions & 0 deletions src/ServerPrivate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ ServerPrivate::~ServerPrivate()
//////////////////////////////////////////////////
void ServerPrivate::OnSignal(int _sig)
{
this->signalReceived = true;
gzdbg << "Server received signal[" << _sig << "]\n";
this->Stop();
}
Expand All @@ -130,6 +131,13 @@ void ServerPrivate::Stop()
bool ServerPrivate::Run(const uint64_t _iterations,
std::optional<std::condition_variable *> _cond)
{
// Return early if we've received a signal right before.
// The ServerPrivate signal handler would set `running=false`,
// but we immediately would set it to true here, which will essentially ignore
// the signal. Since we can't reliably use the `running` variable, we return
// if `signalReceived` is true
if (this->signalReceived)
return false;
this->runMutex.lock();
this->running = true;
if (_cond)
Expand Down
4 changes: 4 additions & 0 deletions src/ServerPrivate.hh
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ namespace gz
/// \brief Our signal handler.
public: gz::common::SignalHandler sigHandler;

/// \brief Set to true from signal handler. This will be used to
/// terminate the server where checking `running` is not sufficient.
public: std::atomic<bool> signalReceived{false};

/// \brief Our system loader.
public: SystemLoaderPtr systemLoader;

Expand Down
Loading