From 5b22d9ed66f466fcf74f9d984a6c3d2ce3539f18 Mon Sep 17 00:00:00 2001 From: j3kestrel Date: Tue, 3 Nov 2026 14:12:27 -0500 Subject: [PATCH] [BACKPORT 1.18] fix(uorb): keep shared topics advertised on publisher teardown (#29176) (#29586) * fix(dataman_client): keep request topic advertised on client teardown * fix(uorb): keep shared topics advertised on publisher teardown Single-instance publications share one advertised flag, so destroying one publisher must not hide updates from the others. Keep automatic unadvertising in PublicationMultiBase and remove the DatamanClient workaround. Cover surviving publishers and instance reuse, and reset retained input samples between test cases. Tested: make tests (215/215), make format. Signed-off-by: jonas Assisted-by: Codex * docs(uorb): correct unadvertise comments and document teardown The unadvertise comment claimed it has no effect on single-instance topics, which is how one publisher's teardown came to hide the others. State what clearing the shared flag does, and document that only PublicationMulti releases its instance on destruction. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: jonas * test(uorb): keep test cases independent of retained samples A destroyed single-instance publication now leaves its last sample visible, so ManualControlTest, test_prec_takeoff and mixer_module_tests passed only in declaration order and failed under --gtest_shuffle. Reset with an inert sample where one exists, otherwise unadvertise the test's own publications, and drop module outputs left over from a previous case. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: jonas * fix(boards): drop the PublicationBase destructor from the ITCM lists The destructor is now trivial and no longer emitted, so itcm_check.py fails on the missing section. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Jacob Dahl --------- Signed-off-by: jonas Signed-off-by: Jacob Dahl Co-authored-by: jonas Co-authored-by: Jacob Dahl (cherry picked from commit e146fe4cc69459493196b172d9b4576dc4bce127) Backport adaptation: release/1.18 has no PublicationMultiBase (main's 4791b70c15c out-lined PublicationMulti), so the destructor that frees the instance goes into PublicationMulti and Publication.cpp is unchanged. The ITCM entries for agam/ark fmu-v6xrt and the navigator tests test_mission_route_cache.cpp / test_prec_takeoff.cpp are dropped: those files are not on release/1.18. TEST_VTEPosition.cpp uses release/1.18's _uav_gps_pub (main renamed it _uav_gnss_pub). Assisted-by: Claude:claude-opus-5-5 Co-authored-by: Jonas Perolini <74473718+JonasPerolini@users.noreply.github.com> --- .../scripts/itcm_functions_includes.ld | 1 - .../scripts/itcm_functions_includes.ld | 1 - .../scripts/itcm_functions_includes.ld | 1 - docs/en/middleware/uorb.md | 3 ++ platforms/common/uORB/Publication.hpp | 11 ++---- platforms/common/uORB/PublicationMulti.hpp | 10 ++++++ platforms/common/uORB/uORBDeviceNode.cpp | 12 ++----- platforms/common/uORB/uORBDeviceNode.hpp | 2 +- platforms/common/uORB/uORBManager.hpp | 1 + .../uORB/uORB_tests/uORBTest_UnitTest.cpp | 36 +++++++++++++++++++ src/lib/mixer_module/mixer_module_tests.cpp | 8 +++++ .../manual_control/ManualControlTest.cpp | 17 +++++++-- .../test/TEST_VTEPosition.cpp | 5 +++ 13 files changed, 84 insertions(+), 24 deletions(-) diff --git a/boards/nxp/mr-tropic/nuttx-config/scripts/itcm_functions_includes.ld b/boards/nxp/mr-tropic/nuttx-config/scripts/itcm_functions_includes.ld index 485aef73740..3cc3d8e6a7e 100644 --- a/boards/nxp/mr-tropic/nuttx-config/scripts/itcm_functions_includes.ld +++ b/boards/nxp/mr-tropic/nuttx-config/scripts/itcm_functions_includes.ld @@ -701,7 +701,6 @@ *(.text._ZN22MavlinkStreamESCStatus8get_sizeEv) *(.text._Z20px4_spi_bus_externalRK13px4_spi_bus_t) *(.text.read) -*(.text._ZN4uORB15PublicationBaseD1Ev) *(.text._ZN22MavlinkStreamDebugVect8get_sizeEv) *(.text._ZN7Mission11on_inactiveEv) *(.text._ZN7sensors19VehicleMagnetometer20UpdateMagCalibrationEv) diff --git a/boards/nxp/tropic-community/nuttx-config/scripts/itcm_functions_includes.ld b/boards/nxp/tropic-community/nuttx-config/scripts/itcm_functions_includes.ld index 485aef73740..3cc3d8e6a7e 100644 --- a/boards/nxp/tropic-community/nuttx-config/scripts/itcm_functions_includes.ld +++ b/boards/nxp/tropic-community/nuttx-config/scripts/itcm_functions_includes.ld @@ -701,7 +701,6 @@ *(.text._ZN22MavlinkStreamESCStatus8get_sizeEv) *(.text._Z20px4_spi_bus_externalRK13px4_spi_bus_t) *(.text.read) -*(.text._ZN4uORB15PublicationBaseD1Ev) *(.text._ZN22MavlinkStreamDebugVect8get_sizeEv) *(.text._ZN7Mission11on_inactiveEv) *(.text._ZN7sensors19VehicleMagnetometer20UpdateMagCalibrationEv) diff --git a/boards/px4/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld b/boards/px4/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld index 4c0a29204db..63c2654f9ca 100644 --- a/boards/px4/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld +++ b/boards/px4/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld @@ -715,7 +715,6 @@ *(.text._ZN22MavlinkStreamESCStatus8get_sizeEv) *(.text._Z20px4_spi_bus_externalRK13px4_spi_bus_t) *(.text.read) -*(.text._ZN4uORB15PublicationBaseD1Ev) *(.text._ZN22MavlinkStreamDebugVect8get_sizeEv) *(.text._ZN7Mission11on_inactiveEv) *(.text._ZN7sensors19VehicleMagnetometer20UpdateMagCalibrationEv) diff --git a/docs/en/middleware/uorb.md b/docs/en/middleware/uorb.md index 3c8e78e0105..c92be5138d3 100644 --- a/docs/en/middleware/uorb.md +++ b/docs/en/middleware/uorb.md @@ -187,6 +187,9 @@ A subscriber will then have to choose to which instance to subscribe to using `o Make sure not to mix `orb_advertise_multi` and `orb_advertise` for the same topic! +Destroying a `uORB::PublicationMulti` frees its instance for the next publisher. +Destroying a `uORB::Publication` leaves the topic advertised with its last sample, because other publishers may share it. + The full API is documented in [platforms/common/uORB/uORBManager.hpp](https://github.com/PX4/PX4-Autopilot/blob/main/platforms/common/uORB/uORBManager.hpp). ## Listing Topics and Listening in diff --git a/platforms/common/uORB/Publication.hpp b/platforms/common/uORB/Publication.hpp index 03d274a0351..782130d99b1 100644 --- a/platforms/common/uORB/Publication.hpp +++ b/platforms/common/uORB/Publication.hpp @@ -64,15 +64,8 @@ protected: PublicationBase(ORB_ID id) : _orb_id(id) {} - ~PublicationBase() - { - if (_handle != nullptr) { - // don't automatically unadvertise queued publications (eg vehicle_command) - if (Manager::orb_get_queue_size(_handle) == 1) { - unadvertise(); - } - } - } + // Single-instance publishers share the node, so destruction must not unadvertise it. + ~PublicationBase() = default; // type-independent publish; data points to a message of the topic's type bool publish(const void *data); diff --git a/platforms/common/uORB/PublicationMulti.hpp b/platforms/common/uORB/PublicationMulti.hpp index 75517308569..6ab561e4449 100644 --- a/platforms/common/uORB/PublicationMulti.hpp +++ b/platforms/common/uORB/PublicationMulti.hpp @@ -69,6 +69,16 @@ public: PublicationBase(static_cast(meta->o_id)) {} + ~PublicationMulti() + { + if (_handle != nullptr) { + // free the instance for the next advertiser, but keep queued publications advertised + if (Manager::orb_get_queue_size(_handle) == 1) { + unadvertise(); + } + } + } + bool advertise() { if (!advertised()) { diff --git a/platforms/common/uORB/uORBDeviceNode.cpp b/platforms/common/uORB/uORBDeviceNode.cpp index 82063d84e3e..2ac3b230ff4 100644 --- a/platforms/common/uORB/uORBDeviceNode.cpp +++ b/platforms/common/uORB/uORBDeviceNode.cpp @@ -304,15 +304,9 @@ int uORB::DeviceNode::unadvertise(orb_advert_t handle) uORB::DeviceNode *devnode = (uORB::DeviceNode *)handle; /* - * We are cheating a bit here. First, with the current implementation, we can only - * have multiple publishers for instance 0. In this case the caller will have - * instance=nullptr and _published has no effect at all. Thus no unadvertise is - * necessary. - * In case of multiple instances, we have at most 1 publisher per instance and - * we can signal an instance as 'free' by setting _published to false. - * We never really free the DeviceNode, for this we would need reference counting - * of subscribers and publishers. But we also do not have a leak since future - * publishers reuse the same DeviceNode object. + * Frees a multi-instance node for the next advertiser and hides it from all subscribers. + * Publishers of a single-instance topic share this flag, so unadvertising hides all of them. + * The DeviceNode is never freed; later publishers reuse it. */ devnode->_advertised.store(false); diff --git a/platforms/common/uORB/uORBDeviceNode.hpp b/platforms/common/uORB/uORBDeviceNode.hpp index 37a860c69ff..dee6bd0a918 100644 --- a/platforms/common/uORB/uORBDeviceNode.hpp +++ b/platforms/common/uORB/uORBDeviceNode.hpp @@ -292,7 +292,7 @@ private: List _callbacks; const uint8_t _instance; /**< orb multi instance identifier */ - px4::atomic_bool _advertised{false}; /**< has ever been advertised (not necessarily published data yet) */ + px4::atomic_bool _advertised{false}; /**< set on advertise, cleared on unadvertise; subscribers only read while set */ int8_t _subscriber_count{0}; diff --git a/platforms/common/uORB/uORBManager.hpp b/platforms/common/uORB/uORBManager.hpp index 431cb8ff4e3..49d6887ef00 100644 --- a/platforms/common/uORB/uORBManager.hpp +++ b/platforms/common/uORB/uORBManager.hpp @@ -258,6 +258,7 @@ public: /** * Unadvertise a topic. + * This hides the topic instance from all subscribers, including data from other publishers of a single-instance topic. * * @param handle handle returned by orb_advertise or orb_advertise_multi. * @return 0 on success diff --git a/platforms/common/uORB/uORB_tests/uORBTest_UnitTest.cpp b/platforms/common/uORB/uORB_tests/uORBTest_UnitTest.cpp index 69d63fa30fd..73a2d2eba80 100644 --- a/platforms/common/uORB/uORB_tests/uORBTest_UnitTest.cpp +++ b/platforms/common/uORB/uORB_tests/uORBTest_UnitTest.cpp @@ -39,7 +39,9 @@ #include #include #include +#include #include +#include #include uORBTest::UnitTest &uORBTest::UnitTest::instance() @@ -324,6 +326,32 @@ int uORBTest::UnitTest::test_single() return test_fail("orb_unadvertise failed: %i", ret); } + uORB::Publication surviving_pub{ORB_ID::orb_test}; + uORB::Subscription sub{ORB_ID::orb_test}; + t.val = 3; + + if (!surviving_pub.publish(t) || !sub.update(&u) || u.val != t.val) { + return test_fail("initial publication failed"); + } + + { + uORB::Publication temporary_pub{ORB_ID::orb_test}; + t.val = 4; + + if (!temporary_pub.publish(t) || !sub.update(&u) || u.val != t.val) { + return test_fail("second publication failed"); + } + } + + t.val = 5; + + if (!surviving_pub.publish(t) || !sub.update(&u) || u.val != t.val) { + return test_fail("publication lost after another publisher was destroyed"); + } + + // Leave the topic unadvertised for the multi-instance tests. + surviving_pub.unadvertise(); + return test_note("PASS single-topic test"); } @@ -332,6 +360,14 @@ int uORBTest::UnitTest::test_multi() /* this routine tests the multi-topic support */ test_note("try multi-topic support"); + { + uORB::PublicationMulti temporary_pub{ORB_ID::orb_multitest}; + + if (!temporary_pub.advertise() || temporary_pub.get_instance() != 0) { + return test_fail("multi-instance advertisement failed"); + } + } // Destruction must release instance 0 for reuse below. + orb_test_s t{}; orb_test_s u{}; diff --git a/src/lib/mixer_module/mixer_module_tests.cpp b/src/lib/mixer_module/mixer_module_tests.cpp index 5a6f08ed869..d58d4cbc7d8 100644 --- a/src/lib/mixer_module/mixer_module_tests.cpp +++ b/src/lib/mixer_module/mixer_module_tests.cpp @@ -82,6 +82,14 @@ class OutputModuleTest : public OutputModuleInterface public: OutputModuleTest() : OutputModuleInterface(MODULE_NAME, px4::wq_configurations::hp_default) {}; + ~OutputModuleTest() override + { + // Hide this test's commands from the next test: a zero actuator command is not inert + _actuator_motors_pub.unadvertise(); + _actuator_servos_pub.unadvertise(); + _actuator_armed_pub.unadvertise(); + } + void Run() override { was_scheduled = true; diff --git a/src/modules/manual_control/ManualControlTest.cpp b/src/modules/manual_control/ManualControlTest.cpp index 582c3e754ce..4ad60436004 100644 --- a/src/modules/manual_control/ManualControlTest.cpp +++ b/src/modules/manual_control/ManualControlTest.cpp @@ -83,10 +83,24 @@ public: param_set(param_find("COM_FLTMODE5"), &mode); mode = NAVIGATION_STATE_AUTO_MISSION; param_set(param_find("COM_FLTMODE6"), &mode); + + // Drop outputs left over from a previous test + while (_manual_control_setpoint_sub.update()) {} + + while (_action_request_sub.update()) {} + } + + void TearDown() override + { + // Hide this test's inputs from the next test: empty switch or status samples are not inert + _manual_control_switches_pub.unadvertise(); + _manual_control_input_pub.unadvertise(); + _vehicle_status_pub.unadvertise(); } uORB::Publication _manual_control_switches_pub{ORB_ID(manual_control_switches)}; uORB::Publication _manual_control_input_pub{ORB_ID(manual_control_input)}; + uORB::Publication _vehicle_status_pub{ORB_ID(vehicle_status)}; uORB::SubscriptionData _manual_control_setpoint_sub{ORB_ID(manual_control_setpoint)}; uORB::SubscriptionData _action_request_sub{ORB_ID(action_request)}; @@ -309,8 +323,7 @@ TEST_F(SwitchTest, ModeSwitchInitialization) TEST_F(SwitchTest, ModeSwitchInitializationArmed) { // GIVEN: vehicle is armed - uORB::Publication vehicle_status_pub{ORB_ID(vehicle_status)}; - vehicle_status_pub.publish({.arming_state = vehicle_status_s::ARMING_STATE_ARMED}); + _vehicle_status_pub.publish({.arming_state = vehicle_status_s::ARMING_STATE_ARMED}); // GIVEN: valid stick input from RC _manual_control_input_pub.publish({.timestamp_sample = _timestamp, .valid = true, .data_source = manual_control_setpoint_s::SOURCE_RC}); diff --git a/src/modules/vision_target_estimator/test/TEST_VTEPosition.cpp b/src/modules/vision_target_estimator/test/TEST_VTEPosition.cpp index 6e1c134dc86..a86c343b78e 100644 --- a/src/modules/vision_target_estimator/test/TEST_VTEPosition.cpp +++ b/src/modules/vision_target_estimator/test/TEST_VTEPosition.cpp @@ -143,6 +143,11 @@ protected: _uav_gps_pub = std::make_unique>(ORB_ID(vehicle_gps_position)); _target_gps_pub = std::make_unique>(ORB_ID(target_gnss)); + // Discard input samples retained from the previous test. + _vision_pub->publish({}); + _uav_gps_pub->publish({}); + _target_gps_pub->publish({}); + _aid_fiducial_sub = std::make_unique>(ORB_ID(vte_aid_fiducial_marker)); _aid_gps_mission_sub = std::make_unique>(ORB_ID(vte_aid_gps_pos_mission)); _aid_gps_target_sub = std::make_unique>(ORB_ID(vte_aid_gps_pos_target));