mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-11-24 16:01:53 +08:00
fix(WorkQueue): fix lifetime race between Run() and destruction
Two related fixes for HRT callout queue corruption under lockstep SIH: 1. WorkQueue::Run() popped a WorkItem, released the work-queue lock, then called work->Run(). If another thread called Deinit on the item in that window, wq->Remove was a no-op (item already popped) and Deinit returned while Run() was still executing. Run() could then call ScheduleDelayed → hrt_call_after on an object that the caller of Deinit has since destructed and reused via placement new — the hrt_call metadata ends up living inside another object's fields, which later overwrite the flink pointer and tear the hrt callout queue. Fix: WorkItem carries a _run_in_progress atomic flag set by WorkQueue::Run around work->Run(). Deinit spins on that flag after wq->Remove so it cannot return while a Run() is still executing. WorkQueue captures its worker tid in the constructor (the ctor runs on the worker thread) so Deinit can skip the self-wait when called from inside Run() itself (e.g. should_exit() paths that invoke ScheduleClear). 2. _hrt_lock was a px4_sem_t and hrt_call_invoke unlocked around the callback so callbacks could re-enter hrt_call_*. That also exposed the queue to other threads mid-invocation. Switch _hrt_lock to a PTHREAD_MUTEX_RECURSIVE pthread_mutex and hold it across the callback — matches NuttX's enter_critical_section() nesting semantics, lets callbacks reschedule themselves safely, and prevents concurrent queue manipulation. Together these eliminate the HRT queue tearing observed in the SIH-at-20x MAVSDK integration soak (torn flink chains with the tail unreachable from the head; orphan nodes pointing into reused memory).
This commit is contained in:
1 parent
da52562708
commit
9b62a1701d
5 files changed
+84
-15
No files matched your search
@@ -47,6 +47,12 @@ namespace px4
|
||||
WorkQueue::WorkQueue(const wq_config_t &config) :
|
||||
_config(config)
|
||||
{
|
||||
// WorkQueue is constructed on its own worker thread (see
|
||||
// WorkQueueRunner in WorkQueueManager.cpp), so pthread_self() here
|
||||
// captures the worker's tid for later self-wait checks in
|
||||
// WorkItem::Deinit.
|
||||
_runner_tid = pthread_self();
|
||||
|
||||
// set the threads name
|
||||
#ifdef __PX4_DARWIN
|
||||
pthread_setname_np(_config.name);
|
||||
@@ -144,6 +150,17 @@ void WorkQueue::Add(WorkItem *item)
|
||||
SignalWorkerThread();
|
||||
}
|
||||
|
||||
void WorkQueue::request_stop()
|
||||
{
|
||||
_should_exit.store(true);
|
||||
// Wake the worker so Run() re-checks should_exit() and returns. Without
|
||||
// this, a queue parked in px4_sem_wait() (no pending work) never notices
|
||||
// the stop request — e.g. when WorkQueueManagerStop() asks every queue to
|
||||
// stop while WorkItems are still attached (so the Detach self-stop path
|
||||
// never ran). The worker would block forever and shutdown would hang.
|
||||
SignalWorkerThread();
|
||||
}
|
||||
|
||||
void WorkQueue::SignalWorkerThread()
|
||||
{
|
||||
int sem_val;
|
||||
@@ -183,10 +200,17 @@ void WorkQueue::Run()
|
||||
while (!_q.empty()) {
|
||||
WorkItem *work = _q.pop();
|
||||
|
||||
// Mark as in-flight so ~WorkItem / Deinit on another thread
|
||||
// can wait for us to return before the item's memory is torn
|
||||
// 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);
|
||||
|
||||
work_unlock(); // unlock work queue to run (item may requeue itself)
|
||||
work->RunPreamble();
|
||||
work->Run();
|
||||
// Note: after Run() we cannot access work anymore, as it might have been deleted
|
||||
work->_run_in_progress.store(false);
|
||||
work_lock(); // re-lock
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user