Svc/ActiveRateGroup: data race on m_cycleStarted and missed cycle slips under queue delay
| F´ Version | devel @ 9416fdad0 |
| Affected Component | Svc/ActiveRateGroup |
Problem Description
In Svc/ActiveRateGroup, the cycle slip (overrun) detection mechanism uses a raw volatile bool m_cycleStarted across thread boundaries without atomic synchronization. This causes two distinct issues:
Data Race / C++14 Memory Model Violation:
m_cycleStartedis declared inSvc/ActiveRateGroup/ActiveRateGroup.hpp:120:volatile bool m_cycleStarted; //!< indicate that cycle has started. Used to detect overruns.CycleIn_preMsgHook(ActiveRateGroup.cpp:116-119) executes synchronously on the invoking thread (e.g.,RateGroupDrivertask, Linux timer thread, or hardware timer ISR) and writesthis->m_cycleStarted = true. Concurrently,CycleIn_handler(ActiveRateGroup.cpp:61-114) executes on theActiveRateGroupworker thread, writingthis->m_cycleStarted = falseat line 67 and readingif (this->m_cycleStarted)at line 98. Per ISO C++14 (§ 1.10 [intro.multithread]),volatiledoes not provide atomic access, memory fences, or thread synchronization. Concurrent access from multiple threads without synchronization constitutes a data race (Undefined Behavior). On weakly ordered multi-core platforms (e.g., ARM Cortex SMP), the write inCycleIn_preMsgHookis not guaranteed to be visible toCycleIn_handlerwithout proper memory ordering.Missed Cycle Slips Under Queue Delay (Flawed Detection Logic): The component relies on
CycleIn_preMsgHooksettingm_cycleStarted = truewhen a cycle message arrives, and clears it at the start ofCycleIn_handler:void ActiveRateGroup::CycleIn_handler(FwIndexType portNum, Os::RawTime& cycleStart) { ... this->m_cycleStarted = false; // Line 67 // invoke any members of the rate group for (FwIndexType port = 0; port < this->m_numContexts; port++) { ... } ... // check for cycle slip if (this->m_cycleStarted) { // Line 98 this->m_cycleSlips++; ... }If the
ActiveRateGrouptask is delayed before dequeuing (e.g., due to CPU scheduling contention, higher-priority task preemption, or queue backlog):- Cycle N arrives:
CycleIn_preMsgHooksetsm_cycleStarted = trueand enqueues message N. - Cycle N+1 arrives before message N is dequeued:
CycleIn_preMsgHooksetsm_cycleStarted = trueagain and enqueues message N+1. A cycle slip has already occurred because the rate group missed its scheduled execution window. - When the worker task finally wakes up to process message N, line 67 immediately executes:
this->m_cycleStarted = false; - This unconditionally wipes out the slip indicator. If message N's member invocations complete before cycle N+2 arrives, line 98 evaluates
if (this->m_cycleStarted)tofalse. - Result: The cycle slip is completely missed. The
RateGroupCycleSlipwarning event is never logged,m_cycleSlipsis never incremented, and telemetry is not updated.
- Cycle N arrives:
Lost Update Race at Handler Reset: If a new cycle tick arrives and
CycleIn_preMsgHookexecutes on the driver thread at the exact moment the worker thread executes line 67 (m_cycleStarted = false), the writem_cycleStarted = truecan be clobbered by the clear operation, silently losing the overrun flag.
Context / Environment
- Operating System: Linux / Darwin / RTOS (FreeRTOS, VxWorks)
- Target Architectures: x86_64, ARM (SMP), RISC-V
- F´ Commit: devel branch (9416fdad0)
- Components involved: Svc/ActiveRateGroup, Svc/RateGroupDriver
How to Reproduce
- Deploy
Svc::ActiveRateGroupdriven periodically by a rate group driver or timer. - Introduce a temporary delay or preempt the
ActiveRateGrouptask so that two cycle ticks are queued before the worker thread begins processing the first cycle. - Observe that after processing both cycles,
RgCycleSlipstelemetry remains0and noRateGroupCycleSlipevent is emitted, despite an entire cycle period being slipped. - Run ThreadSanitizer (TSan) on unit tests or multi-threaded integration runs exercising concurrent cycle delivery to observe the data race report on
m_cycleStarted.
Expected Behavior
- Access to cycle overrun flags across threads must be thread-safe (e.g., using
std::atomicwith explicit memory order semantics, matching recent hardening inSvc/CmdDispatcherm_numCmdsDropped). - Cycle slips resulting from queue backlog or scheduling latency must be faithfully detected and reported, rather than erased by unconditional resets at the entry of
CycleIn_handler.
Suggested Fix
- Replace
volatile bool m_cycleStartedwith an atomic tracking mechanism (e.g., an atomic counterstd::atomic<U32> m_pendingCyclesorstd::atomic<bool> m_cycleOverrunwithstd::memory_order_acquire/std::memory_order_release). - Detect backlog either by querying queue depth or comparing pending cycle counts so that delays occurring before
CycleIn_handlerentry are accounted for.
Source: nasa/fprime