fix(flight_mode_manager): set up subscription before registering callback

init() registered the vehicle_local_position callback before calling
set_interval_us() and initialising _time_stamp_last_loop. The moment the callback
is registered it fires (and Run() is scheduled) on the publisher's thread, racing
those writes - ThreadSanitizer flags both. Register the callback last, once the
object is fully initialised.
This commit is contained in:
Julian Oes authored and Ramon Roche committed 2026-07-02 08:42:49 -07:00
1 parent 65f58b3bfe
commit aed9c16bf1
1 file changed
+7 -3
@@ -68,14 +68,18 @@ FlightModeManager::~FlightModeManager()
bool FlightModeManager::init()
{
// Initialise fully before registering the callback: once registered, the
// callback fires (and Run() is scheduled) on the publisher's thread, which
// would otherwise race with the set_interval_us()/_time_stamp_last_loop writes.
// limit to every other vehicle_local_position update (50 Hz)
_vehicle_local_position_sub.set_interval_us(20_ms);
_time_stamp_last_loop = hrt_absolute_time();
if (!_vehicle_local_position_sub.registerCallback()) {
PX4_ERR("callback registration failed");
return false;
}
// limit to every other vehicle_local_position update (50 Hz)
_vehicle_local_position_sub.set_interval_us(20_ms);
_time_stamp_last_loop = hrt_absolute_time();
return true;
}