arduino-esp32 icon indicating copy to clipboard operation
arduino-esp32 copied to clipboard

Crash on LittleFS read or write

Open hitecSmartHome opened this issue 1 year ago • 4 comments

Board

ESP32-Wrover

Device Description

Hardware Configuration

Version

v2.0.14

IDE Name

PlatformIO

Operating System

Windows10

Flash frequency

80

PSRAM enabled

yes

Upload speed

115200

Description

My system crashes when it tries to read or write to/from flash.

Sketch

boolean FileSystem::write(const char* path, JsonDocument& doc){
    if( !createPath(path) ){ return false; }
    File file = userFS.open(path, FILE_WRITE);
    if(!file){
        #if DB_DEBUG
            logger.write("[FileSystem] - (writeJson) File not found: %s\n",path);
        #endif
        return false;
    }
    size_t result = serializeJson(doc, file);
    file.flush();
    file.close();
    return result != 0;
}

int FileSystem::read(const char* path, JsonDocument& doc){
    File file = userFS.open(path);
    if(!file){
        #if DB_DEBUG
            logger.write("[FileSystem] - (getJson) File not found: %s\n",path);
        #endif
        return -1;
    }
    DeserializationError error = deserializeJson(doc, file);
    file.close();
    if( error == DeserializationError::Ok ){
        #if DB_DEBUG
            logger.write("[FileSystem] - (getJson) Message read from file: %s\n",path);
        #endif
        return doc.size();
    }
    return -1;
}

Debug Message

