← Back to Home

TCP Socket Liveness: Eight Reasons send() and recv() Mislead

TCPsocketnetworking

I actually ran this on my own machine: one loopback server, one client, and a series of setups where the peer has already left. Then I wrote down exactly what Python reports. Environment: Windows 11 build 26300, CPython 3.13.14, everything on 127.0.0.1, no router in between.

The result is not flattering. A successful send() proves almost nothing, and which exception you get depends on how many seconds you set as the timeout. All eight findings below fit in about forty lines of code, included at the end.

TL;DR: judge liveness by what recv() gives you — empty bytes or an exception — and only then think about write probes. Not the other way around.

// FLOW_01Inferring connection state from return values6 NODES01STEP_01send returns a byte count02STEP_02data only reached the send buffer03STEP_03second send raises 1005304STEP_04peer reset or local abort05STEP_05recv returns empty bytes06STEP_06FIN received, orderly closeEND_OF_FLOW
Inferring connection state from return values · diagram drawn from this article
Four TCP liveness outcomes: send, 10053, EOF, RST
Four TCP liveness outcomes: send, 10053, EOF, RST · Concept illustration based on this article
PythonLinux
Technologies referenced · icons from Simple Icons (CC0)

The eight behaviours in one table

SituationWhat you seeWhat actually happened
Write after peer close, first trysend returns 1Data went into the local send buffer
Write after peer close, second tryConnectionAbortedError 10053Buffer cannot drain, local abort
Peer closes with unread datarecv raises 10054Peer sent RST, not FIN
Peer reads everything, then closesrecv returns b''FIN received, orderly finish
You call shutdown(SHUT_WR)Peer recv gets b''Half close, write direction only
Write after half closeBrokenPipeError 10058That direction is shut down
Connect to dead port, 2 s timeoutTimeoutErrorRefusal had not arrived yet
Connect to dead port, 25 s timeoutConnectionRefusedError 10061Refusal arrives at 2.05 s

One: send returns 1 while the connection is already gone

The server accepts and closes immediately without reading a single byte. The client's first send(b"x") returns 1. The second send(b"y") raises ConnectionAbortedError with winerror 10053.

The reason is that send() only promises "handed to the kernel send buffer", never "the peer received it". The first write and the peer's close crossed each other by half a round trip, so the data landed in the buffer first. By the second write, the local stack already knows the connection is dead and aborts.

The Python documentation is unusually blunt in the sendall() entry: on error, "there is no way to determine how much data, if any, was successfully sent". Plain send() does not even tell you that. Microsoft's error table defines 10053 as "An established connection was aborted by the software in your host computer" — note *your host*, meaning the local side gave up, not that the peer refused anything.

So liveness cannot come from writing. Either pair every write with a read, or use the approach in the next section.

Two: empty bytes and 10054 are different events

Two controlled runs:

Both are "the peer went away", but one is polite (FIN) and one is not (RST). The difference is whether the receive buffer still held unread data at close time.

RFC 9293 section 3.5.2 states the general rule: "reset (RST) is sent whenever a segment arrives that apparently is not intended for the current connection", and immediately qualifies it: "A reset must not be sent if it is not clear that this is the case". The stack does not fire RST at random, so when you see one, it means the stack believed something on that connection did not belong there.

This changes how you write read loops. If your loop is while True with data = sock.recv(4096) and break on empty bytes, an RST exits through an exception rather than the break. If the outer handler is a bare except OSError that logs one line, your metrics cannot tell an orderly shutdown from an interrupted one.

Three: shutdown is a half close, close tears down the whole thing

After shutdown(SHUT_WR), the peer's recv returns b'' (it got the FIN), while the local side can still read: in my run it read back b'resp-after-fin' that the peer sent after receiving the FIN. That is exactly the shutdown order request-response protocols should use: close the write direction to say "I am done", drain the remaining response, then close.

Writing after the half close fails immediately: send() raises BrokenPipeError with winerror 10058, defined as "A request to send or receive data was disallowed because the socket had already been shut down in that direction with a previous shutdown call". Calling shutdown(SHUT_WR) twice, on the other hand, silently succeeds.

One more surprise: after shutdown(SHUT_RD), recv does not return empty bytes — both recv and send raise 10053. On Windows that call leaves the connection in worse shape than most people expect.

