From cc28d5184941869e7e2e8c0b3e4016c063dff76e Mon Sep 17 00:00:00 2001 From: Jacob Dahl <37091262+dakejahl@users.noreply.github.com> Date: Sat, 31 Oct 2026 08:24:52 -0800 Subject: [PATCH] fix(px4_work_queue): don't touch a WorkItem after its Run() returns (#29278) (#29531) Modules that stop through ModuleBase::exit_and_cleanup() delete themselves inside Run(), and since 9b62a1701d WorkQueue::Run() then clears _run_in_progress in the freed object. ASAN aborts SITL on `battery_simulator stop` and the stop of most other work queue modules. Track the in-flight item in the WorkQueue instead, which lives until its own Run() exits on the worker thread. Assisted-by: Claude:claude-opus-5-5 (cherry picked from commit 8c5f4e07888a9a6b6379d224b6c33a3229674d98) --- .../px4_platform_common/px4_work_queue/WorkItem.hpp | 5 ----- .../px4_platform_common/px4_work_queue/WorkQueue.hpp | 9 +++++++++ platforms/common/px4_work_queue/WorkItem.cpp | 6 +++--- platforms/common/px4_work_queue/WorkQueue.cpp | 5 +++-- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/platforms/common/include/px4_platform_common/px4_work_queue/WorkItem.hpp b/platforms/common/include/px4_platform_common/px4_work_queue/WorkItem.hpp index 76d34ad2f64..ddcfd01045d 100644 --- a/platforms/common/include/px4_platform_common/px4_work_queue/WorkItem.hpp +++ b/platforms/common/include/px4_platform_common/px4_work_queue/WorkItem.hpp @@ -132,11 +132,6 @@ protected: const char *_item_name; uint32_t _run_count{0}; - // True while a WorkQueue worker is inside Run() for this item. - // Set before Run(), cleared after. Used by Deinit to wait out any - // in-flight Run() before the owning object's memory can be reused. - px4::atomic_bool _run_in_progress{false}; - private: WorkQueue *_wq{nullptr}; diff --git a/platforms/common/include/px4_platform_common/px4_work_queue/WorkQueue.hpp b/platforms/common/include/px4_platform_common/px4_work_queue/WorkQueue.hpp index 0c269aa6dc1..061f3ac951c 100644 --- a/platforms/common/include/px4_platform_common/px4_work_queue/WorkQueue.hpp +++ b/platforms/common/include/px4_platform_common/px4_work_queue/WorkQueue.hpp @@ -76,6 +76,10 @@ public: // Used by WorkItem::Deinit to avoid self-wait when called from inside Run(). pthread_t runner_tid() const { return _runner_tid; } + // True while the worker is inside item->Run(). Used by WorkItem::Deinit + // to wait out an in-flight Run() before the item's memory can be reused. + bool is_running(const WorkItem *item) const { return _running_item.load() == item; } + void request_stop(); void print_status(bool last = false); @@ -109,6 +113,11 @@ private: px4::atomic_bool _should_exit{false}; pthread_t _runner_tid{}; + // Tracked here, not in the WorkItem: an item may delete itself inside + // Run() (ModuleBase::exit_and_cleanup), so the worker must not touch it + // once Run() returns. + px4::atomic _running_item{nullptr}; + #if defined(ENABLE_LOCKSTEP_SCHEDULER) int _lockstep_component {-1}; #endif // ENABLE_LOCKSTEP_SCHEDULER diff --git a/platforms/common/px4_work_queue/WorkItem.cpp b/platforms/common/px4_work_queue/WorkItem.cpp index a84b4f34aab..a949efe3ea4 100644 --- a/platforms/common/px4_work_queue/WorkItem.cpp +++ b/platforms/common/px4_work_queue/WorkItem.cpp @@ -104,10 +104,10 @@ void WorkItem::Deinit() // and corrupts the hrt callout queue. // // Skip the wait if we're being called from inside Run() on the - // worker thread itself (e.g. should_exit() → ScheduleClear), - // which would deadlock against our own flag. + // worker thread itself (e.g. should_exit() → exit_and_cleanup), + // which would deadlock waiting on ourselves. if (!pthread_equal(pthread_self(), wq_temp->runner_tid())) { - while (_run_in_progress.load()) { + while (wq_temp->is_running(this)) { px4_usleep(100); } } diff --git a/platforms/common/px4_work_queue/WorkQueue.cpp b/platforms/common/px4_work_queue/WorkQueue.cpp index bd85be267da..a85b1138ab6 100644 --- a/platforms/common/px4_work_queue/WorkQueue.cpp +++ b/platforms/common/px4_work_queue/WorkQueue.cpp @@ -205,12 +205,13 @@ void WorkQueue::Run() // down. Without this, a Run() executing after Deinit has // popped the item will call Schedule* on dead memory, // corrupting the hrt callout queue. - work->_run_in_progress.store(true); + _running_item.store(work); work_unlock(); // unlock work queue to run (item may requeue itself) work->RunPreamble(); work->Run(); - work->_run_in_progress.store(false); + // Note: after Run() we cannot access work anymore, as it might have been deleted + _running_item.store(nullptr); work_lock(); // re-lock }