← All posts
hardware

The bytes the driver threw away

· 6 min read · by aft3r_d0t

Duotone ink poster: a dark stream narrowing like a funnel as it pours straight down through pale swirling currents, a single drop falling beside it, its left edge breaking up into scattered dots.

Altar is the small ESP32 screen on my desk. It shows what the night crew is doing, and it reads its pictures from an SD card. To get a new file of about half a gigabyte onto that card, a hub program on my Mac sends it over USB in small pieces. How many pieces it may send before waiting for an answer is called the window. When a piece arrives broken, the screen asks for it again, and the sender goes back to it. We call that a rewind.

This is the story of why a bigger window gave us more rewinds and no speed, and of the four kilobytes where our bytes were going.

Eight against four

Each piece is 512 bytes of the file, wrapped in one line of JSON: an offset, a CRC-32 checksum and the data in base64. With every field at its longest, one line is 748 bytes. The screen checks the offset and the CRC, writes good data into a 16 KB buffer, flushes that buffer to the SD card every 32 pieces, and acknowledges each piece.

On 15 November we ran the same upload on the real board twice. At window 8 the screen asked for 23 rewinds before the run stopped at the 149 MB mark; 22 of them were CRC failures. (The stop itself was a USB link drop, a separate problem.) The run was then resumed at window 4 and carried on for another 245 MB. That stretch had zero rewinds and zero device errors. Later that day a whole 540 MB file went up at window 4, clean from start to end.

The part that made it interesting: window 8 wasn’t any faster. Both settings moved about 0.365 MB/s. We had been paying for the bigger window in broken pieces and getting nothing for it.

The shipped fix that day was the boring one. The default window went back to 4 on both sides, with a flag to override it. It worked. It also explained nothing, so one of us went looking for the cause.

Where the bytes wait

The research followed one line of JSON from the USB cable to the SD card, by reading the firmware and the vendor’s USB driver.

When USB data arrives, an interrupt handler copies each packet, at most 64 bytes, into a receive ring buffer. Our firmware sets that ring to 4,096 bytes. A task on the chip reads from the ring 128 bytes at a time, assembles lines, checks them and acknowledges them.

That same task also does the 16 KB write to the SD card, every 32nd piece. While it writes, it isn’t reading. The data that the sender already has in flight keeps arriving and piles up in the ring.

How much can pile up? If the piece that triggered the flush is still waiting for its acknowledgement, the sender can have W − 1 more lines behind it:

window (W − 1) × 748 B behind a stalled piece
4 2,244 B
6 3,740 B
8 5,236 B

The ring holds 4,096 bytes on paper, but the driver only adds a packet if the whole packet fits, so in practice it fills somewhere between 4,033 and 4,096. Window 4, 5 or 6 can never fill it, however long the SD card takes. Window 8 fills it whenever one write runs long enough.

On average a flush took 2.49 ms. At the measured transfer rate, the ring fills in somewhere between 2.7 and 7.6 ms, depending on how fast the Mac pushes the bytes in a burst, which we didn’t measure. So most flushes are just fast enough, and only the slow ones in the tail overflow. That fits what we saw: roughly one loss per 300 to 400 flushes, not one per flush.

What the driver does with a full ring

This is the line that matters, from the public ESP-IDF v5.5.6 USB driver:

xRingbufferSendFromISR(p_usb_serial_jtag_obj->rx_ring_buf, buf, rx_fifo_len, &xTaskWoken);

The function returns false when the packet doesn’t fit. The return value is ignored. By then the hardware has already accepted the packet and is ready for the next one, so it never tells the Mac to slow down. There is no error and no counter. The packet is simply gone.

The fingerprint

A theory like this predicts more than “some losses”. It predicts where they land.

When the 32nd piece triggers a flush at a 16 KB boundary, five more lines of about 747 bytes fit in the ring, and the sixth is the one that gets cut. Because whole 64-byte packets go missing, the base64 inside the line usually stays a valid length. The line still parses and still decodes. Only the checksum is wrong. Sometimes the cut runs past the end of the line and two lines merge into one that’s too long, and the next piece then arrives at the wrong offset.

Either way, the first refused piece should be the sixth after a flush boundary. That is offset 5 × 512 = 2,560, modulo 16,384.

The research counted every rewind from the four window-8 runs on 15 November: 55 of them, 51 CRC and 4 offset. All 55 sat at 2,560 modulo 16,384. Not most of them. All of them, at the one offset the arithmetic predicted. A second pass recounted the log independently and got the same 55.

The four window-4 runs had zero. If the window made no difference, the window-8 rate would have predicted about 38 rewinds over those 245 MB. Under a simple model, the chance of seeing zero by luck is around 4 × 10⁻¹⁷.

What we didn’t do

We didn’t flash a fix. There are a few on the table, each with a cost:

  • a bigger ring, 8 KB, which costs 4 KB of the chip’s scarce internal RAM, for good;
  • a separate writer task, so the reading task never stops for the SD card, which is the biggest change of the lot;
  • window 5 or 6, which the arithmetic says is safe on the current firmware;
  • or staying at window 4, which costs nothing and has measured no speed loss.

That choice is still open as I write this. There’s also a cheap test that would settle the mechanism on the board without flashing anything: upload at window 6, which should be clean, and at window 7, which should lose bytes at exactly 2,560 modulo 16,384. It hasn’t been run yet. And two other upload routes still default to window 8. By the same arithmetic they lose bytes too, but nobody has measured them.

The lesson I’m keeping

When a bigger window gives you no speed and a steady trickle of losses, the window isn’t a speed setting. It’s a measuring stick for a buffer you haven’t found yet.

The bug wasn’t really in the window, or in the SD card, or in the cable. It was a full buffer with nobody listening to its return value. Ours was 4,096 bytes in a driver we didn’t write. Yours might be a queue, a socket buffer, or a log shipper. If something drops data when it’s full, find out whether it says so. Ours didn’t.