Microsoft's graceful shutdown article explains what close really does: closesocket implicitly starts a shutdown sequence, and the default is "graceful shutdown, immediate return" — "the application has no way of knowing when (or whether) the graceful shutdown sequence actually completes". If the peer must receive everything, shut down explicitly instead of trusting close.

Four: a refused connection takes two seconds, so the exception depends on your timeout

This is the finding worth taking away.

create_connection(("127.0.0.1", dead_port), timeout=2) raises TimeoutError, not ConnectionRefusedError. Run the same call with timeout=25 and you get ConnectionRefusedError (10061) at 2.05 s; the IPv6 loopback ::1 returns it at 2.04 s.

So within roughly two seconds, this machine cannot deliver the "actively refused" answer at all. The SYN retransmit timer runs even on loopback, and the refusal lands around the 2-second mark.

The practical consequence: you write except ConnectionRefusedError to print "service is not up", but the actual timeout is 1 or 2 seconds, so that branch never fires. Every log line says TimeoutError, you go hunting for firewalls, and the real cause is a service that never started.

Two fixes. Give the connect at least 3 seconds before you interpret refusal. And handle TimeoutError and ConnectionRefusedError separately instead of collapsing both into "cannot connect".

Five: SO_REUSEADDR works backwards on Windows

On Linux the muscle memory is: the port is stuck in TIME_WAIT after a restart, binding fails, add SO_REUSEADDR and move on. That habit causes damage on Windows.

Four combinations, all against a port that the first socket is already listening on at 127.0.0.1:

Options on the second socketResult
Nothing setOSError 10048, address already in use
Only the second socket sets SO_REUSEADDRPermissionError 10013
Both sockets set SO_REUSEADDRBind succeeds
First sets SO_EXCLUSIVEADDRUSE, second sets SO_REUSEADDRPermissionError 10013

The second row is the trap: adding the option only to the newcomer turns 10048 into 10013, a different code for the same underlying conflict. Microsoft's bind page explains 10048 as "a process on the computer is already bound to the same fully qualified address and the socket has not been marked to allow address reuse with SO_REUSEADDR" — the condition hangs on whether the socket  was marked, not on the one calling bind.

Setting it on both binds successfully, but the connection still goes to the first listener (two consecutive accept calls landed on the first socket). That is not port sharing; it is an undefined handover rule.

The SO_EXCLUSIVEADDRUSE page is explicit: the option "prevents other sockets from being forcibly bound to the same address and port, a practice enabled by the SO_REUSEADDR socket option", and "Such reuse can be executed by malicious applications to disrupt the application".

So a Windows server should set SO_EXCLUSIVEADDRUSE (constant value -5, exposed as socket.SO_EXCLUSIVEADDRUSE), not copy the Linux SO_REUSEADDR line.

One extra control: after actively closing a connection, rebinding the same port immediately succeeded both with and without SO_REUSEADDR. Windows does not block rebinding the way Linux does during TIME_WAIT. The line you must add on Linux is the line that hurts you on Windows.

Six: SO_KEEPALIVE ships off, and the default check takes two hours

Defaults on a fresh socket: SO_KEEPALIVE is 0, TCP_NODELAY is 0, and both send and receive buffers are 65536 bytes.

Microsoft's page states "The SO_KEEPALIVE option for a socket is disabled (set to FALSE) by default", and "For TCP, the default keep-alive timeout is 2 hours and the keep-alive interval is 1 second".

Two hours. For a long-lived connection behind a NAT that expired, a peer that lost power, or a cable that was pulled, the default configuration takes two hours to tell you. If your service is push notifications, long polling, or a WebSocket upstream, that is the same as having no liveness check.

The good news is that the three knobs are settable on Windows now: TCP_KEEPIDLE, TCP_KEEPINTVL and TCP_KEEPCNT all accepted 30 and read back 30 in my run. The Python changelog records when this landed: TCP_KEEPCNT on Windows since 3.6.5, TCP_KEEPIDLE and TCP_KEEPINTVL since 3.7.

Set SO_KEEPALIVE to 1 first, or the three parameters have nothing to act on.

Seven: settimeout(0) is not "zero timeout"

After settimeout(0), recv() raises BlockingIOError with winerror 10035. That is not a timeout, it is "no data right now". The documentation says it directly: "If zero is given, the socket is put in non-blocking mode", and errors in non-blocking mode are "unfortunately system-dependent".

settimeout(0.3) is what puts the socket in timeout mode: my run raised TimeoutError after 0.303 s, with no errno, no winerror, and the message timed out.

