mobizt / Firebase-ESP-Client

[DEPRECATED]🔥Firebase Arduino Client Library for ESP8266, ESP32 and RP2040 Pico. The complete, fast, secured and reliable Firebase Arduino client library that supports RTDB, Cloud Firestore, Firebase and Google Cloud Storage, Cloud Messaging and Cloud Functions for Firebase.
MIT License
471 stars 100 forks source link

CORRUPT HEAP: Bad head at 0x3ffe35b4. Expected 0xabba1234 got 0x3ffe0454 #518

Closed mrezzaaa closed 1 year ago

mrezzaaa commented 1 year ago

Describe the bug Running streaming to RTDB. Crash after 20mins above. I think when regenerating token.

To Reproduce Separating task between main loop and firebase handling (Run on core #0). No time blocking execution, that's why i used different task and different core. I use core 0 esp32 for network handling. On stream connection established, free heap about 25-30% available

 void loop(){
            if(WiFi.status() == WL_CONNECTED){
                if(Firebase.ready()){
                    state.firebaseconnected = true;
                    if(firebase.connection.isStream() || !Firebase.isTokenExpired()){
                        digitalWrite(PIN::LED,HIGH);
                        state.previousFirebaseConnected = millis();
                        state.firebaseconnected = true;
                    }
                    else{
                        state.firebaseconnected = false;
                    }
                }   
                else{
                    Serial.println("Firebase not ready");
                    state.devicestate = "Server Unready";
                    digitalWrite(PIN::LED,LOW);
                    Firebase.refreshToken(&firebase.config);
                    if(state.collector == false){
                        if (!Firebase.RTDB.beginMultiPathStream(&firebase.connection, "/")){
                            Serial.printf("stream begin error, %s\n\n", firebase.connection.errorReason().c_str());
                        }
                        else{
                            Serial.println("Restream success");
                        }
                    }
                }
                if(millis() - state.previousFirebaseConnected > firebaseTimeout){
                    Serial.println("Timeout Firebase.Restarting...");
                    ESP.restart();
                }
            }
            else{
                Serial.println("Wifi disconnected");
                state.devicestate = "WIFI DISCONNECTED";
            }
        }

Error debug

CORRUPT HEAP: Bad head at 0x3ffe35b4. Expected 0xabba1234 got 0x3ffe0454

assert failed: multi_heap_free multi_heap_poisoning.c:253 (head != NULL)

Backtrace:0x400839f1:0x3fff49000x40093fb9:0x3fff4920 0x400999d1:0x3fff4940 0x40099617:0x3fff4a70 0x40083eb1:0x3fff4a90 0x40099a01:0x3fff4ab0 0x401901b9:0x3fff4ad0 0x400fddd1:0x3fff4af0 0x401b1b0a:0x3fff4b10 0x400faeaa:0x3fff4b30 0x400fd95d:0x3fff4b50 0x400f8e04:0x3fff4b70 0x400f79ef:0x3fff4b90 0x400f7c15:0x3fff4c40 0x400f7c7d:0x3fff4c60 0x400f7cb1:0x3fff4c80 

  #0  0x400839f1:0x3fff4900 in panic_abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp_system/panic.c:402
  #1  0x40093fb9:0x3fff4920 in esp_system_abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp_system/esp_system.c:128
  #2  0x400999d1:0x3fff4940 in __assert_func at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/newlib/assert.c:85
  #3  0x40099617:0x3fff4a70 in multi_heap_free at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/heap/multi_heap_poisoning.c:253
      (inlined by) multi_heap_free at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/heap/multi_heap_poisoning.c:245
  #4  0x40083eb1:0x3fff4a90 in heap_caps_free at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/heap/heap_caps.c:340
  #5  0x40099a01:0x3fff4ab0 in free at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/newlib/heap.c:39
  #6  0x401901b9:0x3fff4ad0 in operator delete(void*) at /builds/idf/crosstool-NG/.build/HOST-x86_64-apple-darwin12/xtensa-esp32-elf/src/gcc/libstdc++-v3/libsupc++/del_op.cc:49
  #7  0x400fddd1:0x3fff4af0 in FB_TCP_Client::~FB_TCP_Client() at .pio/libdeps/esp32doit-devkit-v1/Firebase Arduino Client Library for ESP8266 and ESP32/src/wcs/esp32/FB_TCP_Client.cpp:52
  #8  0x401b1b0a:0x3fff4b10 in Firebase_Signer::freeClient(FB_TCP_Client**) at .pio/libdeps/esp32doit-devkit-v1/Firebase Arduino Client Library for ESP8266 and ESP32/src/signer/Signer.cpp:852
      (inlined by) Firebase_Signer::freeClient(FB_TCP_Client**) at .pio/libdeps/esp32doit-devkit-v1/Firebase Arduino Client Library for ESP8266 and ESP32/src/signer/Signer.cpp:846
  #9  0x400faeaa:0x3fff4b30 in Firebase_Signer::reconnect() at .pio/libdeps/esp32doit-devkit-v1/Firebase Arduino Client Library for ESP8266 and ESP32/src/signer/Signer.cpp:1814
  #10 0x400fd95d:0x3fff4b50 in Firebase_Signer::tokenReady() at .pio/libdeps/esp32doit-devkit-v1/Firebase Arduino Client Library for ESP8266 and ESP32/src/signer/Signer.cpp:2163
  #11 0x400f8e04:0x3fff4b70 in FirebaseData::tokenReady() at .pio/libdeps/esp32doit-devkit-v1/Firebase Arduino Client Library for ESP8266 and ESP32/src/session/FB_Session.cpp:870
  #12 0x400f79ef:0x3fff4b90 in FB_RTDB::handleStreamRead(FirebaseData*) at .pio/libdeps/esp32doit-devkit-v1/Firebase Arduino Client Library for ESP8266 and ESP32/src/rtdb/FB_RTDB.cpp:651
  #13 0x400f7c15:0x3fff4c40 in FB_RTDB::readStream(FirebaseData*) at .pio/libdeps/esp32doit-devkit-v1/Firebase Arduino Client Library for ESP8266 and ESP32/src/rtdb/FB_RTDB.cpp:614
  #14 0x400f7c7d:0x3fff4c60 in FB_RTDB::mRunStream() at .pio/libdeps/esp32doit-devkit-v1/Firebase Arduino Client Library for ESP8266 and ESP32/src/rtdb/FB_RTDB.cpp:919
      (inlined by) FB_RTDB::mRunStream() at .pio/libdeps/esp32doit-devkit-v1/Firebase Arduino Client Library for ESP8266 and ESP32/src/rtdb/FB_RTDB.cpp:893
  #15 0x400f7cb1:0x3fff4c80 in FB_RTDB::runStreamTask(FirebaseData*, char const*)::{lambda(void*)#1}::_FUN(void*) at .pio/libdeps/esp32doit-devkit-v1/Firebase Arduino Client Library for ESP8266 and ESP32/src/rtdb/FB_RTDB.cpp:850
      (inlined by) _FUN at .pio/libdeps/esp32doit-devkit-v1/Firebase Arduino Client Library for ESP8266 and ESP32/src/rtdb/FB_RTDB.cpp:857

Expected behavior When token is expired, regenerate token (refresh token) and establish connection again.

IDE and its version:

mobizt commented 1 year ago

You should not run time and mem consuming tasks on core 0 especially networking task.

Please run your task on core 1 and debug it.

mobizt commented 1 year ago

Your device free Heap is very critical and if you don't have much free memory available, I recommend calling this in loop. https://github.com/mobizt/Firebase-ESP-Client/blob/d2fce37b28a29dfa37fa97e1ef37f87983d90a13/examples/RTDB/DataChangesListener/Callback/Callback.ino#L194

With this, no memory was reserved, and stream will run in loop instead of FreeRTOS task and stream callback will get call as normally.

mrezzaaa commented 1 year ago

Ok, ill try. I will update later

mrezzaaa commented 1 year ago

It works! Thank you sir