Skip to content

fix(xtcp): tcp destination queue, deadlines, and re-dial (#345 freeze, #339 restart blind) - #352

Merged
randomizedcoder merged 4 commits into
mainfrom
redteam/tcp-dest-queue-worker
Nov 27, 2026
Merged

randomizedcoder merged 4 commits into
mainfrom
redteam/tcp-dest-queue-worker

Conversation

@randomizedcoder

Copy link
Copy Markdown
Owner

Closes #345. Fixes #339. Lands the product fixes the delivery-outage suite (PR #346) surfaced.

The findings

The fixes

  • pkg/xtcp/destinations_queue.go (new, untagged): envelopeQueue moves out of s3queue.go and is shared by both destinations — the tcp destination compiles into every flavor including stdlib-only min, so no build tag; backoffWindow kept verbatim (its jitter tests pin the attempt-4 and attempt-≥33 boundaries).
  • pkg/xtcp/destinations_tcp.go rewritten around a bounded queue + one background worker:
    • Send never blocks: evicts oldest, counts xtcp_tcp_dropped_total{reason} / _bytes_total + xtcp_tcp_buffered_bytes{stage="queue"}
    • SetWriteDeadline(now+writeTimeout) on every dial and write — a blocked write now fails in bounded time instead of freezing the poller
    • failed write closes the conn; the next attempt re-dials under full-jitter backoff (base 1s, cap = clamp(poll_frequency/10, 1s, 1h)), 3 attempts then drop-and-count
    • lazy dial — startup no longer aborts on a down sink; the worker dials when delivery begins
    • dial moved outside connMu — under it, Close's dropConn could block on the mutex for the length of a dial
    • Close drains the queue for tcpWorkerDrainTimeout (5s) then force-drops the conn
  • Knobs, proto-validated (fields 120–122) + env + flags: tcp_write_timeout (default 30s, belt [1s, 1h]), tcp_queue_depth (16), tcp_max_buffered_bytes; -tcpWriteTimeout / -tcpQueueDepth / -tcpMaxBufferedBytes; TCP_WRITE_TIMEOUT / TCP_QUEUE_DEPTH / TCP_MAX_BUFFERED_BYTES; deploy axis XTCP2_TCP_WRITE_TIMEOUT (Vagrantfile arg 12).
  • New destTCP counters: Dial, Reconnect, queueFull, dropped, Writes, WriteBytes — the blind is now VISIBLE and COUNTED while dark.
  • Regenerated protos (cpp/dart/go/openapi/python) + both flag goldens; TestBuildConfig fixture extended.

Scenario flips (expectations travel with fixes and fail CI)

  • rt-11-5: CONFIRMED → MITIGATED, 18/18 (20261126T111738-209ab068a-1688938, TCP_WRITE_TIMEOUT=5s, two poison cycles): polls keep issuing through the stall (25→47 over 32s — the freeze is gone), Write/Dial errors and drops counted while dark, scrape 200; de-poison heals delivery with no daemon restart (reconnects=1, 2498 lines/6s); cycle 2 repeats (Write/error 10→14, heals again at 2500 lines/6s).
  • rt-4-7: split → BLOCKED, 15/15 (20261126T110001-209ab068a-1548426): outage counted (Write/error=24, Dial/error=25, drops=9), polls continue, catcher restart heals delivery with no daemon restart (reconnects=1, 2482 lines/6s).

Testing

  • 7 tcp-destination tests green ×2 runs, race-clean (queue eviction, sink-down-at-start, reconnect, write-timeout, send-after-close)
  • All local gates green: golangci-lint-quick, golangci-lint, lint-baseline, flavor-tags (the queue move compiles in every build flavor), go test ./pkg/xtcp/ ./cmd/xtcp2/
  • find-verdict harness notes: a re-poisoned freshly re-dialed REACHABLE neighbor entry ignores gratuitous ARP until STALE — the cycle-2 flip is a bounded wait_for, not a sleep

…345 poller freeze and the #339 restart-on-sink-outage blind

The delivery-outage suite (PR #346) surfaced two ways a sink-side fault
could stop xtcp2 from reporting TCP statistics about the base OS and
non-attacker containers, while every /metrics scrape kept answering 200:

- #345 (rt-11-5, CONFIRMED 16/16): with the sink IP ARP-blackholed, a
  blocked Write "succeeds" into SO_SNDBUF and the retransmit window
  (hours at prod knobs) — the poller froze behind it with zero counters.
- #339 (rt-4-7): a down sink at startup aborted collection outright, and
  a sink that dies mid-run never re-dialed: tcpDest held a dead conn
  forever, so every record since the outage was lost silently.

Product fixes, driven by the scenarios:

- destinations_queue.go (new, untagged — the tcp destination compiles
  into every flavor incl. stdlib-only min): envelopeQueue moves out of
  s3queue.go and is shared; backoffWindow kept verbatim (its jitter
  tests pin the attempt-4 and attempt>=33 boundaries).
- destinations_tcp.go rewritten around a bounded queue + one background
  worker: Send never blocks (evicts oldest, counts
  xtcp_tcp_dropped_total{reason}/_bytes_total and buffered-bytes),
  SetWriteDeadline(now+writeTimeout) on every dial and write — a
  blocked write now fails in bounded time and re-dials under full-jitter
  backoff (cap clamped from poll_frequency/10) up to tcpWriteMaxAttempts
  before drop-and-count. Lazy dial: a down sink at start no longer
  aborts the daemon (#339's other half); the worker dials when delivery
  begins. Dial moved outside connMu — under it, Close's dropConn could
  block on the mutex for the length of a dial. Close drains the queue
  for tcpWorkerDrainTimeout then drops the conn.
- New knobs, all belt-bounded and proto-validated (fields 120-122):
  tcp_write_timeout (default 30s, [1s,1h]), tcp_queue_depth (16),
  tcp_max_buffered_bytes; flags -tcpWriteTimeout/-tcpQueueDepth/
  -tcpMaxBufferedBytes, env TCP_WRITE_TIMEOUT/TCP_QUEUE_DEPTH/
  TCP_MAX_BUFFERED_BYTES, deploy axis XTCP2_TCP_WRITE_TIMEOUT.
- New destTCP counters: Dial, Reconnect, queueFull, dropped, Writes,
  WriteBytes — the blind is now VISIBLE and COUNTED while dark.

Scenario flips, both re-run green on the fixed build:

- rt-11-5: CONFIRMED -> MITIGATED (18/18, 20261126T111738-209ab068a-
  1688938): polls keep issuing through the stall, Write/Dial errors and
  drops counted, de-poison heals with no restart; cycle 2 repeats.
- rt-4-7: split verdict -> BLOCKED end to end (15/15, 20261126T110001-
  209ab06-1548426): outage counted, polls continue, catcher restart
  heals delivery with no daemon restart (reconnects=1).

Fixes #345. Fixes #339.
The matrix re-provisions the same VMs for hours, and the cloud image's
apt-daily timers fired mid-run: rt-11-3, rt-2-9, rt-4-7, rt-11-4, and
rt-11-5 (the last five scenarios, in a cascade) all failed provisioning
with 'E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by
process (unattended-upgr)' — an environmental failure, not a product
one; every scenario before it passed, 28/33 total.

common.sh (the first provisioner on both guests) now stops and masks
the apt-daily timers and unattended-upgrades, then waits out any
in-flight run (bounded, 10 min) before the first apt call. The docker
block below needs the lock too on a fresh VM, so the guard runs before
it. attacker.sh already had this wait for re-provisions; common.sh is
where the race actually hit.
The matrix regression (28/33, five environmental dpkg-lock failures
fixed separately) surfaced a neighbor-state race in the rt-11-5
dark-window assert: the write deadline closes the conn, a re-dial falls
back to broadcast ARP, the REAL sink answers, and delivery SELF-HEALS
mid-poison — 1262 lines/6s while the attack continues (a REACHABLE
entry then ignores the poison's gratuitous replies for ~30s, so the
re-poison can't re-stall it inside the window). That run failed 16/18
with both misses being the scenario demanding shape A (sustained stall,
drops counted) when the product had delivered shape B (healed-through,
zero drops, loss bounded to the reconnect window) — a stronger
mitigation, not a regression.

The scenario now asserts the invariants that matter and accepts either
shape: Write/Dial errors MUST rise (flowing with zero errors = the
poison failed or the stall went silent), the poller MUST keep issuing,
and de-poison MUST heal without a restart. Shape A still requires the
drops be counted (with a late-heal escape — an envelope budget takes
~30s to burn); cycle 2 requires new Write errors (a flipped entry makes
the in-flight write error within ~5s, so no new errors = a silent
stall). Re-verified 18/18 MITIGATED, shape A
(20261127T071651-195d45c15-986471): dark 0 lines/6s, drops=3, polls
52->77 through the stall, heals at reconnects=1, cycle 2 repeats.
@randomizedcoder
randomizedcoder merged commit 14d7e1c into main Nov 27, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant