Skip to content

Commit 91e9258

Browse files
committed
FIX: potential race condition
1 parent f6b45d2 commit 91e9258

1 file changed

Lines changed: 20 additions & 16 deletions

File tree

module/src/LidarOdometry_InitialLocalization.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -196,23 +196,27 @@ void LidarOdometry::handleInitialLocalizationStateEstimation()
196196
// Check if state estimator has converged
197197
mrpt::poses::CPose3DPDFGaussian estimatedPose;
198198

199-
// First, ensure we have geo-referencing
200-
// (either from loaded map or from state estimator)
201-
bool hasGeoRef =
202-
state_.local_map->georeferencing.has_value() || state_.external_georef.has_value();
203-
204-
if (!hasGeoRef) {
205-
MRPT_LOG_THROTTLE_INFO(
206-
5.0, "Waiting for geo-referencing (from loaded map or state estimator)...");
207-
return; // Not ready yet
208-
}
199+
// Snapshot georef state under lock to avoid data race with
200+
// onExternalMapUpdate which writes state_.external_georef concurrently.
201+
{
202+
auto lckState = mrpt::lockHelper(state_mtx_);
203+
204+
const bool hasGeoRef =
205+
state_.local_map->georeferencing.has_value() || state_.external_georef.has_value();
206+
207+
if (!hasGeoRef) {
208+
MRPT_LOG_THROTTLE_INFO(
209+
5.0, "Waiting for geo-referencing (from loaded map or state estimator)...");
210+
return; // Not ready yet
211+
}
209212

210-
// Apply external georef to local map if needed
211-
if (!state_.local_map->georeferencing.has_value() && state_.external_georef.has_value()) {
212-
state_.local_map->georeferencing.emplace();
213-
state_.local_map->georeferencing->geo_coord = state_.external_georef->geo_coord;
214-
state_.local_map->georeferencing->T_enu_to_map = state_.external_georef->T_enu_to_map;
215-
state_.mark_local_map_georef_as_updated();
213+
// Apply external georef to local map if needed
214+
if (!state_.local_map->georeferencing.has_value() && state_.external_georef.has_value()) {
215+
state_.local_map->georeferencing.emplace();
216+
state_.local_map->georeferencing->geo_coord = state_.external_georef->geo_coord;
217+
state_.local_map->georeferencing->T_enu_to_map = state_.external_georef->T_enu_to_map;
218+
state_.mark_local_map_georef_as_updated();
219+
}
216220
}
217221

218222
// Now check if state estimator has converged

0 commit comments

Comments
 (0)