← index #4578Issue #4575
Related · medium · value 2.566
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

rapid writes corrupts SDCard

closedby rhubarbdogopened 2019-03-05updated 2019-03-07
port-stm32

I've started with a newly formatted SD Card, running on a pyboard 1.1 with a SKIPSD file on the flash.
i've written a data logger which logs data in a callback on timer 4 running at 8kHz and writes it to SD Card.
When I plug the pyboard in the program runs to completion I think, I can't start a serial terminal fast enough to catch any errors.
but

>>> os.listdir()
['data-set.bin']
>>> os.rename('data-set.bin', 'data-set.rst')

I now soft reboot it to run again

PYB: sync filesystems
PYB: soft reboot
uncaught exception in Timer(4) interrupt handler
Traceback (most recent call last):
  File "main.py", line 36, in buffer_data
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()
['data-set.bin', 'data-set.rst']
>>> data_set.terminate()
>>> os.umount()

ironically it looks like its worked, so I unplug the SD card and plug it into my laptop the disk looks blank.
so i put the SD Card back into my pyboard and press reset the program runs

I now start REPL and type os.listdir()
i now get

['data-set.bin', 'data-set.rst', '\x08\x05\x00\x00\x1fT\x0f', '+\x05\x00\x1cv\x0f', ')\x05\x00\x00VX\x0f', '0\x03\x00\x00\x1bo\x0f', 'g\x03\x00\x15Q\x0f']

I unmount the SD card and remove it, it still looks blank in my laptop

here's a copy of the program i'm running

data_logger2.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