==================== THREAD 1 (TCB: 0x3ffcca88, name: 'loopTask') =====================
#0  0x400839c0 in panic_abort (details=0x3ffcc3c0 "assert failed: 0x401c720b") at C:\Users\Pc\.platformio\packages\[email protected]\components\esp_system\panic.c:408
#1  0x4008a134 in esp_system_abort (details=0x3ffcc3c0 "assert failed: 0x401c720b") at C:\Users\Pc\.platformio\packages\[email protected]\components\esp_system\esp_system.c:137
#2  0x4008f1a8 in __assert_func (file=<optimized out>, line=0, func=0x0, expr=0x0) at C:\Users\Pc\.platformio\packages\[email protected]\components
ewlib\assert.c:47
#3  0x401c720e in lfs_file_read (lfs=0x3ffbe870, file=0x0, buffer=0x3f82688c, size=128) at managed_components\esp_littlefs\src\littlefs\lfs.c:6126
#4  0x401c313d in vfs_littlefs_read (ctx=0x3ffbe78c, fd=<optimized out>, dst=0x3f82688c, size=128) at managed_components\esp_littlefs\src\esp_littlefs.c:1545
#5  0x401f6189 in esp_vfs_read (r=<optimized out>, fd=0, dst=0x3f82688c, size=128) at C:\Users\Pc\.platformio\packages\[email protected]\components\vfs\vfs.c:457
#6  0x4000bdbe in ?? ()
#7  0x40001128 in ?? ()
#8  0x400594ec in ?? ()
#9  0x40269409 in _fread_r (ptr=0x3ffccaf4, buf=<optimized out>, size=1, count=1, fp=0x3ffd1a8c) at /builds/idf/crosstool-NG/.build/HOST-x86_64-w64-mingw32/xtensa-esp32-elf/src/newlib/newlib/libc/stdio/fread.c:228
#10 0x4026949c in fread (buf=0x3ffcc580, size=1, count=1, fp=0x3ffd1a8c) at /builds/idf/crosstool-NG/.build/HOST-x86_64-w64-mingw32/xtensa-esp32-elf/src/newlib/newlib/libc/stdio/fread.c:266
#11 0x401c083a in VFSFileImpl::read (this=0x3f8260b4, buf=0x3ffcc580 "\214\020\034\200\300\305\374?\034\306\374?$\306\374?", size=1) at C:/Users/Pc/.platformio/packages/framework-arduinoespressif32/libraries/FS/src/vfs_api.cpp:408
#12 0x401a797e in fs::File::read (this=<optimized out>, buf=0x3ffcc580 "\214\020\034\200\300\305\374?\034\306\374?$\306\374?", size=1) at C:/Users/Pc/.platformio/packages/framework-arduinoespressif32/libraries/FS/src/FS.cpp:82
#13 0x401a7990 in fs::File::readBytes (this=0x3ffcc690, buffer=0x3ffcc580 "\214\020\034\200\300\305\374?\034\306\374?$\306\374?", length=1) at C:/Users/Pc/.platformio/packages/framework-arduinoespressif32/libraries/FS/src/FS.h:63
#14 0x402722a5 in ArduinoJson::V700PB2::detail::Reader<fs::File, void>::read (this=0x3ffcc610) at lib/ArduinoJson-7.x/src/ArduinoJson/Deserialization/Readers/ArduinoStreamReader.hpp:17
#15 ArduinoJson::V700PB2::detail::Latch<ArduinoJson::V700PB2::detail::Reader<fs::File, void> >::load (this=0x3ffcc610) at lib/ArduinoJson-7.x/src/ArduinoJson/Json/Latch.hpp:38
#16 ArduinoJson::V700PB2::detail::Latch<ArduinoJson::V700PB2::detail::Reader<fs::File, void> >::current (this=0x3ffcc610) at lib/ArduinoJson-7.x/src/ArduinoJson/Json/Latch.hpp:30
#17 ArduinoJson::V700PB2::detail::JsonDeserializer<ArduinoJson::V700PB2::detail::Reader<fs::File, void> >::current (this=0x3ffcc600) at lib/ArduinoJson-7.x/src/ArduinoJson/Json/JsonDeserializer.hpp:47
#18 0x40137959 in ArduinoJson::V700PB2::detail::JsonDeserializer<ArduinoJson::V700PB2::detail::Reader<fs::File, void> >::skipSpacesAndComments (this=0x3ffcc600) at lib/ArduinoJson-7.x/src/ArduinoJson/Json/JsonDeserializer.hpp:581
#19 0x40137ca1 in ArduinoJson::V700PB2::detail::JsonDeserializer<ArduinoJson::V700PB2::detail::Reader<fs::File, void> >::skipSpacesAndComments (this=0x3ffcc600) at lib/ArduinoJson-7.x/src/ArduinoJson/Json/JsonDeserializer.hpp:579
#20 ArduinoJson::V700PB2::detail::JsonDeserializer<ArduinoJson::V700PB2::detail::Reader<fs::File, void> >::parseVariant<ArduinoJson::V700PB2::detail::AllowAllFilter> (this=0x3ffcc600, variant=..., filter=..., nestingLimit=...) at lib/ArduinoJson-7.x/src/ArduinoJson/Json/JsonDeserializer.hpp:67
#21 0x40137f49 in ArduinoJson::V700PB2::detail::JsonDeserializer<ArduinoJson::V700PB2::detail::Reader<fs::File, void> >::parse<ArduinoJson::V700PB2::detail::AllowAllFilter> (nestingLimit=..., variant=..., this=0x3ffcc600, filter=...) at lib/ArduinoJson-7.x/src/ArduinoJson/Json/JsonDeserializer.hpp:31
#22 ArduinoJson::V700PB2::detail::doDeserialize<ArduinoJson::V700PB2::detail::JsonDeserializer, ArduinoJson::V700PB2::JsonDocument&, ArduinoJson::V700PB2::detail::Reader<fs::File, void>, ArduinoJson::V700PB2::detail::DeserializationOptions<ArduinoJson::V700PB2::detail::AllowAllFilter> > (dst=..., reader=..., options=...) at lib/ArduinoJson-7.x/src/ArduinoJson/Deserialization/deserialize.hpp:57
#23 0x401380f9 in ArduinoJson::V700PB2::detail::deserialize<ArduinoJson::V700PB2::detail::JsonDeserializer, ArduinoJson::V700PB2::JsonDocument&, fs::File&, , void>(ArduinoJson::V700PB2::JsonDocument&, fs::File&) (input=..., dst=...) at lib/ArduinoJson-7.x/src/ArduinoJson/Deserialization/deserialize.hpp:69
#24 ArduinoJson::V700PB2::deserializeJson<ArduinoJson::V700PB2::JsonDocument&, fs::File&> (dst=...) at lib/ArduinoJson-7.x/src/ArduinoJson/Json/JsonDeserializer.hpp:679
#25 FileSystem::read (this=0x3ffb43c0 <db>, path=0x3f40228c "/graphs.json", doc=...) at src/FileSystem/FileSystemReads.cpp:138
#26 0x400e697e in Graphs::get (this=<optimized out>, type=0x3f406918 "shader", id=..., graphData=...) at src/Components/Graph/Graph.cpp:8
#27 0x4010c4d2 in Shader::handleGraph (this=0x3f8515ec, detail=...) at src/Components/Shader/GraphHandler.cpp:22
#28 0x4010c670 in Shader::checkGraph (this=0x3f8515ec) at src/Components/Shader/GraphHandler.cpp:13
#29 0x40113a5d in <lambda(int, int)>::operator() (__closure=0x3ffcc950, prev=<optimized out>, now=<optimized out>) at src/Components/Shader/Shader.cpp:14
#30 std::_Function_handler<void(int, int), Shader::Shader(ArduinoJson::V700PB2::JsonObject&)::<lambda(int, int)> >::_M_invoke(const std::_Any_data &, int &&, int &&) (__functor=..., __args#0=@0x3ffcc920: 59, __args#1=@0x3ffcc924: 0) at c:\users\pc\.platformio\packages       [email protected]+2021r2-patch5\xtensa-esp32-elf\include\c++\8.4.0\bits/std_function.h:297
#31 0x401977c0 in std::function<void (int, int)>::operator()(int, int) const (this=0x3ffcc950, __args#0=<optimized out>, __args#1=<optimized out>) at c:\users\pc\.platformio\packages     [email protected]+2021r2-patch5\xtensa-esp32-elf\include\c++\8.4.0\bits/std_function.h:687
#32 0x40197888 in Time::emitMinChange (now=0, prev=59, this=<optimized out>) at src/Time/TimeEvents.cpp:45
#33 Time::emitMinChange (this=<optimized out>, prev=59, now=0) at src/Time/TimeEvents.cpp:39
#34 0x40197a0a in Time::monitorMinChange (this=0x3ffb3d7c <hshTime>) at src/Time/TimeEvents.cpp:154
#35 0x40197a33 in Time::handleEvents (this=0x3ffb3d7c <hshTime>) at src/Time/TimeEvents.cpp:205
#36 0x4019676d in Time::loop (this=0x3ffb3d7c <hshTime>) at src/Time/Time.cpp:211
#37 0x401a1754 in loop () at src/main.cpp:91
#38 0x401bf404 in loopTask (pvParameters=<optimized out>) at C:/Users/Pc/.platformio/packages/framework-arduinoespressif32/cores/esp32/main.cpp:50

