Skip to content

command: don't resync onto the start of an invalid block - #195

Open
lastowl wants to merge 1 commit into
Arksine:masterfrom
lastowl:fix-resync-livelock
Open

lastowl wants to merge 1 commit into
Arksine:masterfrom
lastowl:fix-resync-livelock

Conversation

@lastowl

@lastowl lastowl commented Oct 27, 2026

Copy link
Copy Markdown

command_find_block() can livelock when a rejected block begins with MESSAGE_STX1. The resync memchr() starts at buf[0], finds the same byte, and pops nothing. Every later call fails on the same bytes, so the receive buffer never drains. Once it is more than half full, usb_cdc stops reading the bulk OUT endpoint and the device NAKs everything until it is reset. The same function is used by serial_irq.c and canserial.c, so the fix applies to those transports too.

On Creality K1 series printers this happens every time. Their udev rules probe new ttyACM devices by writing af ff 02 00 01 55 fa fd, which is the cause of #137. I'll put the details in that issue.

This change starts the resync search one byte past the start of the rejected block. Progress on the error path is then guaranteed. The entry path, with CF_NEED_SYNC set from an earlier call, still accepts a valid STX1 at buf[0].

Testing

  • On a K1 Max with a Cartographer V4 (stm32g431, Katapult v0.0.1-104-gfcb2f84), CONNECT always timed out after the udev probe. With the probe out of the way, unmodified flashtool.py connected and flashed from the printer.
  • Host harness built from the real command_find_block() and the usb_cdc bulk out loop:
    • K1 case: 0/6 → 6/6 frames dispatched.
    • 20,000 random junk prefixes, each followed by valid CONNECTs: 18,043 permanent stalls before, 572 after.
  • The remaining 572 are a separate case. Junk containing a fake 01 88 xx LEN header with a large LEN leaves 65–127 bytes buffered. usb_cdc won't read another packet until 64 bytes are free, so a block longer than what's buffered can never complete. That needs a header that's well-formed by chance, so I've left it out of this PR. Happy to look at it separately if you'd like.
  • test/configs/stm32g4.config builds cleanly: text grows 5240 → 5244 bytes.

Fixes #137

When command_find_block() rejects a block that begins with
MESSAGE_STX1, the resync search starts at buf[0], finds that same
byte and pops nothing.  Every later call fails on the same bytes, so
the receive buffer never drains.  Once it is more than half full,
usb_cdc stops reading the bulk out endpoint and the bootloader NAKs
all further data until it is reset.

This happens on Creality K1 series printers, whose udev rules probe
every new ttyACM device for an AI lidar by writing
"af ff 02 00 01 55 fa fd".  Katapult discards the first four bytes,
then livelocks on "01 55", so flashtool's CONNECT always times out.

Start the resync search after the first byte of the rejected block.

Fixes Arksine#137

Signed-off-by: Tom Hodgson <tom@justsanity.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MIPS support for flash_can.py?

1 participant