From aed9c16bf11c165b337e8a0bc070d9f628707c3e Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Thu, 18 Jun 2026 20:45:16 +1200 Subject: [PATCH] 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. --- src/modules/flight_mode_manager/FlightModeManager.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/modules/flight_mode_manager/FlightModeManager.cpp b/src/modules/flight_mode_manager/FlightModeManager.cpp index f83322511f3..b9c12f8e4dd 100644 --- a/src/modules/flight_mode_manager/FlightModeManager.cpp +++ b/src/modules/flight_mode_manager/FlightModeManager.cpp @@ -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; }