← index #4578Issue #4110
Related · high · value 2.109
QUERY · ISSUE

Writing to a file in a callback corrupts the SD card when the file_id_object has closed

openby rhubarbdogopened 2019-03-06updated 2019-03-07
port-stm32

When running the following program which writes to SD card in a timer's callback. The SD card may become corrupt if the file has been closed before the callback has been set to None.
I discoverd this in a real example, but have written this version which you press the USR switch to close the file.

corrupt.py.txt

I'm running a pyboard version 1.1 and have edited my boot.py so the pyboard is a serial device and mouse.
Using a newly formatted SD Card, I insert the card into the pyboard and plug the pyboard into my laptop. I let the program run to completion.
I then start a REPL session and do a soft reboot. I press the USR switch to close the file during execution. The first time i do this everything looks ok. So I repeat the soft reboot and press the switch again, this time all my data has been lost there's only main.py left on the SD card.

PYB: sync filesystems
PYB: soft reboot
uncaught exception in Timer(4) interrupt handler
Traceback (most recent call last):
  File "main.py", line 28, in timer_cb
OSError: [Errno 22] EINVAL
MicroPython v1.9.4-543-gdc77fdb7d on 2018-09-19; PYBv1.1 with STM32F405RG
Type "help()" for more information.
>>> os.listdir()
['main.py', 'data-set.bin', 'data.001', 'new-file.bin']
>>> os.rename('new-file.bin', 'new.001')
>>> 
PYB: sync filesystems
PYB: soft reboot
uncaught exception in Timer(4) interrupt handler
Traceback (most recent call last):
  File "main.py", line 28, in timer_cb
OSError: [Errno 22] EINVAL
MicroPython v1.9.4-543-gdc77fdb7d on 2018-09-19; PYBv1.1 with STM32F405RG
Type "help()" for more information.
>>> os.listdir()
['main.py']

when i plug the SD card into my laptop i can see files

data.001
main.py
new.001
new-file.bin

but no data-set.bin

CANDIDATE · ISSUE

stm32: SD card append file hangs on close after sleepy write loop

closedby rhubarbdogopened 2018-09-09updated 2018-09-12
bugport-stm32

I have written a data logger. It writes data to the SD card, I mount the sd card manually as i have a SKIPSD file in my flash storage. It loops writing data once every few minutes, press the switch to terminate gracefully unmounting the sd card. I let the program run for a few loops then press the switch. The lights change indicating the loop is terminating. it then just hangs for a bit, terminates my repl session on my laptop, continues to hang eventually the lights switch off, but my file probably will be corrupted.

First time i run the program when there is no data file it works correctly and writes the data to disk. It's second time i run the program when the data file already exists the program hangs on the closing of the file. I have swapped the with open("data_file.txt", "a") as f: to f=open("data_file.txt", "a") in this second program it hangs at f.close()

here's a listing
append.py.txt

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied