Skip to content

show() refresh throttle takes an unconditional deep yield — gate on NetworkDetector, or drop it now that transposition is fast (root cause of #2994) #3762

Closed

Description

@zackees

Summary

CFastLED::show()'s refresh-rate throttle is the last call site still taking an unconditional deep yield. Every other site that had this problem was already fixed — by runtime NetworkDetector gating (#2817) and by skipping the vTaskDelay(1) floor (#2493 follow-up, 9d71de01d0). show() was missed, and that is the root cause of the timing regression in #2994.

This issue proposes finishing the job, and asks a second question: whether the yield is needed at all anymore now that transposition is fast.

Current state

src/FastLED.cpp.hpp:238 (introduced in 5c8f7a1e7f, "try and fix wifi slowdown", refs #2193):

while (mNMinMicros && ((fl::micros() - lastshow) < mNMinMicros)) {
#if SKETCH_HAS_LARGE_MEMORY
    fl::task::run(250, fl::task::ExecFlags::SYSTEM);
#endif
}

Chain of costs:

  1. executor.cpp.hpp:135-141 — because microseconds > 0, this calls pumpCoroutines(1000). The caller's 250 is discarded; 1000 is hardcoded.
  2. coroutine_esp32.impl.hpp:82-85 — if (ticks == 0) { ticks = 1; } then vTaskDelay(ticks).
  3. Net: ≥1 FreeRTOS tick per loop iteration — 1 ms at CONFIG_FREERTOS_HZ=1000, 10 ms at the 100 Hz default.

At setMaxRefreshRate(800) the throttle target is 1250 µs, so every show() overshoots to a tick boundary. Measured effect in #2994: 499 shows/sequence × 0.05–0.6 ms = 25–299 ms drift, against 63–300 ms reported. 3.10.3's plain busy-wait was microsecond-accurate, which is why it reported a rock-steady 2495 ms.

This is the same defect 9d71de01d0 already described for waitForCondition, quoting its own commit message:

waitForCondition call task::run(250, SYSTEM) once per iteration to keep WiFi/lwIP alive while polling. On ESP32-P4 at CONFIG_FREERTOS_HZ=1000 this floor-rounds to vTaskDelay(1) = 1 ms minimum per iteration.

Proposal 1 — gate the show() throttle on NetworkDetector

fl::NetworkDetector already exposes exactly what is needed, and the channel wait sites already use it (driver.cpp.hpp:51, manager.cpp.hpp:477):

API Semantics
weak symbol == nullptr component not linked — free, covers most LED-only sketches
isWiFiActive() esp_wifi_get_mode() != WIFI_MODE_NULL (initialized)
isWiFiConnected() esp_wifi_sta_get_ap_info() == ESP_OK (associated)
isBluetoothActive() esp_bt_controller_get_status() == ENABLED (enabled, not merely initialized)
isAnyNetworkActive() / isAnyNetworkConnected() aggregates

Recommend isAnyNetworkActive(), matching the existing channel sites — not isAnyNetworkConnected(). Scanning and association are precisely when the WiFi stack most needs CPU; gating on Connected would skip the yield during exactly that window, reinventing the pre-connection gap the executor comment warns about.

Sub-tick handling should follow 9d71de01d0: only take the deep yield when at least a full tick remains; spin the remainder. Above ~1000 Hz refresh the entire wait is sub-tick, so today it is pure quantization noise.

Proposal 2 — dead code to resolve either way

ICoroutineRuntime::needsDeepYield() (coroutine_esp32.impl.hpp:88, overriding coroutine_runtime.h:47) implements cached WiFi detection and is called from nowhere. It duplicates NetworkDetector with weaker semantics (WIFI_MODE_NULL only, no BT, no Ethernet). Either wire it to NetworkDetector or delete it.

Separately, executor.cpp.hpp:138 ignoring its microseconds argument is a bug independent of all of the above — every caller asking for a shorter yield silently gets 1000 µs, then tick-rounded.

Open question — is the yield still needed at all?

The yield was added for #2193/#2254, when the starvation cause was long blocking transposition inside the driver. That has since been substantially optimized. If transposition no longer blocks long enough to starve lwIP, the correct fix may be to drop the yield from show() entirely rather than gate it — show()'s throttle is a timing wait, not a DMA wait, and the caller explicitly asked to be blocked until the refresh interval elapses.

This should be settled by measurement, not argument.

Proposed experiment (ESP32-C6 on the bench)

Three build variants, two network conditions:

variant show() throttle
A master (unconditional deep yield)
B NetworkDetector::isAnyNetworkActive()-gated + sub-tick spin
C no yield (3.10.3 behavior)

Conditions: (1) WiFi off, (2) WiFi connected with sustained traffic (iperf or websocket echo).

Metrics:

Decision rule:

  • If C shows no throughput regression under condition 2 → drop the yield from show(); simplest and fastest.
  • If C regresses but B does not → ship B.
  • If B also regresses → the yield is load-bearing; revisit mNMinMicros semantics instead.

Acceptance

Activity

  1. zackees commented on Jul 29, 2026

    @zackees
    MemberAuthor

    Quantified the threshold question from #3765, and the answer is a tradeoff rather than a bug — so recording the numbers here instead of picking a default unilaterally.

    The observation

    #3764's throttle guards the deep yield with remaining > spinBudget, where the default spinBudget is 250 us. But a deep yield costs up to one full FreeRTOS tick (1000 us at CONFIG_FREERTOS_HZ=1000). So when remaining lands in (250, 1000), the throttle takes a yield that can overshoot its own deadline.

    Simulation

    200k frames, setMaxRefreshRate(800) (mNMinMicros = 1250 us), 1 kHz tick, uniform tick phase:

    threshold mean overshoot worst drift / 499-show sequence spin per show spin @200 Hz
    250 us (current default) 281 us 750 us ~140 ms 31 us 0.6% CPU
    1000 us (tick period) 0 us 0 us 0 ms 500 us 10% CPU

    Why this is a tradeoff, not a defect

    The tick threshold gives exact timing — but the spin is task::run(0) = taskYIELD(), which per #2254's own analysis does not yield to lower-priority tasks. lwIP sits below the LED task. So the precise variant burns ~10% CPU in exactly the way that starves WiFi, which is the thing #2254 fixed.

    On a tick-quantized RTOS you cannot have both microsecond-accurate throttling and full lower-priority yielding. The current 250 us default buys WiFi throughput at the cost of ~140 ms/sequence of jitter for radio-up users; 1000 us inverts that.

    The knob already exists — FastLED.setWaitSpinBudgetUs(1000) gets exact timing today for anyone who wants it, and setWaitSpinBudgetUs(0) disables spinning entirely for maximum throughput. So this is a documented-default question, not missing capability.

    Recommendation

    Leave the default at 250 us. It preserves the #2254 fix, which is a correctness property, over timing precision, which is tunable. Worth a doc note on setMaxRefreshRate that with a radio active the throttle is tick-quantized and setWaitSpinBudgetUs trades CPU for precision.

    Caveat worth stating

    This matters much less than I implied when I opened #3765. The throttle provably does not engage in #2994's sketch (show() is gated at 5 ms, far above the 1250 us threshold), so none of the above explains that report. The real #2994 investigation is tracked in #3765 and now points at show() duration — encoding, DMA, or a driver wait — with ci/autoresearch/test_timing_drift.py (#3768) ready to measure it once a board is back on the bench.

  2. zackees commented on Aug 20, 2026

    @zackees
    MemberAuthor

    Burn-down disposition: COMPLETED

    The actionable scope of this issue landed in two merged PRs:

    Current src/FastLED.cpp.hpp and src/fl/task/executor.cpp.hpp contain those behaviors. The remaining threshold choice is intentionally user-tunable: 250 us favors network progress, while a tick-sized spin budget favors tighter timing.

    Important correction from the issue thread: the refresh throttle was not the root cause of #2994 because that reproducer's 5 ms cadence never engages the 1.25 ms throttle. #2994 remains a separate, deferred hardware-timing investigation; it is not a reason to keep this already-fixed deep-yield issue open.

  3. moved this from Triage to Done in FastLED Trackeron Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions