Saltar al contenido principal

When a Connection Lies: Detecting and Healing Half-Open TCP in a Cluster

· 10 min de lectura
Clustron Team
Ingeniería de Sistemas Distribuidos

The cleanest way to lose data in a distributed system is not a crash. A crash is loud: the socket resets, the peer notices, failover kicks in, everyone agrees something happened. The quiet way is a connection that dies without telling anyone. One node keeps writing replication traffic into a TCP socket, the writes return success, the send buffer fills, and on the other end there is simply no one listening anymore — a peer that was network-partitioned, a middlebox that silently dropped the flow, a kernel that reset the far side while this side never got the memo. Nothing throws. Nothing logs an error. The connection is half-open, and it will stay that way, cheerfully accepting bytes that go nowhere, until something forces the issue.

For a store that replicates over TCP, this is the failure that matters most, because it is invisible at exactly the layer you would normally trust. socket.Connected is true. The write call succeeds. The health check — if it rides the same control channel — may even pass, because the control channel reconnected while the data channel stayed wedged. Meanwhile a partition's owner is trying to ship updates to its replica, the frames pile up in a send queue that never drains, and the replica falls further behind on every write. No alarm, no exception, just a backlog growing toward a cliff.

Zaris 2.2.0 ships the transport-layer fix for this, and the interesting part is not the reset — resets are easy — it is the detection. You cannot ask a half-open socket whether it is alive; it will lie. So the question becomes: what is the one symptom a wedged connection cannot fake?

The symptom that cannot be faked​

A healthy outbound connection has a property that is tedious to state and impossible for a dead one to imitate: bytes handed to it eventually leave. Not instantly — TCP buffers, flow-control pauses, a slow peer all delay the drain legitimately — but over any window of a few seconds, a working connection that has data queued will make the queue smaller. The frames written to the socket turn into frames the kernel acknowledges off the send buffer, and the backlog shrinks.

A half-open connection does the opposite in a very specific way. The send loop is alive — it is still pulling frames off the queue and calling write — so "is the sender thread running?" says yes. But the writes never drain, because there is no one on the far end to acknowledge them, so the kernel send buffer saturates and back-pressures. The result is a signature you can measure without trusting the socket at all: the backlog is non-zero, it is not shrinking, and the number of bytes that actually left the socket in the last interval is less than the backlog waiting to go. The send loop is alive but not draining. That is the fingerprint of a connection that has stopped carrying data while pretending it has not.

Zaris's pipelined transport watches exactly this, per outbound peer, on a sweep. It samples three things each pass — the current backlog, the previous backlog, and how many frames were genuinely written out since the last sample — and asks one question:

stalled  =  backlog > 0
&& backlog >= previousBacklog // queue is not shrinking
&& framesWrittenDelta < backlog // and we did not drain what is waiting

When that is true, the sweep logs it plainly — WEDGE DIAG data-outbound STALLED … writtenDelta=0 backlog=1981 (frames written but not draining — possible half-open) — and, crucially, does nothing drastic yet. One stalled sample is not proof. A peer can legitimately stall for a sweep: a GC pause, a burst of flow control, a momentary scheduling hiccup. Acting on a single sample would turn every transient blip into a connection reset, which is its own kind of damage. So detection is deliberately patient.

Patience, then a decisive reset​

The policy that decides whether a stall is real is the smallest piece of the system and the one that earns its keep: it counts consecutive stalled sweeps and only acts on a run of them.

// A single stall is noise. A sustained run of stalls is a wedged link.
const int ConsecutiveStallThreshold = 3;

bool ShouldReset(int consecutiveStalls) => consecutiveStalls >= ConsecutiveStallThreshold;

Three sweeps in a row where the backlog will not drain is no longer a blip — it is a connection that has stopped carrying data and is not coming back on its own. At that point the transport stops waiting for the socket to admit it is dead and forces the truth: it tears down the wedged outbound, abandons the send queue with a clear reason rather than letting it rot, and marks the path for a fresh dial on the next send. The logs say it in order — Send queue terminated … reason=no_available_connection, then Forcing fresh re-dial of wedged outbound … before map request. The next frame to that peer opens a brand-new connection, and because it is new, it either succeeds cleanly or fails loudly. Either way the lie is over.

Underneath this, the socket itself is configured to fail faster when the OS can tell: TCP_USER_TIMEOUT caps how long the kernel will keep retransmitting unacknowledged data before it gives up and surfaces an error. That catches the half-opens the OS can detect. The stall-sweep catches the ones it cannot — the application-level wedges where the kernel is content but the data is going nowhere. Belt and suspenders, aimed at the same failure from two layers.

Healing the backlog, not just the socket​

Resetting the connection is necessary but not sufficient. While the link was wedged, the owner kept accepting writes it could not ship, so the replica is now behind — by exactly the frames that piled up in that undrained queue. A fresh socket that carries new traffic correctly would still leave a hole where the stalled updates should have been.

So the reset is paired with a catch-up. Once the connection is re-dialed, the owner reconciles what the peer is missing and forward-backfills it: [ForwardCatchUp] Forward-backfilling 208 behind segment(s) to peer …. The behind segments are replayed over the healthy connection, the replica converges back to the owner's state, and the partition is whole again. The sequence end to end — detect the stall, confirm it across sweeps, reset the wedged link, re-dial fresh, backfill what fell behind — runs entirely inside the cluster, on a timer, with no operator in the loop and no process restart.

Ownersend queuehalf-open: writes don't drainbacklog ↑ writtenDelta = 0Replicafalling behindStall sweep3 consecutive stalls → resetreset wedged link → re-dial freshForwardCatchUp: backfill behind segmentsreplica converges — no operator, no restart

Why the patience matters as much as the reset​

It is tempting to read this as "Zaris resets dead connections," but the engineering is in the refusal to overreact. A detector that fires on the first stalled sample would be worse than no detector: healthy clusters stall transiently all the time, and resetting a connection has a real cost — an in-flight send is abandoned, a new handshake pays latency, and the backfill does work that a moment's patience would have made unnecessary. A system that resets at the first sign of slowness spends its life thrashing good connections and calling it resilience.

The three-sweep threshold is the whole design philosophy in one constant. It draws the line between slow and wedged at the only place the data can tell you the difference: a connection that is merely slow will drain something between sweeps and reset the counter; a connection that is wedged will not, three times running. Detection you can trust is detection that is willing to wait exactly long enough to be sure, and not one sweep longer.

Wrap-up​

Half-open connections are the dishonest failure: the socket says alive, the write says success, and the data goes nowhere. You cannot interrogate the connection, because the thing that is broken is the thing you would ask. What you can do is measure the one behaviour a working link has and a dead one cannot fake — that queued bytes eventually leave — and treat a sustained failure to drain as the ground truth a half-open socket will never volunteer. Zaris watches that signal per peer, confirms a stall across three consecutive sweeps so transient slowness is never mistaken for a wedge, resets and re-dials the link when the stall is real, and forward-backfills whatever the replica missed while the connection was lying. The cluster notices its own silent partition and heals it, on a timer, without waking anyone. That is the difference between a store that survives the failures that announce themselves and one that also survives the failures that don't.