Page 1 of 2

LoadProhibited in fseek()

Posted: Fri Sep 30, 2022 7:16 pm
by bassie127
I am writing a file in SPIFFS.
When the file size is about 60% or more of the available space as reported by esp_spiffs_info, then after some time the program crashes with LoadProhibited in fseek.
  1. Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.
  2.  
  3. Core  0 register dump:
  4. PC      : 0x401420fc  PS      : 0x00060f30  A0      : 0x801420b9  A1      : 0x3ffbac40  
  5. A2      : 0x3ffaf6dc  A3      : 0x00000000  A4      : 0x0007e867  A5      : 0x00000000  
  6. A6      : 0x3ff96458  A7      : 0x0000000c  A8      : 0x801420fc  A9      : 0x3ffbac20  
  7. A10     : 0x00000000  A11     : 0x3ffbac7c  A12     : 0x00000002  A13     : 0x000001b6  
  8. A14     : 0x3ffaf6dc  A15     : 0x3ffca644  SAR     : 0x00000020  EXCCAUSE: 0x0000001c  
  9. EXCVADDR: 0x00000064  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xfffffffd  
  10.  
  11.  
  12. Backtrace:0x401420f9:0x3ffbac400x401420b6:0x3ffbacb0 0x400d8cb8:0x3ffbacd0 0x400d82f5:0x3ffbad00 0x400d8489:0x3ffbb430 0x401549eb:0x3ffbb460 0x4008b6b5:0x3ffbb480
  13.  
  14.  
  15.  
  16.  
  17. ELF file SHA256: 26dbf0313a57db38
  18.  
  19. CPU halted.
  20.  
  21. ```
  22.  
  23.  
  24.  
  25. ```
  26. $ idf.py flash monitor > run.txt
  27. --- idf_monitor on /dev/ttyUSB0 115200 ---
  28. --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
  29.  
  30. 0x40080400: _init at ??:?
  31.  
  32. 0x40080400: _init at ??:?
  33.  
  34. 0x401420fc: _fseeko_r at /builds/idf/crosstool-NG/.build/xtensa-esp32-elf/src/newlib/newlib/libc/stdio/fseeko.c:116 (discriminator 14)
  35.  
  36. 0x401420f9: _fseeko_r at /builds/idf/crosstool-NG/.build/xtensa-esp32-elf/src/newlib/newlib/libc/stdio/fseeko.c:116 (discriminator 14)
  37.  
  38. 0x401420b6: _fseek_r at /builds/idf/crosstool-NG/.build/xtensa-esp32-elf/src/newlib/newlib/libc/stdio/fseek.c:89
  39.  (inlined by) fseek at /builds/idf/crosstool-NG/.build/xtensa-esp32-elf/src/newlib/newlib/libc/stdio/fseek.c:99
  40.  
  41. 0x400d8cb8: SPIFFS_FILE::spiff_file::append_line_to_file(char*) at /home/baswi/Programs/esp32_softap/components/spiffs_file/spiffs_file.cpp:173 (discriminator 5)
  42.  
  43. 0x400d82f5: test_json_spiff() at /home/baswi/Programs/esp32_softap/main/main.cpp:1174
  44.  
  45. 0x400d8489: app_main at /home/baswi/Programs/esp32_softap/main/main.cpp:1146
  46.  
  47. 0x401549eb: main_task at /home/baswi/esp/esp-idf/components/freertos/port/port_common.c:141 (discriminator 2)
  48.  
  49. 0x4008b6b5: vPortTaskWrapper at /home/baswi/esp/esp-idf/components/freertos/port/xtensa/port.c:131

Re: LoadProhibited in fseek()

Posted: Sat Oct 01, 2022 5:40 am
by ESP_igrr
Hi bassie127,

It looks like somehow a NULL file pointer value gets passed to fseek instead of the real file pointer.
Please check if the file pointer is valid, for example if you are calling fopen, check if it didn't return NULL. Also please check if you have enough heap memory at this point, as some allocation internal to fseek could be failing.

fopen() of file in SPIFFS suddenly returns NULL when file grows over 60% of available space

Posted: Sun Oct 02, 2022 3:13 pm
by bassie127
You are correct.
I added a test, and it seems that after some time (after writing several strings to the file), suddenly fopen() of the existing file returns a NULL pointer.
So I know why fseek() fails, but now is the question why fopen() fails after some time.

I also made a simple test program, deduced from spiffs_example. When the file grows to about 60% of the free space in SPIFFS as reported by esp_spiffs_info, the fopen() suddenly returns a NULL pointer. Any ideas?

Re: LoadProhibited in fseek()

