Skip to content

Occasionally stuck in loop during file write #50

Closed
@calcut

Description

@calcut

Run into an error where my watchdog timer has been getting triggered.
I'm working on a minimal example... but I've only seen the error very occasionally (maybe once a day) so it might be tricky.

I'm running something like this

with open('/sd/log.txt', 'a') as f:
    f.write(text)

I get this traceback:

  File "adafruit_sdcard.py", line 446, in readblocks
  File "adafruit_sdcard.py", line 289, in _block_cmd
  File "adafruit_sdcard.py", line 260, in _cmd
<WatchDogTimeout>

Which tells me the code is getting stuck in the _cmd() function due to
while buf[1] != 0xFE: never being satisfied.
Or at least not within a 60s watchdog timeout.

It looks like there is intended to be a timeout on this funciton (_CMD_TIMEOUT)
but it can fail due to the while loop.

relevant part of _cmd() is here:

# wait for the response (response[7] == 0)
for _ in range(_CMD_TIMEOUT):
    card.readinto(buf, end=1, write_value=0xFF)
    if not (buf[0] & 0x80):
        if response_buf:
            if data_block:
                # Wait for the start block byte
                buf[1] = 0xFF
                while buf[1] != 0xFE: 
                    card.readinto(buf, start=1, end=2, write_value=0xFF)  <-------- line 260
            card.readinto(response_buf, write_value=0xFF)
            if data_block:
                # Read the checksum
                card.readinto(buf, start=1, end=3, write_value=0xFF)
        return buf[0]
return -1

I'm going to try mimicing the for _ in range(_CMD_TIMEOUT): pattern instead of the while loop.
Can do a PR if it works.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions