diff --git a/platforms/common/uORB/SubscriptionBlocking.hpp b/platforms/common/uORB/SubscriptionBlocking.hpp index 71bcff1adfa..97eb86646d6 100644 --- a/platforms/common/uORB/SubscriptionBlocking.hpp +++ b/platforms/common/uORB/SubscriptionBlocking.hpp @@ -60,6 +60,9 @@ public: virtual ~SubscriptionBlocking() { + // before call()'s mutex and condition variable go + this->unregisterCallback(); + pthread_mutex_destroy(&_mutex); pthread_cond_destroy(&_cv); } diff --git a/platforms/common/uORB/SubscriptionCallback.hpp b/platforms/common/uORB/SubscriptionCallback.hpp index 67c8cd693a6..a4f67b2f02f 100644 --- a/platforms/common/uORB/SubscriptionCallback.hpp +++ b/platforms/common/uORB/SubscriptionCallback.hpp @@ -66,6 +66,8 @@ public: { } + // A publisher calls call() until the callback is unregistered, and by the time this runs the subclass + // that implements it is gone. Subclasses must unregister in their own destructor. virtual ~SubscriptionCallback() { unregisterCallback(); @@ -138,7 +140,10 @@ public: { } - virtual ~SubscriptionCallbackWorkItem() = default; + virtual ~SubscriptionCallbackWorkItem() + { + unregisterCallback(); + } void call(unsigned generation) override { diff --git a/platforms/common/uORB/test/CMakeLists.txt b/platforms/common/uORB/test/CMakeLists.txt index 8aed8d0cbd7..c6f4ccc21e5 100644 --- a/platforms/common/uORB/test/CMakeLists.txt +++ b/platforms/common/uORB/test/CMakeLists.txt @@ -34,3 +34,4 @@ px4_add_functional_gtest(SRC uORBMessageFieldsTest.cpp LINKLIBS uORB) px4_add_functional_gtest(SRC uORBSubscriptionTest.cpp LINKLIBS uORB) +px4_add_functional_gtest(SRC uORBSubscriptionCallbackTest.cpp LINKLIBS uORB) diff --git a/platforms/common/uORB/test/uORBSubscriptionCallbackTest.cpp b/platforms/common/uORB/test/uORBSubscriptionCallbackTest.cpp new file mode 100644 index 00000000000..9ff404da175 --- /dev/null +++ b/platforms/common/uORB/test/uORBSubscriptionCallbackTest.cpp @@ -0,0 +1,147 @@ +/**************************************************************************** + * + * Copyright (c) 2026 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/** + * Callback subscriptions destroyed while their topic publishes. A publisher calls call() until the + * subscription is unregistered, so a subclass that leaves that to ~SubscriptionCallback() gets called + * after its own destructor has run, which aborts with "pure virtual method called". + */ + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +using namespace time_literals; + +namespace uORB +{ +namespace test +{ + +// Publishes orb_test as fast as it can for as long as it exists +class Publisher +{ +public: + Publisher() + { + _pub.advertise(); + _thread = std::thread([this] { + orb_test_s message{}; + + while (!_stop.load()) + { + message.val++; + _pub.publish(message); + } + }); + } + + ~Publisher() + { + _stop.store(true); + _thread.join(); + } + +private: + uORB::Publication _pub{ORB_ID(orb_test)}; + std::atomic _stop{false}; + std::thread _thread; +}; + +class TestWorkItem : public px4::WorkItem +{ +public: + TestWorkItem() : WorkItem("uorb_callback_test", px4::wq_configurations::test1) {} + + void Run() override {} +}; + +class uORBSubscriptionCallbackTest : public ::testing::Test +{ +protected: + static void SetUpTestSuite() + { + uORB::Manager::initialize(); + ASSERT_EQ(px4::WorkQueueManagerStart(), 0); + + // the manager reports running from its own thread + const hrt_abstime start = hrt_absolute_time(); + + while (px4::WorkQueueFindOrCreate(px4::wq_configurations::test1) == nullptr) { + ASSERT_LT(hrt_elapsed_time(&start), 10_s) << "work queue manager did not start"; + px4_usleep(10_ms); + } + } + + static void TearDownTestSuite() + { + px4::WorkQueueManagerStop(); + uORB::Manager::terminate(); + } + + // Each cycle is one chance to hit the window; without the fix a few thousand are plenty + static constexpr int CYCLES = 20000; +}; + +TEST_F(uORBSubscriptionCallbackTest, DestroyBlockingWhilePublishing) +{ + Publisher publisher; + + for (int i = 0; i < CYCLES; i++) { + uORB::SubscriptionBlocking sub{ORB_ID(orb_test)}; + ASSERT_TRUE(sub.registerCallback()); + } +} + +TEST_F(uORBSubscriptionCallbackTest, DestroyWorkItemSubscriptionWhilePublishing) +{ + TestWorkItem work_item; + Publisher publisher; + + for (int i = 0; i < CYCLES; i++) { + uORB::SubscriptionCallbackWorkItem sub{&work_item, ORB_ID(orb_test)}; + ASSERT_TRUE(sub.registerCallback()); + } +} + +} // namespace test +} // namespace uORB diff --git a/src/lib/drivers/mcp_common/CallbackHandler.hpp b/src/lib/drivers/mcp_common/CallbackHandler.hpp index b5b6caa141d..1cc64337c81 100644 --- a/src/lib/drivers/mcp_common/CallbackHandler.hpp +++ b/src/lib/drivers/mcp_common/CallbackHandler.hpp @@ -43,7 +43,10 @@ public: uint16_t input{0}; CallbackHandler(orb_id_t id) : uORB::SubscriptionCallback(id) {} - virtual ~CallbackHandler() {} + virtual ~CallbackHandler() + { + unregisterCallback(); + } void call(unsigned generation) override {