Skip to content

Commit

Permalink
Prevent optimization thread from doing its work when a stop was reque…
Browse files Browse the repository at this point in the history
…sted
  • Loading branch information
carlos-m159 committed Feb 19, 2025
1 parent c4fd475 commit 0754252
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions fuse_optimizers/src/fixed_lag_smoother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ void FixedLagSmoother::optimizationLoop()
// Optimize
{
std::lock_guard<std::mutex> lock(optimization_mutex_);
// Make sure stop was not called while we were trying to lock optimization mutex
if (!started_)
{
ROS_DEBUG_STREAM("Optimizer stopped while trying to acquire optimization_mutex_. Skipping optimization.");
continue;
}

// Apply motion models
auto new_transaction = fuse_core::Transaction::make_shared();
// DANGER: processQueue obtains a lock from the pending_transactions_mutex_
Expand Down Expand Up @@ -492,21 +499,21 @@ void FixedLagSmoother::start()
void FixedLagSmoother::stop()
{
ROS_INFO_STREAM("Stopping optimizer.");
// Tell all the plugins to stop
stopPlugins();
started_ = false;
ignited_ = false;
// Reset the optimizer state
{
std::lock_guard<std::mutex> lock(optimization_requested_mutex_);
optimization_request_ = false;
}
started_ = false;
ignited_ = false;
setStartTime(ros::Time(0, 0));
// DANGER: The optimizationLoop() function obtains the lock optimization_mutex_ lock and the
// pending_transactions_mutex_ lock at the same time. We perform a parallel locking scheme here to
// prevent the possibility of deadlocks.
{
std::lock_guard<std::mutex> lock(optimization_mutex_);
// Tell all the plugins to stop
stopPlugins();
// Clear all pending transactions
{
std::lock_guard<std::mutex> lock(pending_transactions_mutex_);
Expand Down

0 comments on commit 0754252

Please sign in to comment.