file.flush() causes a crash

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
br0kenpixel
Posts: 5
Joined: Sun Dec 06, 2020 3:15 pm

file.flush() causes a crash

Post by br0kenpixel » Fri Nov 26, 2021 9:36 am

Hi.
I'm working on a weather station project and it uses the ESP32's RTC memory. I added a function (rtcmem.saveSession()) that dumps the content of the RTC memory (as a string) and writes it to a file when the node is about to go to deep sleep. Then, upon startup, if a crash was detected, I can use this file to restore the memory contents.
Recently, I decided to take some parts of my code (including the one that manages the RTC memory) and compile it with mpy-cross. This change however, introduced a "bug". I noticed that sometimes the node crashes when it tries to run rtcmem.saveSession().
This is the code for the saveSession function:

Code: Select all

def saveSession(self, ignoreEmpty=False):
    if self.readMemory(True) == dict():
        if not ignoreEmpty:
            raise Exception("Cannot save empty memory, use ignoreEmpty arg to ignore this")
        else:
            return
    try:
        contentDump = open("/rtcmem.dump", "w")
        content = self.readMemory(True)
        #content will be a Python dictionary
        content = str(content)
        contentDump.write(content)
        contentDump.flush()
        contentDump.clse()
    except Exception as ex:
        contentDump.close()
        self.deleteSessionFiles()
        raise ex
    rtcmm_log("debug", "RTC session files saved")
Since, I can't really debug the code, I just added a print(".") statement after each line inside the try block. It turns out, that contentDump.flush() is what caused the crash.

Code: Select all

Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC      : 0x4018c603  PS      : 0x00060430  A0      : 0x800eef90  A1      : 0x3ffb75c0
A2      : 0x00810008  A3      : 0x3ffb7620  A4      : 0x0000000c  A5      : 0x3f817984
A6      : 0x00000860  A7      : 0x00000000  A8      : 0x00000001  A9      : 0x00000001
A10     : 0x00000001  A11     : 0xff7f0000  A12     : 0x00810000  A13     : 0x00000001
A14     : 0x00000003  A15     : 0x00060023  SAR     : 0x00000010  EXCCAUSE: 0x0000001c
EXCVADDR: 0x00810008  LBEG    : 0x4008b919  LEND    : 0x4008b93b  LCOUNT  : 0xffffffff

ELF file SHA256: 57ae9103722b1711

Backtrace: 0x4018c600:0x3ffb75c0 0x400eef8d:0x3ffb75e0 0x400ef8e2:0x3ffb7690 0x400f0069:0x3ffb76e0 0x400ec051:0x3ffb7700 0x400dd0a1:0x3ffb7720 0x400e353d:0x3ffb7740 0x400e3672:0x3ffb7760 0x400ea9fd:0x3ffb7780 0x400eacbe:0x3ffb77b0 0x400dd0f2:0x3ffb77e0 0x400e353d:0x3ffb7810 0x400e5dce:0x3ffb7830 0x400dd18c:0x3ffb78d0 0x400e353d:0x3ffb7900 0x400e3672:0x3ffb7920 0x400e5e7d:0x3ffb7940 0x400dd18c:0x3ffb79e0 0x400e353d:0x3ffb7a50 0x400e5dce:0x3ffb7a70 0x400dd18c:0x3ffb7b10 0x400e353d:0x3ffb7b40 0x400e3566:0x3ffb7b60 0x400e35ea:0x3ffb7b80 0x401040ad:0x3ffb7c10 0x401040f1:0x3ffb7c40 0x400dd101:0x3ffb7c60 0x400e353d:0x3ffb7c90 0x400e5dce:0x3ffb7cb0 0x400dd18c:0x3ffb7d50 0x400e353d:0x3ffb7d80 0x400e3566:0x3ffb7da0 0x400ecb97:0x3ffb7dc0 0x400ecf59:0x3ffb7e50 0x400ecfbd:0x3ffb7e70 0x400d71f3:0x3ffb7e90
Removing the flush() call doesn't help, because then close() causes a crash. It probably tries flushing too.
Could someone please explain what is going on? Is there a bug in how MicroPython handles file I/O or the issue is something else?

Post Reply