Repository navigation
Description
Board / MCU
ESP32
LED Chipset
N/A
FastLED Version
3.10.8
Arduino / PlatformIO version
PlatformIO Core 6.2.0.post1 with the pioarduino platform 53.03.10 (Arduino-ESP32 3.1.0, ESP-IDF 5.3.2, GCC 13.2.0 esp-13.2.0_20240530)
Operating System
macOS
Description
I hit this issue on a project of mine. Claude helped me diagnose it and write the repro below. I've confirmed that the repro works locally on my hardware, and I think what it's saying is pretty credible in this case.
On ESP32, led_sysdefs_esp32.h defines NEED_CXX_BITS, which makes FastLED.cpp define weak __cxa_guard_acquire(), __cxa_guard_release() and __cxa_guard_abort(). These are the C++ ABI functions that make function-local statics safe to initialize from several threads, but FastLED's versions don't lock: they only check and set the guard byte.
ESP-IDF (and so Arduino-ESP32) already provides thread-safe versions of these functions. Normally the linker picks those over FastLED's weak ones. But once FastLED is compiled with LTO, the firmware ends up with FastLED's versions inlined everywhere and ESP-IDF's versions gone. Every function-local static in the program then stops being thread-safe.
I expected a function-local static to be constructed once, with other threads waiting for it. Instead, two threads that reach it at the same time both construct it.
Where
#define NEED_CXX_BITS Lines 894 to 925 in 91009b8
#ifdef NEED_CXX_BITS namespace __cxxabiv1 { #if !defined(FL_IS_ESP8266) && !defined(FL_IS_ESP32) && !defined(ARDUINO) extern "C" void __cxa_pure_virtual (void) {} #endif /* guard variables */ /* The ABI requires a 64-bit type. */ __extension__ typedef int __guard __attribute__((mode(__DI__))); extern "C" int __cxa_guard_acquire (__guard *) FL_LINK_WEAK; extern "C" void __cxa_guard_release (__guard *) FL_LINK_WEAK; extern "C" void __cxa_guard_abort (__guard *) FL_LINK_WEAK; extern "C" int __cxa_guard_acquire (__guard *g) { return !*(char *)(g); } extern "C" void __cxa_guard_release (__guard *g) { *(char *)g = 1; } extern "C" void __cxa_guard_abort (__guard *) { } } #endif
How I hit it
My project runs on an ESP32-C6 with FastLED 3.10.3 and the same PlatformIO and Arduino-ESP32 versions as above (with the RISC-V build of GCC 13.2.0), built with LTO for both its sources
and its libraries, including FastLED (see the lto.py script in the steps below). Its Bluetooth singleton is a
function-local static, and the constructor starts Bluedroid. On the single-core C6, Bluedroid's task delivered a GAP event before the constructor returned. The callback called the same accessor and, instead of waiting for the main task to finish constructing the static, ran the constructor a second time. That called esp_bt_controller_init() twice, and the device crashlooped with ESP_ERROR_CHECK failed: esp_err_t 0x103 (ESP_ERR_INVALID_STATE) on esp_bt_controller_init(&cfg).
What I found in that project's builds:
- Its object files still call the guard functions under LTO:
gcc-nmon its LTO objects lists
U __cxa_guard_acquireandU __cxa_guard_release. - The LTO firmware doesn't contain them.
nm firmware.elf | grep __cxa_guardonly lists ESP-IDF's
__cxa_guard_dummy. The accessor's disassembly is just the compiler's acquire-load of the guard byte, a second plain
load (FastLED'sreturn !*(char *)(g);) and a branch into the constructor, with no call and no lock. - It's the same for every Arduino build of the project I checked with LTO: classic ESP32, ESP32-S3 and ESP32-C6.
- One exception: a build that uses ESP-IDF without the Arduino core, with FastLED compiled the same way, still
contained__cxa_guard_acquire. - Without LTO, ESP-IDF's version is back in every one of those builds:
nmlistsT __cxa_guard_acquire, and the
C6 boots normally.
Suggested fix
Don't define NEED_CXX_BITS on ESP32. ESP-IDF's cxx component always provides thread-safe __cxa_guard_*, and the
ESP32 branch already skips FastLED's __cxa_pure_virtual for the same reason. ESP8266 also defines NEED_CXX_BITS
(led_sysdefs_esp8266.h), and #266 reported that the ESP8266 core started providing these functions too.
Workaround
Don't compile FastLED with LTO.
Steps to reproduce
-
Create a PlatformIO project with this
platformio.ini, which has one env with LTO and one without:[env] platform = https://github.com/pioarduino/platform-espressif32/releases/download/53.03.10/platform-espressif32.zip board = m5stack-atom framework = arduino lib_deps = fastled/[email protected] upload_speed = 1500000 monitor_speed = 115200 monitor_filters = esp32_exception_decoder, time [env:nolto] [env:lto] build_src_flags = -flto=auto extra_scripts = post:lto.py
-
Next to it, create
lto.py, which also compiles libraries, including FastLED, with LTO:Import("env") for lib_builder in env.GetLibBuilders(): lib_builder.env.Append(CCFLAGS=["-flto=auto"]) # The prebuilt Arduino ld_flags contain -fno-lto. env["LINKFLAGS"] = [flag for flag in env["LINKFLAGS"] if flag != "-fno-lto"] env.Append(LINKFLAGS=["-flto=auto"]) ar = env.subst("$AR") if ar.endswith("-ar") and not ar.endswith("-gcc-ar"): env.Replace(AR=ar[: -len("-ar")] + "-gcc-ar", RANLIB=ar[: -len("-ar")] + "-gcc-ranlib")
-
Put the minimal sketch below in
src/main.cpp. The FastLED calls only make sure FastLED, and so its guard functions,
get linked in. No LEDs need to be connected. -
Run
pio run -e lto -t upload -t monitor. It printsconstructions: 2: both tasks ran the constructor. -
Run
pio run -e nolto -t upload -t monitor. It printsconstructions: 1: the second task waited for the first. -
Optionally, run
xtensa-esp32-elf-nm .pio/build/<env>/firmware.elf | grep __cxa_guard_acquirefor each env. It
prints nothing forlto, and ESP-IDF'sT __cxa_guard_acquirefornolto.
Minimal sketch
#include <FastLED.h>
// Any pin works; this only makes sure FastLED, and its guard functions, get linked in.
constexpr uint8_t kLedPin = 27;
CRGB leds[1];
static volatile int gConstructions = 0;
struct Slow {
Slow() {
gConstructions++;
delay(200); // Gives the other task time to reach Get() while we're still constructing.
}
};
Slow& Get() {
static Slow sSlow;
return sSlow;
}
void OtherTask(void*) {
Get();
vTaskDelete(nullptr);
}
void setup() {
Serial.begin(115200);
FastLED.addLeds<WS2812B, kLedPin, GRB>(leds, 1);
FastLED.show();
xTaskCreate(OtherTask, "other", 4096, nullptr, 1, nullptr);
Get();
}
void loop() {
// Printed repeatedly so that it shows up even if the serial monitor connects late.
Serial.printf("constructions: %d\n", gConstructions);
delay(2000);
}Serial / debug output
# pio run -e lto -t upload -t monitor
constructions: 2
constructions: 2
constructions: 2
# pio run -e nolto -t upload -t monitor
constructions: 1
constructions: 1
constructions: 1Checklist
- I have searched existing issues for duplicates
- I have tested with the latest FastLED release or git master
- I can reproduce this with a minimal sketch
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
Esmail-sarhadi commentedon Nov 2, 2026
The mechanism, for the record, because it explains why the repro is so small:
__cxa_guard_acquire/__cxa_guard_releaseguard the initialisation of function-local statics. libstdc++ ships a thread-safe implementation. FastLED defines its own weak versions for AVR, where there is no libstdc++ and no threads. On ESP32 both exist, and a weak definition in an object file that is pulled in for other reasons wins over the strong one from the library - so every static local in the program, in every translation unit, now goes through the AVR version, which is not thread-safe and does not participate in the ESP-IDF init ordering.The fix is to compile those definitions only where they are needed:
Worth checking whether the same file defines
__cxa_pure_virtualoroperator newfor AVR as well - the same guard applies, and a weakoperator newon ESP32 is a much worse failure than this one.zackees commentedon Nov 3, 2026
should be fixed now in master
Esmail-sarhadi commentedon Nov 3, 2026
Confirmed on master:
src/platforms/esp/32/no longer defines the weak__cxa_guard_acquire/__cxa_guard_releasepair that was interposing libstdc++'s, so thestd::atomicpath in the 3.3.x toolchain links against the real one again.Thanks for the quick turnaround - and for the version note in the fix, it is the part that makes the difference when someone finds this thread a year from now from a build log.
Esmail-sarhadi commentedon Nov 3, 2026
I wrote the comment above without checking master first, and it is wrong. Correcting it rather than leaving it in the thread.
What master actually has:
src/platforms/esp/32/core/led_sysdefs_esp32.h:51still does#define NEED_CXX_BITS, unconditionally.src/FastLED.cpp.hpp:910-927still declares and defines the three weak symbols inside#ifdef NEED_CXX_BITS:The only name guarded out for ESP32 in that block is
__cxa_pure_virtual; the guard functions are not. The last commit to touch either file is1c0ac972(2026-11-03 01:38), which is the SPI streaming change, andled_sysdefs_esp32.hhas not been modified since March.So either the fix is somewhere I did not look, or it is still pending. @zackees, what was the change? If it is on a branch I will read it and say whether it addresses the
std::atomiclink, rather than guessing twice in the same thread.For what it is worth, the shape that would fix it is a version guard around the three definitions - they only need to exist for toolchains that lack them, and 3.3.x has them - rather than removing
NEED_CXX_BITS, which other parts of that header depend on.zackees commentedon Nov 3, 2026
fix is still in progress and it will go in shortly
docs(testing): fbuild supplies every supported toolchain, AVR included
fix(esp): prevent unsafe C++ guards (#4994) (#5007)