Ray's Knowledge Base
Unlock

Closing a socket with unread client data makes Linux reset the connection

PitfallVerified 5 Nov 2026Holds anywhere
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

A small local server answers each request and closes. Its tests pass on macOS. On Linux CI a test fails with a connection reset, and the client loses the answer.

Cause#

If the server closes while unread bytes are still in its receive buffer, Linux sends a reset (RST). The client can lose the data that the server already sent. macOS does not show this.

Fix#

After the answer, shut down the write side (shutdown(fd, SHUT_WR)). Then read and drop what the client still sends until end of input (or a short timeout). Then close.

Evidence#

In rvow's engine socket, a daemon test failed only on ubuntu CI (daemon_the_65th_connection_is_refused panicked). The shutdown and drain fix made all 14 CI jobs pass.

Also found by searching

  • connection reset by peer on Linux but not on macOS
  • server test fails in CI only on ubuntu with ECONNRESET
  • how do I close a socket after the answer without a reset