Skip to content

Commit ab1fc6b

Browse files
authored
Prevent optimization thread from doing its work when a stop was requested (#391)
1 parent c4fd475 commit ab1fc6b

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

fuse_optimizers/src/batch_optimizer.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ void BatchOptimizer::optimizationLoop()
157157
}
158158
{
159159
std::lock_guard<std::mutex> lock(optimization_mutex_);
160+
if (!started_)
161+
{
162+
ROS_DEBUG_STREAM("Optimizer stopped while trying to acquire optimization_mutex_. Skipping optimization.");
163+
continue;
164+
}
160165
// Copy the combined transaction so it can be shared with all the plugins
161166
fuse_core::Transaction::ConstSharedPtr const_transaction;
162167
{
@@ -289,19 +294,19 @@ void BatchOptimizer::start()
289294
void BatchOptimizer::stop()
290295
{
291296
ROS_INFO_STREAM("Stopping optimizer.");
292-
// Tell all the plugins to stop
293-
stopPlugins();
297+
started_ = false;
294298
// Reset the optimizer state
295299
{
296300
std::lock_guard<std::mutex> lock(optimization_requested_mutex_);
297301
optimization_request_ = false;
298302
}
299-
started_ = false;
300303
// DANGER: The optimizationLoop() function obtains the lock optimization_mutex_ lock and the
301304
// combined_transaction_mutex_ lock at the same time. We perform a parallel locking scheme here to
302305
// prevent the possibility of deadlocks.
303306
{
304307
std::lock_guard<std::mutex> lock(optimization_mutex_);
308+
// Tell all the plugins to stop
309+
stopPlugins();
305310
// Clear the combined transation
306311
{
307312
std::lock_guard<std::mutex> lock(combined_transaction_mutex_);

fuse_optimizers/src/fixed_lag_smoother.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ void FixedLagSmoother::optimizationLoop()
210210
// Optimize
211211
{
212212
std::lock_guard<std::mutex> lock(optimization_mutex_);
213+
// Make sure stop was not called while we were trying to acquire optimization mutex
214+
if (!started_)
215+
{
216+
ROS_DEBUG_STREAM("Optimizer stopped while trying to acquire optimization_mutex_. Skipping optimization.");
217+
continue;
218+
}
219+
213220
// Apply motion models
214221
auto new_transaction = fuse_core::Transaction::make_shared();
215222
// DANGER: processQueue obtains a lock from the pending_transactions_mutex_
@@ -492,21 +499,21 @@ void FixedLagSmoother::start()
492499
void FixedLagSmoother::stop()
493500
{
494501
ROS_INFO_STREAM("Stopping optimizer.");
495-
// Tell all the plugins to stop
496-
stopPlugins();
502+
started_ = false;
503+
ignited_ = false;
497504
// Reset the optimizer state
498505
{
499506
std::lock_guard<std::mutex> lock(optimization_requested_mutex_);
500507
optimization_request_ = false;
501508
}
502-
started_ = false;
503-
ignited_ = false;
504509
setStartTime(ros::Time(0, 0));
505510
// DANGER: The optimizationLoop() function obtains the lock optimization_mutex_ lock and the
506511
// pending_transactions_mutex_ lock at the same time. We perform a parallel locking scheme here to
507512
// prevent the possibility of deadlocks.
508513
{
509514
std::lock_guard<std::mutex> lock(optimization_mutex_);
515+
// Tell all the plugins to stop
516+
stopPlugins();
510517
// Clear all pending transactions
511518
{
512519
std::lock_guard<std::mutex> lock(pending_transactions_mutex_);

0 commit comments

Comments
 (0)