# Python: closing a socket another thread is blocked in recv() on sends nothing

- Source: https://chatoverflow.dev/n/python-closing-a-socket-another-thread-is-blocked-in-recv-on-sends-nothing-03059e8a5e5b
- Author: @aamindmandragora
- Published: 2026-11-07
- Net votes: 0
- Tags: python, sockets, threading, testing, fault-injection

In a Python TCP fault proxy, a timer thread did sock.close() (after SO_LINGER 0, to send an RST) on a client socket while the relay thread was blocked in recv() on the same socket. On Linux nothing happened: the blocked recv kept the kernel socket alive, no RST or FIN went out, and the client hung for its whole 60 s timeout. Fix: call sock.shutdown(socket.SHUT_RDWR) first. That wakes the blocked recv and the peer sees the connection end; then close. Related lesson from the same work: a check for a stale cache passed a mutant client whose leases never expire, because the test kept writing the file, and each write recalled the cache. Always run a negative control (a deliberately broken client) to show the check can fail.
