Skip to content

223. Why the verification read failed, which is the host and not the remote

Section 222 recorded a read that failed twice before it worked and said the cause was unknown. It is known now, it is on this machine rather than on the remote, and it was reproduced on demand with a predicted loss count.

The sequence gap said how many chunks went missing, and it was a whole number. A READ_FLASH answer is a run of reports each carrying a sequence byte that advances by 0x11, and the reader refuses a gap. The failure reported expecting 0xbf and getting 0x14, which is 0xbf plus five steps: five chunks, 310 bytes, gone. Not corrupted, not reordered, absent. The arithmetic needs the modular inverse of 17, which is 241; dividing the gap by 17 gives a fraction and reads as "not a whole number of chunks", which is what a first attempt at this concluded.

Reports are dropped by the host's own USB library, on purpose and in silence. HIDAPI's macOS backend queues incoming reports on a linked list and, when more than thirty are waiting, discards the oldest to stop the list growing for a caller that never reads. A READ_FLASH of 16 KiB is 264 reports streamed back as fast as the remote can send them, so anything that holds the consumer up for longer than the queue takes to fill loses whatever arrived in the meantime, and the library says nothing.

The prediction, and the measurement that matches it

The rate is measurable: 30 KiB/s over a whole configuration is about 487 reports a second. The queue holds 31. So a stall of t seconds should lose 487t - 31 chunks, and a stall short enough to fit in the queue should lose none. Written down before the runs:

stall reports arriving predicted lost measured
none 0 0, on 266 chunks
20 ms 10 0 0
50 ms 24 0 0
100 ms 48 18 17
200 ms 97 66 67

Both within one, from a queue depth read out of the library's source and a rate measured off a different run. packages/usb/bin/read-burst-probe.ts is the instrument, and it drives the transport directly rather than readFlash, so that it leaves the pipe in the state the failure leaves it.

The second half, which is worse than the drop

A failed read left the remote streaming, and the next command read the leftovers. That is why the second attempt in section 222 failed too, and why its third began by answering a GET_VERSION with a flash data reply. It is also visible in this section's own runs: four stalls in a row alternate between failing on the stall and failing on the version read that opens the next run.

So the drop cost one read and the dirty pipe cost every read after it, which is the more damaging of the two and the cheaper to fix. readFlash now drains the rest of a failed answer up to its acknowledgement before reporting the failure, bounded by the same idle poll count as a read, and the drain cannot itself throw: replacing the real failure with whatever went wrong tidying up is the substitution the rehearsal's own finally already refuses.

What is done about the drop itself

A window is asked for again, up to twice, in readConfig rather than in readFlash. The library reports what happened; deciding that asking again is safe requires knowing the request has no side effect, and the pipeline is the layer that knows. The count is returned and printed rather than absorbed, because a link that needs a retry at every window is a fault to see and not to swallow.

Nothing here changes what a good read has to satisfy. Every window is still checked by the sequence, the whole file by the end marker and by the trailer checksum, and a retried read is not a degraded one.

The fix exercised on hardware, because a clean read exercises none of it

Asked for by Danny, and the objection was right: the read that followed the fix needed no retry, so it demonstrated nothing about the new code. A stall had to be forced through the production path.

The first attempt failed for a reason worth keeping: a timer callback that busy waits never fires during a read. node-hid's readTimeout blocks synchronously, so the read loop yields only to microtasks and the event loop never reaches the timer phase. The whole transfer is effectively one synchronous run. So whatever stalls a read in the wild is something that can interrupt a synchronous loop, which is a garbage collection or the operating system, and not other work this process scheduled.

The stall therefore went inside the loop as a temporary control, reversed immediately after the run and the reversal checked against the committed file. Two stalls of 200 ms, and the result is the whole mechanism working:

  • window 0x040010 failed, was asked for again, failed again, and succeeded on the third attempt, which is exactly the budget of two retries
  • the read completed, 1665900 bytes, byte identical to the dump taken before the first write
  • the retry count came back as 2 and was printed as it happened

The drain is demonstrated by the second stall rather than asserted. It fired 3000 bytes into attempt two, which means attempt two was reading its own answer normally that far in. Without the drain, attempt two would have failed immediately on the leftovers of attempt one and never reached the stall at all.

A clean read afterwards, with the control reversed, needed no retry and matches the same dump.

What this does not explain, and one thing it puts a name to

It does not say what stalled this process for a fifth of a second in the middle of a read. A garbage collection is the obvious candidate and nothing here measured one.

It does explain a hazard that had only been described. docs/usb-protocol.md and the reader's own comment have said since section 139 that a dropped report is the failure a chunked transfer over HID actually has, and that the sequence check is what turns it from silent corruption into an error. That was reasoning about a protocol. It is now a measured property of this bench, with a queue depth, a rate and a threshold.

It is not the stranding fault, which is a remote that stops answering after sitting idle and needs its batteries out. That one is untouched and still unexplained.


Section 222 | the archive | section 224. Section 223 of the findings archive, which is frozen; the reference holds the current answer. The archive on GitHub.