Hard
Reusing a send buffer with MSG_ZEROCOPY
A publisher switches its 256KB snapshot sends to MSG_ZEROCOPY to save CPU. Every send() returns the full length. Afterward, the receiver occasionally decodes snapshots that mix bytes from two consecutive iterations, even though the stream is never desynchronized. What is going on?
main.cpp
int one = 1;
setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &one, sizeof(one));
static char buf[256 * 1024]; // one buffer, reused every iteration
for (;;) {
fill_snapshot(buf, sizeof(buf));
ssize_t n = send(fd, buf, sizeof(buf), MSG_ZEROCOPY);
// n == sizeof(buf) every time; loop refills buf immediately
}37 of 45 in Networking