Other Steps to Reproduce

On an other task menawhile the system tried to retrive littlefs information

==================== THREAD 2 (TCB: 0x3ffbee58, name: 'systemTask') =====================
#0  0x4000bff0 in ?? ()
#1  0x4008a899 in vPortClearInterruptMaskFromISR (prev_level=<optimized out>) at C:\Users\Pc\.platformio\packages\[email protected]\components\freertos\port\xtensa\include/freertos/portmacro.h:571
#2  vPortExitCritical (mux=<optimized out>) at C:\Users\Pc\.platformio\packages\[email protected]\components\freertos\port\xtensa\port.c:332
#3  0x4027c80a in xTaskResumeAll () at C:\Users\Pc\.platformio\packages\[email protected]\components\freertos     asks.c:2629
#4  0x40084f35 in spi_flash_enable_interrupts_caches_and_other_cpu () at C:\Users\Pc\.platformio\packages\[email protected]\components\spi_flash\cache_utils.c:217
#5  0x40085db0 in cache_enable (arg=0x0) at C:\Users\Pc\.platformio\packages\[email protected]\components\spi_flash\spi_flash_os_func_app.c:68
#6  0x40085df6 in spi1_end (arg=0x3ffb28d8 <main_flash_arg>) at C:\Users\Pc\.platformio\packages\[email protected]\components\spi_flash\spi_flash_os_func_app.c:131
#7  0x400888e8 in spiflash_end_default (chip=0x3ffb286c <default_chip>, err=0) at C:\Users\Pc\.platformio\packages\[email protected]\components\spi_flash\esp_flash_api.c:154
#8  0x400859fc in esp_flash_read (chip=<optimized out>, buffer=0x3ffbe218, address=16576512, length=128) at C:\Users\Pc\.platformio\packages\[email protected]\components\spi_flash\esp_flash_api.c:859
#9  0x401f4464 in esp_partition_read (partition=<optimized out>, src_offset=6025216, dst=0x3ffbe218, size=128) at C:\Users\Pc\.platformio\packages\[email protected]\components\spi_flash\partition.c:424
#10 0x401c37e4 in littlefs_esp_part_read (c=<optimized out>, block=1471, off=0, buffer=0x3ffbe218, size=128) at managed_components\esp_littlefs\src\littlefs_esp_part.c:21
#11 0x401c3a10 in lfs_bd_read (lfs=0x3ffcce18, pcache=0x0, rcache=0x3ffcce18, hint=4, block=1471, off=0, buffer=<optimized out>, buffer@entry=0x3ffd0a78, size=size@entry=4) at managed_components\esp_littlefs\src\littlefs\lfs.c:116
#12 0x401c4a64 in lfs_dir_fetchmatch (lfs=0x3ffcce18, dir=0x3ffd0b20, pair=0x3ffd0b38, fmask=4294967295, ftag=4294967295, id=0x0, cb=cb@entry=0x0, data=data@entry=0x0) at managed_components\esp_littlefs\src\littlefs\lfs.c:1114
#13 0x401c4f3c in lfs_dir_fetch (lfs=0x3ffcce18, dir=0x3ffd0b20, pair=0x3ffd0b38) at managed_components\esp_littlefs\src\littlefs\lfs.c:1378
#14 0x401c5186 in lfs_fs_traverse_ (lfs=0x3ffcce18, cb=0x40274bf8 <lfs_fs_size_count>, data=0x3ffd0b80, includeorphans=<optimized out>) at managed_components\esp_littlefs\src\littlefs\lfs.c:4656
#15 0x401c5c68 in lfs_fs_size_ (lfs=0x3ffcce18) at managed_components\esp_littlefs\src\littlefs\lfs.c:5115
#16 0x401c7680 in lfs_fs_size (lfs=0x3ffcce18) at managed_components\esp_littlefs\src\littlefs\lfs.c:6357
#17 0x401c2638 in get_total_and_used_bytes (efs=0x3ffccd34, total_bytes=<optimized out>, used_bytes=0x3ffb3b14 <hshSystem+4>) at managed_components\esp_littlefs\src\esp_littlefs.c:295
#18 0x401c3390 in esp_littlefs_info (partition_label=<optimized out>, total_bytes=0x3ffb3b10 <hshSystem>, used_bytes=0x3ffb3b14 <hshSystem+4>) at managed_components\esp_littlefs\src\esp_littlefs.c:337
#19 0x4018ca74 in Sys::getMemoryInfo (this=0x3ffb3b10 <hshSystem>, memoryInfo=...) at src/System/SystemInfo.cpp:99
#20 0x4018d3a7 in Sys::getSysInfo (this=0x3ffb3b10 <hshSystem>, info=...) at src/System/SystemInfo.cpp:30
#21 0x4018d3fc in Sys::sendSysInfo (this=<optimized out>) at src/System/SystemInfo.cpp:15
#22 0x4018af14 in Sys::loop (this=0x3ffb3b10 <hshSystem>) at src/System/System.cpp:553
#23 0x4018af2c in systemTask (pvParameters=<optimized out>) at src/System/System.cpp:559
bool success = esp_littlefs_info("system", &sysTotal, &sysUsed) == ESP_OK &&
esp_littlefs_info("user", &userTotal, &userUsed) == ESP_OK

