From e01c2dd44e26780a4a220e50f4a58e343cba086a Mon Sep 17 00:00:00 2001 From: Balduin Date: Fri, 20 Nov 2026 08:44:15 +0100 Subject: [PATCH] fix(mavlink): store params read from the receiver thread atomically (#29908) * fix(mavlink): read MAV_HB_FORW_EN via an atomic snapshot in the receiver forward_message() reads it on the receiver thread while the main mavlink thread updates the ModuleParams member in updateParams(). Flagged by TSan in CI (hex, ThreadSanitizer, multicopter failure injection test). Assisted-by: Claude:claude-opus-5-5 * fix(mavlink): read MAV_FWDEXTSP via an atomic snapshot in the receiver The receiver thread reads it when handling setpoint messages while the main mavlink thread updates the ModuleParams member in updateParams(). Assisted-by: Claude:claude-opus-5-5 * fix(mavlink): read SYS_FAILURE_EN via an atomic snapshot in the receiver The receiver thread reads it when handling commands while the main mavlink thread updates the ModuleParams member in updateParams(). Assisted-by: Claude:claude-opus-5-5 * fix(mavlink): read MAV_HASH_CHK_EN via an atomic snapshot in the receiver MavlinkParametersManager runs on the receiver thread and reads it on PARAM_SET of _HASH_CHECK while the main mavlink thread updates the ModuleParams member in updateParams(). Assisted-by: Claude:claude-opus-5-5 * fix(mavlink): read MAV_TYPE via an atomic snapshot in the receiver The receiver thread reads it when handling ODOMETRY while the main mavlink thread updates the ModuleParams member in updateParams(). Assisted-by: Claude:claude-opus-5-5 --- src/modules/mavlink/mavlink_main.cpp | 8 ++++++++ src/modules/mavlink/mavlink_main.h | 21 ++++++++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/modules/mavlink/mavlink_main.cpp b/src/modules/mavlink/mavlink_main.cpp index c0d50db983b..a1812168a0e 100644 --- a/src/modules/mavlink/mavlink_main.cpp +++ b/src/modules/mavlink/mavlink_main.cpp @@ -311,6 +311,12 @@ Mavlink::mavlink_update_parameters() // Refresh atomic snapshots of params read by the receiver thread, so the // receiver doesn't read the (non-atomic) ModuleParams members directly. _use_hil_gps_atomic.store(_param_mav_usehilgps.get()); + _hb_forw_en_atomic.store(_param_mav_hb_forw_en.get()); + _fwdextsp_atomic.store(_param_mav_fwdextsp.get()); + _hash_chk_en_atomic.store(_param_mav_hash_chk_en.get()); +#if defined(CONFIG_MODULES_FAILURE_INJECTION_MANAGER) + _failure_injection_en_atomic.store(_param_sys_failure_en.get()); +#endif // CONFIG_MODULES_FAILURE_INJECTION_MANAGER setProtocolVersion(_param_mav_proto_ver.get()); @@ -319,6 +325,8 @@ Mavlink::mavlink_update_parameters() _param_mav_type.commit_no_notification(); PX4_ERR("MAV_TYPE parameter invalid, resetting to 0."); } + + _mav_type_atomic.store(_param_mav_type.get()); } bool Mavlink::set_channel() diff --git a/src/modules/mavlink/mavlink_main.h b/src/modules/mavlink/mavlink_main.h index 02422c8d06e..cbc5c6cefa6 100644 --- a/src/modules/mavlink/mavlink_main.h +++ b/src/modules/mavlink/mavlink_main.h @@ -258,7 +258,7 @@ public: // mavlink_update_parameters(). bool get_use_hil_gps() { return _use_hil_gps_atomic.load(); } - bool get_forward_externalsp() { return _param_mav_fwdextsp.get(); } + bool get_forward_externalsp() { return _fwdextsp_atomic.load(); } bool get_flow_control_enabled() { return _flow_control_mode; } @@ -428,7 +428,7 @@ public: void update_radio_status(const radio_status_s &radio_status); - unsigned get_system_type() { return _param_mav_type.get(); } + unsigned get_system_type() { return _mav_type_atomic.load(); } Protocol get_protocol() const { return _protocol; } @@ -506,13 +506,13 @@ public: const events::SendProtocol &get_events_protocol() const { return _events; }; bool ftp_enabled() const { return _ftp_on; } - bool hash_check_enabled() const { return _param_mav_hash_chk_en.get(); } - bool forward_heartbeats_enabled() const { return _param_mav_hb_forw_en.get(); } + bool hash_check_enabled() const { return _hash_chk_en_atomic.load(); } + bool forward_heartbeats_enabled() const { return _hb_forw_en_atomic.load(); } bool failure_injection_enabled() const { #if defined(CONFIG_MODULES_FAILURE_INJECTION_MANAGER) - return _param_sys_failure_en.get(); + return _failure_injection_en_atomic.load(); #else return false; #endif // CONFIG_MODULES_FAILURE_INJECTION_MANAGER @@ -706,9 +706,16 @@ private: // Atomic so concurrent reads/writes are race-free without taking lock_send(). px4::atomic _protocol_version{0}; ///< after initialization the only values are 1 and 2 - // Atomic snapshot of MAV_USEHILGPS, written from the main mavlink thread - // in mavlink_update_parameters() and read from the receiver thread. + // Atomic snapshots of params read from the receiver thread, written from the + // main mavlink thread in mavlink_update_parameters(). px4::atomic_bool _use_hil_gps_atomic{false}; + px4::atomic_bool _hb_forw_en_atomic{false}; + px4::atomic_bool _fwdextsp_atomic{false}; + px4::atomic_bool _hash_chk_en_atomic{false}; + px4::atomic _mav_type_atomic{0}; +#if defined(CONFIG_MODULES_FAILURE_INJECTION_MANAGER) + px4::atomic_bool _failure_injection_en_atomic {false}; +#endif // CONFIG_MODULES_FAILURE_INJECTION_MANAGER px4::atomic _bytes_tx{0}; px4::atomic _bytes_txerr{0};