Repository navigation
feat(esp32): honor CoroutineConfig::core_id for task affinity - #2337
Conversation
Resolves KBI001 lint failure introduced with the hook. Follows the project convention documented in ci/compiled_size.py — catch KeyboardInterrupt first, delegate to handle_keyboard_interrupt, and re-raise so Ctrl+C still terminates. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Previously the ESP32 coroutine backend accepted CoroutineConfig::core_id through the full createTaskCoroutine plumbing but discarded it at the FreeRTOS boundary — every task was created with tskNO_AFFINITY. This is phase 1 of issue #2308 option (B): make the existing core_id knob functional on dual-core ESP32 variants so callers can opt into pinning audio (or any coroutine) onto the idle core, leaving the other free for FastLED.show(). Changes: - Add FL_CPU_CORES and FL_HAS_MULTICORE_AFFINITY feature flags in src/platforms/esp/32/feature_flags/enabled.h. Derived from SOC_CPU_CORES_NUM on IDF 4.0+, with an explicit variant-list fallback for IDF 3.x (ESP32dev/S3/P4 are dual-core; S2/C2/C3/C5/C6/H2 are single-core). - TaskCoroutineESP32::create now validates core_id against FL_CPU_CORES and passes it through to xTaskCreateStaticPinnedToCore / xTaskCreatePinnedToCore. Out-of-range or negative values downgrade to tskNO_AFFINITY, preserving existing behaviour for every current caller (all of which pass the -1 default). No behaviour change for callers that do not set core_id. An audio-side opt-in API (Processor::setCoreAffinity) is deliberately left for a follow-up PR so it can land with hardware FPS measurements. Compile-verified: esp32dev (dual-core Xtensa LX6) and esp32c3 (single-core RISC-V) via bash compile Blink. Refs #2308 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
📝 WalkthroughWalkthroughAdded keyboard interrupt handling to a CI script, introduced CPU core affinity support for ESP32 coroutines with a new Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/platforms/esp/32/coroutine_esp32.impl.hpp (1)
185-193: Validation logic is clean — correctness depends onFL_CPU_CORESbeing RTOS-accurate.The resolution from
core_idtoBaseType_t affinity(including the graceful fallback totskNO_AFFINITYfor negative / out-of-range values) looks good and preserves prior behavior for the default (-1) caller. One thing to be aware of: correctness of the upper bound checkcore_id < FL_CPU_COREShinges entirely onFL_CPU_CORESreflecting the RTOS-usable core count rather than the silicon core count — see the separate comment onfeature_flags/enabled.hregardingCONFIG_FREERTOS_UNICORE. Once that's addressed, this block is correct.Minor nit: the behavior of silently downgrading an explicit, out-of-range
core_idis arguably surprising to a caller who intentionally requests e.g. core 1. Consider at least anFL_WARNwhen the requested affinity is ignored, so misconfiguration on single-core chips is visible in logs rather than silently running unpinned:💡 Optional: warn on silent downgrade
BaseType_t affinity = tskNO_AFFINITY; if (core_id >= 0 && core_id < FL_CPU_CORES) { affinity = static_cast<BaseType_t>(core_id); + } else if (core_id >= 0) { + FL_WARN("TaskCoroutineESP32: requested core_id=" << core_id + << " is out of range (FL_CPU_CORES=" << FL_CPU_CORES + << "); falling back to tskNO_AFFINITY for '" << name << "'"); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/platforms/esp/32/coroutine_esp32.impl.hpp` around lines 185 - 193, The current core resolution silently downgrades an out-of-range or negative core_id to tskNO_AFFINITY; add a warning log so callers know their requested affinity was ignored: in the block that computes BaseType_t affinity from core_id (using FL_CPU_CORES and tskNO_AFFINITY), detect when core_id is >= 0 but >= FL_CPU_CORES and emit an FL_WARN (or equivalent logger) stating the requested core_id was out-of-range and affinity will be tskNO_AFFINITY, leaving the existing fallback behavior otherwise unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/platforms/esp/32/feature_flags/enabled.h`:
- Around line 40-55: FL_CPU_CORES should reflect the number of cores FreeRTOS
can actually schedule on, not just silicon cores; change the FL_CPU_CORES
definition to respect CONFIG_FREERTOS_UNICORE (set to 1 when
CONFIG_FREERTOS_UNICORE is defined, otherwise use SOC_CPU_CORES_NUM) and keep
FL_HAS_MULTICORE_AFFINITY derived from that FL_CPU_CORES value so
coroutine_esp32.impl.hpp's core_id validation (core_id >= 0 && core_id <
FL_CPU_CORES) and xTaskCreatePinnedToCore calls won't accept invalid core IDs.
---
Nitpick comments:
In `@src/platforms/esp/32/coroutine_esp32.impl.hpp`:
- Around line 185-193: The current core resolution silently downgrades an
out-of-range or negative core_id to tskNO_AFFINITY; add a warning log so callers
know their requested affinity was ignored: in the block that computes BaseType_t
affinity from core_id (using FL_CPU_CORES and tskNO_AFFINITY), detect when
core_id is >= 0 but >= FL_CPU_CORES and emit an FL_WARN (or equivalent logger)
stating the requested core_id was out-of-range and affinity will be
tskNO_AFFINITY, leaving the existing fallback behavior otherwise unchanged.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 80ce1b2b-1903-4e9c-84c3-4e74e3bfb9d5
📒 Files selected for processing (3)
ci/hooks/check_pr_merge_reviews.pysrc/platforms/esp/32/coroutine_esp32.impl.hppsrc/platforms/esp/32/feature_flags/enabled.h
| // FL_CPU_CORES: Number of CPU cores available for RTOS task pinning. | ||
| // FL_HAS_MULTICORE_AFFINITY: 1 when the platform supports xTaskCreatePinnedToCore | ||
| // with a core argument other than tskNO_AFFINITY (i.e. has >=2 cores). | ||
| // | ||
| // Used by platforms/esp/32/coroutine_esp32.impl.hpp to validate | ||
| // CoroutineConfig::core_id. Out-of-range requests fall back to tskNO_AFFINITY. | ||
| #ifndef FL_CPU_CORES | ||
| #define FL_CPU_CORES SOC_CPU_CORES_NUM | ||
| #endif | ||
| #ifndef FL_HAS_MULTICORE_AFFINITY | ||
| #if FL_CPU_CORES > 1 | ||
| #define FL_HAS_MULTICORE_AFFINITY 1 | ||
| #else | ||
| #define FL_HAS_MULTICORE_AFFINITY 0 | ||
| #endif | ||
| #endif |
There was a problem hiding this comment.
🧩 Analysis chain
🌐 Web query:
ESP-IDF xTaskCreatePinnedToCore CONFIG_FREERTOS_UNICORE assertion core 1
💡 Result:
When CONFIG_FREERTOS_UNICORE is enabled in ESP-IDF, FreeRTOS runs in single-core mode on Core 0 only, even on multi-core chips like ESP32. Attempting to create a task pinned to core 1 using xTaskCreatePinnedToCore(..., 1) triggers an assertion failure because core 1 is invalid in unicore mode. The xTaskCreatePinnedToCore function includes this check at the start: configASSERT( taskVALID_CORE_ID( xCoreID ) == pdTRUE || xCoreID == tskNO_AFFINITY ); The macro taskVALID_CORE_ID(xCoreID) returns true only if 0 <= xCoreID < configNUMBER_OF_CORES. With CONFIG_FREERTOS_UNICORE enabled, configNUMBER_OF_CORES is 1, so taskVALID_CORE_ID(1) is false. If xCoreID is 1 (not tskNO_AFFINITY), the assertion fails, typically printing "assertion failed: taskVALID_CORE_ID" or similar. Official docs confirm: in unicore mode, SMP APIs like ...PinnedToCore ignore the xCoreID argument or only accept 0. To fix, either disable CONFIG_FREERTOS_UNICORE (run idf.py menuconfig > Component config > FreeRTOS > Run FreeRTOS only on first core) to enable dual-core, or pin tasks to core 0 / use tskNO_AFFINITY (task will run on core 0).
Citations:
- 1: https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/freertos_idf.html
- 2: https://docs.espressif.com/projects/esp-idf/en/v6.0/esp32p4/api-reference/system/freertos_idf.html
- 3: https://github.com/espressif/esp-idf/blob/12f36a02/components/freertos/esp_additions/freertos_tasks_c_additions.h
- 4: https://github.com/espressif/esp-idf/blob/12f36a02/components/freertos/esp_additions/include/freertos/idf_additions.h
🏁 Script executed:
# Check the top of enabled.h to confirm sdkconfig.h inclusion
head -n 20 src/platforms/esp/32/feature_flags/enabled.hRepository: FastLED/FastLED
Length of output: 599
🏁 Script executed:
# Search for the validation logic in coroutine_esp32.impl.hpp
rg "core_id" src/platforms/esp/32/coroutine_esp32.impl.hpp -A 3 -B 3Repository: FastLED/FastLED
Length of output: 1556
🏁 Script executed:
# Check if FL_CPU_CORES or FL_HAS_MULTICORE_AFFINITY is used in validation
rg "FL_CPU_CORES|FL_HAS_MULTICORE_AFFINITY" src/platforms/esp/32/ -A 2 -B 2Repository: FastLED/FastLED
Length of output: 1914
FL_CPU_CORES must account for CONFIG_FREERTOS_UNICORE to match documented contract.
SOC_CPU_CORES_NUM reflects silicon cores, but the header comment (line 40) states the intent is "Number of CPU cores available for RTOS task pinning." When CONFIG_FREERTOS_UNICORE=y (a supported configuration on dual-core ESP32/S3), FreeRTOS sets configNUMBER_OF_CORES=1 and only schedules on core 0. In that case, the validation in coroutine_esp32.impl.hpp (if (core_id >= 0 && core_id < FL_CPU_CORES)) will accept core_id=1 because FL_CPU_CORES==2, but xTaskCreatePinnedToCore(..., 1) will trigger an assertion failure inside ESP-IDF with taskVALID_CORE_ID(1) == false.
Since sdkconfig.h is already included, gate FL_CPU_CORES on CONFIG_FREERTOS_UNICORE:
Proposed fix
`#ifndef` FL_CPU_CORES
+#if defined(CONFIG_FREERTOS_UNICORE) && CONFIG_FREERTOS_UNICORE
+#define FL_CPU_CORES 1
+#else
`#define` FL_CPU_CORES SOC_CPU_CORES_NUM
`#endif`
+#endif🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/platforms/esp/32/feature_flags/enabled.h` around lines 40 - 55,
FL_CPU_CORES should reflect the number of cores FreeRTOS can actually schedule
on, not just silicon cores; change the FL_CPU_CORES definition to respect
CONFIG_FREERTOS_UNICORE (set to 1 when CONFIG_FREERTOS_UNICORE is defined,
otherwise use SOC_CPU_CORES_NUM) and keep FL_HAS_MULTICORE_AFFINITY derived from
that FL_CPU_CORES value so coroutine_esp32.impl.hpp's core_id validation
(core_id >= 0 && core_id < FL_CPU_CORES) and xTaskCreatePinnedToCore calls won't
accept invalid core IDs.
📊 Header Compilation PerformanceTotal Time: 1200.97ms Top 5 Slowest Headers
Warnings
Full Report |
Summary
Phase 1 of #2308 option (B) — restore audio-pipeline FPS by letting callers pin coroutines to a specific core on dual-core ESP32s.
The
CoroutineConfig::core_idfield has existed onfl::taskfor a while, but the ESP32 backend silently discarded it (int /*core_id*/) and always usedtskNO_AFFINITY. This PR makes that parameter actually work.Changes
src/platforms/esp/32/feature_flags/enabled.h):FL_CPU_CORES— number of RTOS-usable cores. Derived fromSOC_CPU_CORES_NUMon IDF 4.0+, with an explicit ESP32dev/S3/P4 vs S2/C2/C3/C5/C6/H2 fallback for IDF 3.x.FL_HAS_MULTICORE_AFFINITY—1whenFL_CPU_CORES > 1.src/platforms/esp/32/coroutine_esp32.impl.hpp):TaskCoroutineESP32::create()gains acore_idparameter (default-1).core_idis validated againstFL_CPU_CORES. Out-of-range or negative values downgrade totskNO_AFFINITY, matching prior behaviour for every existing caller.xTaskCreateStaticPinnedToCore/xTaskCreatePinnedToCore.createTaskCoroutine()entry point now forwardscore_idinstead of discarding it.What this unlocks
Once landed, a caller can do:
On ESP32-dev / S3 / P4 this pins to core 0. On single-core variants (S2/C3/etc.) the
0stays valid (core 0 is the only core); any out-of-range request silently falls back to no-affinity.What this does NOT change
createTaskCoroutinecall passes the defaultcore_id = -1, which maps totskNO_AFFINITY— identical to today.Processor::createWithAutoInputstill usesfl::task::every_ms(1)(main scheduler thread). Converting that to a pinned coroutine changes the thread on which user callbacks fire, which can race withFastLED.show()if a callback touches LEDs. That opt-in API (Processor::setCoreAffinity) needs hardware FPS measurements and is deliberately left for a follow-up.Test plan
bash compile esp32dev --examples Blink(dual-core Xtensa LX6)bash compile esp32c3 --examples Blink(single-core RISC-V)Also in this PR
One unrelated lint fix:
ci/hooks/check_pr_merge_reviews.pywas missing aKeyboardInterrupthandler (KBI001 was flagging on every stop hook). Separate commit, follows the existing convention inci/compiled_size.py.Refs #2308
🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Chores