I have checked existing issues, online documentation and the Troubleshooting Guide

  • [X] I confirm I have checked existing issues, online documentation and Troubleshooting guide.

hitecSmartHome avatar May 27 '24 08:05 hitecSmartHome

Please provide minimal code to reproduce. This looks like you are trying to access the flash in a routine that does not have access (eg, an ISR)

lbernstone avatar May 27 '24 09:05 lbernstone

There is no ISR. It just tries to save a json document into flash in a loop of a task, meanwhile an other task tries to retrive the flash information with esp_littlefs_info. esp_littlefs_info seems to load the cpu in itself far too much anyway.

hitecSmartHome avatar May 27 '24 09:05 hitecSmartHome

Perhaps you could put a mutex on the disk access if they are blocking each other.

lbernstone avatar May 27 '24 09:05 lbernstone

Yeah, I tought that LittleFS is thread safe and there are no concurrent memory access problems at all.

I can see that there is some semaphore handling in this code

void get_total_and_used_bytes(esp_littlefs_t *efs, size_t *total_bytes, size_t *used_bytes) {
    sem_take(efs);
    size_t total_bytes_local = efs->cfg.block_size * efs->fs->block_count;
    if(total_bytes) *total_bytes = total_bytes_local;

    /* lfs_fs_size may return a size larger than the actual filesystem size.
     * https://github.com/littlefs-project/littlefs/blob/9c7e232086f865cff0bb96fe753deb66431d91fd/lfs.h#L658
     */
    if(used_bytes) *used_bytes = MIN(total_bytes_local, efs->cfg.block_size * lfs_fs_size(efs->fs));
    sem_give(efs);
}

This is in esp_littlefs.c

hitecSmartHome avatar May 27 '24 09:05 hitecSmartHome