One related trap: TimeoutError is a subclass of OSError. A bare except OSError swallows timeouts, resets and refusals alike, leaving you a single log line that says "network error".

The documentation separates the three modes cleanly: blocking waits until completion or a system error; non-blocking fails when an operation "cannot be completed immediately"; timeout mode raises if the operation does not finish within the given window. Pick the wrong one and the behaviour changes completely.

Eight: streams have no message boundaries

The client calls sendall twice with 100 bytes each. The server's single recv(1000) returns 200 bytes, starting with b'A' and ending with b'B'. The reverse holds too: three recv(1) calls returned b'h', b'e', b'l'.

TCP is a byte stream; the number of send calls has no relationship to the number of recv calls. Every "client sends one message at a time, server reads one message at a time" design is betting against coalescing — and Nagle is on by default (TCP_NODELAY is 0), so the stack is actively helping messages merge.

For a sense of scale: in non-blocking mode, sending until it blocked absorbed 458,752 bytes before raising 10035, which is 0.68% of the 64 MB I had prepared. In other words, "the write succeeded" is nearly guaranteed while the buffer has room, regardless of whether anyone is listening.

Troubleshooting: seven real exceptions and what to do

A liveness check you can copy

import socket

def peer_state(sock, timeout=3.0):
    sock.settimeout(timeout)
    try:
        data = sock.recv(1, socket.MSG_PEEK)
    except TimeoutError:
        return "alive_silent"
    except ConnectionResetError:
        return "reset"
    except OSError:
        return "broken"
    if data == b"":
        return "closed_by_peer"
    return "alive_with_data"

The trick is MSG_PEEK: it looks without consuming, so the following read still works. Empty bytes mean FIN, 10054 means RST, and a timeout is the only case that means "still alive but quiet". Decide about heartbeats after this function returns — doing it in the other order walks straight into finding one.

Where this stops

Liveness is really a question of which layer you trust. The kernel buffer, the protocol state machine, and the peer application each have their own answer, and the number send returns describes only the innermost one. The same "return value is not the result" pattern shows up in build tooling too, for example pip install reporting success and running old code.

This article contains no affiliate links.

FAQ

Q: Local tests are always fine, production throws 10054 occasionally. Is that my code?

Probably a middlebox. 10054 means an RST arrived, and RSTs do not only come from the peer application: load balancer idle timeouts and expired NAT entries both produce them. Check whether the interval is stable — if it always happens around minute N, it is idle reclamation, not your code.

Q: If sendall() succeeds, has the peer received the data?

No. It only guarantees everything was handed to the kernel send buffer. The documentation says it "continues to send data from bytes until either all data has been sent or an error occurs", where sent means sent locally, not acknowledged by the peer. Real confirmation requires an application-level reply.

Q: Should a Windows server set SO_REUSEADDR or SO_EXCLUSIVEADDRUSE?

SO_EXCLUSIVEADDRUSE. In my run a second process could bind the same port as long as both sides set SO_REUSEADDR, which is both a debugging nuisance and a hijacking entry point.

Q: Will a 30-second keepalive kill healthy connections?

TCP_KEEPCNT decides how many probes before the connection is declared dead, and all three settings matter. Lowering only TCP_KEEPIDLE while leaving TCP_KEEPCNT at its default makes detection take longer, not shorter. Keepalive packets are empty segments from the kernel, and some middleboxes treat them as anomalous traffic, so validate against your own path.

👉 Join Xiaomi MiMo Platform: Leading AI model platform with cost-effective inference

👉 Join Aliyun AI: Top AI products with exclusive coupons for business innovation

📌 This article was AI-assisted generated and human-reviewed | TechPassive — An AI-driven content testing site focused on real tool reviews

🔗 Recommended Tools

These are carefully selected tools. Using our affiliate links supports us to keep producing quality content:

As an Amazon Associate, I earn from qualifying purchases at no extra cost to you.

☁️ DigitalOcean Cloud ⚡ Vultr VPS 🤖 QoderWork CN (Refer & Earn) ☁️ Aliyun AI Products 📚 WordPress Books 🧠 AI & LLM Books 🏗️ System Design Books 🐳 Docker & K8s Books 🐧 Linux Admin Books 🐍 Python Books 🗄️ Database Books 🚀 DevOps & SRE Books 🤖 Xiaomi MiMo Platform
← Back to Home