Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#6040·fprime

Svc/ActiveRateGroup: data race on m_cycleStarted and missed cycle slips under queue delay

Author: sjsreehariCreated Sep 24, 2026Updated Sep 27, 2026
Labelsbug
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:

  1. Data Race / C++14 Memory Model Violation: m_cycleStarted is declared in Svc/ActiveRateGroup/ActiveRateGroup.hpp:120:

    cpp
    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., RateGroupDriver task, Linux timer thread, or hardware timer ISR) and writes this->m_cycleStarted = true. Concurrently, CycleIn_handler (ActiveRateGroup.cpp:61-114) executes on the ActiveRateGroup worker thread, writing this->m_cycleStarted = false at line 67 and reading if (this->m_cycleStarted) at line 98. Per ISO C++14 (§ 1.10 [intro.multithread]), volatile does 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 in CycleIn_preMsgHook is not guaranteed to be visible to CycleIn_handler without proper memory ordering.

  2. Missed Cycle Slips Under Queue Delay (Flawed Detection Logic): The component relies on CycleIn_preMsgHook setting m_cycleStarted = true when a cycle message arrives, and clears it at the start of CycleIn_handler:

    cpp
    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 ActiveRateGroup task is delayed before dequeuing (e.g., due to CPU scheduling contention, higher-priority task preemption, or queue backlog):

    • Cycle N arrives: CycleIn_preMsgHook sets m_cycleStarted = true and enqueues message N.
    • Cycle N+1 arrives before message N is dequeued: CycleIn_preMsgHook sets m_cycleStarted = true again 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) to false.
    • Result: The cycle slip is completely missed. The RateGroupCycleSlip warning event is never logged, m_cycleSlips is never incremented, and telemetry is not updated.
  3. Lost Update Race at Handler Reset: If a new cycle tick arrives and CycleIn_preMsgHook executes on the driver thread at the exact moment the worker thread executes line 67 (m_cycleStarted = false), the write m_cycleStarted = true can 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

  1. Deploy Svc::ActiveRateGroup driven periodically by a rate group driver or timer.
  2. Introduce a temporary delay or preempt the ActiveRateGroup task so that two cycle ticks are queued before the worker thread begins processing the first cycle.
  3. Observe that after processing both cycles, RgCycleSlips telemetry remains 0 and no RateGroupCycleSlip event is emitted, despite an entire cycle period being slipped.
  4. 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

  1. Access to cycle overrun flags across threads must be thread-safe (e.g., using std::atomic with explicit memory order semantics, matching recent hardening in Svc/CmdDispatcher m_numCmdsDropped).
  2. 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_cycleStarted with an atomic tracking mechanism (e.g., an atomic counter std::atomic<U32> m_pendingCycles or std::atomic<bool> m_cycleOverrun with std::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_handler entry are accounted for.

Source: nasa/fprime

View original on GitHubView discussion on GitHub