Posted: Sun Oct 02, 2022 3:24 pm
by bassie127
For everyone that wishes to reproduce the problem, I have added my test project to github, see https://github.com/basaandewiel/spiffsT ... ple_main.c
(look in main file for "this should not happen"

Re: LoadProhibited in fseek()

Posted: Mon Oct 03, 2022 2:44 am
by ESP_Sprite
No clue what the issue is, but you might gleam a bit more info from also printing the value of `errno`. fopen sets that if it encounters an error.

Re: LoadProhibited in fseek()

Posted: Tue Oct 04, 2022 7:23 pm
by bassie127
Maybe stupid question, but I don't know how to get the errno; the fopen() only returns a pointer, and that is NULL.
Via the gdbstub I could get following info, does that help?
  1.  
  2. (gdb) bt
  3. #0  esp_crosscore_int_send_yield (core_id=0) at /home/baswi/esp/esp-idf/components/esp_system/crosscore_int.c:145
  4. #1  0x400891d0 in vTaskDelay (xTicksToDelay=100) at /home/baswi/esp/esp-idf/components/freertos/tasks.c:1595
  5. #2  0x400d6e61 in app_main () at /home/baswi/Programs/spiffsTestBigFileFails/main/spiffs_example_main.c:122
  6. #3  0x400f4661 in main_task (args=<unavailable>) at /home/baswi/esp/esp-idf/components/freertos/port/port_common.c:141
  7. (gdb) frame 2
  8. #2  0x400d6e61 in app_main () at /home/baswi/Programs/spiffsTestBigFileFails/main/spiffs_example_main.c:122
  9. 122                     vTaskDelay(1000 / portTICK_PERIOD_MS);
  10. (gdb) info locals
  11. gdb = 1
  12. conf = {base_path = 0x3f4034d8 "/spiffs", partition_label = 0x0, max_files = 5, format_if_mount_failed = true}
  13. ret = 0
  14. total = 52961
  15. used = 45933
  16. available_space = 7028
  17. f = 0x0
  18. st = {st_dev = 0, st_ino = 0, st_mode = 33279, st_nlink = 0, st_uid = 0, st_gid = 0, st_rdev = 0, st_size = 45240, st_atim = {tv_sec = 0, tv_nsec = 0},
  19.   st_mtim = {tv_sec = 0, tv_nsec = 0}, st_ctim = {tv_sec = 0, tv_nsec = 0}, st_blksize = 0, st_blocks = 0, st_spare4 = {0, 0}}
  20. (gdb)

Re: LoadProhibited in fseek()

Posted: Tue Oct 04, 2022 10:46 pm
by ESP_igrr
errno is a thread-local variable (access to which happens via a scary-looking macro), you can simply use it as an integer:

printf("errno: %d (%s)\n", errno, strerror(errno));

Re: LoadProhibited in fseek()

Posted: Wed Oct 05, 2022 1:23 pm
by bassie127
After a couple of succesfull writes to the file I got errno 5:
  1. I (347) example: Initializing SPIFFS
  2. I (347) example: Partition size: total: 52961, used: 45933
  3. I (357) example: Checking for  existing file or creating new file
  4. I (357) example: Using existing file: /spiffs/test.txt
  5. I (407) example: Partition size: total: 52961, used: 45933
  6. I (447) example: Partition size: total: 52961, used: 45933
  7. I (487) example: Partition size: total: 52961, used: 45933
  8. I (527) example: Partition size: total: 52961, used: 45933
  9. I (567) example: Partition size: total: 52961, used: 45933
  10. E (567) example: THIS SHOULD NOT HAPPEN - Failed to open file for updating, 7028
  11. errno: 5 (I/O error)

Re: LoadProhibited in fseek()

Posted: Wed Oct 05, 2022 1:29 pm
by bassie127
As you can see also the write (append) operations before the error occurs are failing.
As you can see my github (see link above) following code is executed:
  1. fseek(f, 0, SEEK_END);
  2.  
  3.             fprintf(f, "this is a log test string to fill up the file, for testing whether everything works OK\n");
  4.             fclose(f);
  5.  
  6.             ret = esp_spiffs_info(conf.partition_label, &total, &used);
  7.             if (ret != ESP_OK)
  8.             {
  9.                 ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s). Formatting...", esp_err_to_name(ret));
  10.                 esp_spiffs_format(conf.partition_label);
  11.                 return;
  12.             }
But the used number of bytes do not increase (anymore)

Re: LoadProhibited in fseek()

Posted: Wed Oct 05, 2022 2:41 pm
by Craige Hales
https://github.com/nodemcu/nodemcu-firmware/issues/2718
good info at the beginning, not so much at the end.

I found I could reload a static spiffs image by (1) delete everything, then (2) re-add the biggest files first, smallest last. My spiffs dir is 80% used. Before that, I tried

Code: Select all

for each file{delete the file, replace the file}
and it failed.