Timers crash file IO
Posted: Sat Sep 08, 2018 2:14 am
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. Firstly it loads a `boot_file.txt` then deletes it. Then it loops writing data once every few minutes, press the switch to terminate gracefully. Writing the saved state to `boot_file.txt` for next time and unmount the sd card. In the progam's initial sequence i use a timer to flash an LED using a callback. when done i cancel the callback with `timer.callback(None)`.
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.
Here's a minimium example
For this bug to happen it appears i need all the events
1) a previously used timer
2) a boot file which is deleted on boot and re written on exit
3) writing of data in a sleepy loop
This is a bug and i haven't done some thing wrong?
update:
if i re start repl whilst there are LEDs still on I get the message unable to open `/dev/ttyACM0`
if i wait until they extinguish the repl session is totally un responsive except to pressing CTRL+D (soft reboot). This just causes the pyboard to flash the red and green lights (error?), no message printed.
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.
Here's a minimium example
Code: Select all
import pyb
import os
pressed = False
def switch_cb():
global pressed
pressed = True
def timer_cb(timer):
pyb.LED(1).toggle()
def main():
pyb.LED(2).on()
os.mount(pyb.SDCard(), '/sd')
os.chdir('/sd')
try:
with open("boot_file.txt","r") as f:
info=f.readline()
except OSError:
info=None
if "boot_file.txt" in os.listdir():
os.remove("boot_file.txt")
blue=pyb.LED(4)
blue.off()
yellow=pyb.LED(3)
yellow.on()
print("")
with open("data_file.txt","a") as f:
while not pressed:
f.write("hello world.\n")
print(".",end="")
for _ in range(60):
if pressed:
break
pyb.delay(1000)
yellow.toggle()
blue.toggle()
print("")
blue.off()
yellow.off()
with open("boot_file.txt", "w") as f:
f.write("saved state for next time\n")
os.umount('/sd')
pyb.LED(2).off()
if __name__ == "__main__":
switch = pyb.Switch()
switch.callback(switch_cb)
timer = pyb.Timer(14, freq=3)
timer.callback(timer_cb)
pyb.delay(5000)
timer.callback(None)
main()
1) a previously used timer
2) a boot file which is deleted on boot and re written on exit
3) writing of data in a sleepy loop
This is a bug and i haven't done some thing wrong?
update:
if i re start repl whilst there are LEDs still on I get the message unable to open `/dev/ttyACM0`
if i wait until they extinguish the repl session is totally un responsive except to pressing CTRL+D (soft reboot). This just causes the pyboard to flash the red and green lights (error?), no message printed.