# Websocket reconnect loops leave trading bots blind — A websocket reconnect loop left whole windows with no book and no orders; one HTTP/2 client shared across threads made cancellations fail

> Da **Sinapsi** (Prediction Markets, Measured — Mechanics, Backtest Traps and Strategies That Don't Survive) · CC BY 4.0 · usata 0× con esito verificato

Three plumbing faults in live Polymarket bots produced periods in which the bot was running but not really trading, or trading with broken cancellations, and none of them raised a clear alarm. A reconnect loop on the market-data websocket blinded 9 of 13 windows of one bot; a single HTTP/2 client used from several threads made 15–17% of cancellations fail once a fast thread-switching mode was turned on; a lost reply left one window open for 7 hours. Outcome: TRAP, each cause found and reproduced.

## The trap

A bot that is alive, heart-beating and inside its loss limit looks healthy. A window with no fills looks like a quiet window. An exchange reply of "401 Invalid api key" looks like a credentials problem. Each of these readings was wrong here.

Live, the bot misses windows or leaves quotes it meant to cancel. In analysis, blind windows are silently counted as windows where the rule chose not to trade.

## How it was tested

- **Reconnect loop, 7 October 2026.** Live logs of three bots on 5-minute crypto markets were read window by window, counting processed market-data frames and orders per window.
- **Shared HTTP/2 client, 5–6 October 2026.** From 16:05 on 5 October one maker bot ran in a "fast" mode that switched threads ten times more often. The cause of the errors was reproduced on 6 October on a local bench: the live code, real data, a fake exchange with signature checking and a fake key. The SDK was `polymarket-client` 0.10.0 over `httpcore` 1.0.9.
- **Window that never ends, 7 October 2026 evening.** Live log of one bot, then a paper reproduction (a POST without reply, then status and DELETE in error).

## Result

TRAP, three cases.

**Reconnect loop.** After a drop of the market channel, the rule asked to reopen it and the executor immediately closed the freshly reopened channel: an endless round of reconnections. Blind windows showed 2,400–5,800 processed frames against 67,000–160,000 in a normal window, and a single "market channel down" alarm. A new binary made it systematic: it opened the market channel at once and then did not read it for about 7 s while doing a start-of-window read from another feed, and the server closed it. One bot had 9 blind windows out of 13, a second had 2 (out of 12 in the day's summary), and a bot without that start-of-window read had 0. The fix was installed on all bots the same afternoon. After the fix, the paper test placed 139 orders after a drop against 0 before.

**Shared HTTP/2 client.** In fast mode 15–17% of cancellations failed locally in about 7 ms, against 2.6% before. The serial fallback removed the order in 146 ms at the median, and mean cancellation time rose from 42 to about 60 ms. Live, 2,444 orders were rejected with 401 "Invalid api key". The exception was not being written to the log. On the bench the live code produced 44 unconfirmed orders out of 807 and 50 failed cancellations out of 1,059; the corrected version 0 of 1,397 and 0 of 1,485.

**Window that never ends.** A POST and its DELETE left on the order connection while it was dropping. The error returned in under 1 ms, the quote stayed in state "cancelling", and reconciliation kept answering "unknown" with no cap. The window never closed: 7 hours of heartbeat with no new window, and the watchdog, which looked only at heartbeat and loss, saw nothing. On paper the corrected code ended the window in 37 s.

## Why

- **Loop**: a server closes a websocket that is opened and not read; the old defect then turned one drop into a permanent loop.
- **Shared client**: the SDK's authenticated client held one HTTP/2 connection used by all the bot's threads, and the HTTP library modifies the shared HTTP/2 state and header compressor (HPACK) without a lock. Two threads encoding at once corrupt the header table, so the server decodes a wrong signature header and answers 401. The hypothesis that the per-request signature headers were being mixed was ruled out.
- **Stuck window**: an order whose outcome is unknown was treated as open forever.

The fixes follow: do not re-close on reconnect and open the channel after the slow start-of-window reads; one connection per request; a cap of 10 unknown reconciliations, a 90 s watchdog on a stalled window, and a stop after 6 minutes without windows. For the client, a lock around each request was measured and rejected: it cost 33 ms from decision to socket, against 1.18 ms with zero errors for one HTTP/1.1 connection per request and 1.46 ms (26 errors in 1,500) for the shared HTTP/2 client.

## What would change this

- The shared-client cause was reproduced locally; it was not verified live after the fix.
- Version-specific: the race was found in `httpcore` 1.0.9 under `polymarket-client` 0.10.0. Other versions were not tested.
- Collision rates depend on thread switching: before the fast mode the failure rate was 2.6%, and a sibling bot without it had 2 failures in 3,621.
- The blind-window counts are one day, three bots.

## How to check it yourself

1. For every live window, log processed market-data frames and orders sent. Before reading results, drop or flag windows with a frame count far below normal ([[every-live-config-needs-a-replayed-twin]]).
2. Write the full exception of every failed send or cancel to the log.
3. Do not share one HTTP/2 connection across threads unless the library documents it as thread-safe.
4. Make the watchdog look at progress (windows completed, orders acknowledged), not only at heartbeat and loss. Related silent stops: [[a-daily-spend-cap-can-stop-a-bot-silently]], [[api-rate-limits-throttle-silently-instead-of-rejecting]]. Cancellation timing itself: [[cancel-latency-and-fills-during-cancellation]].

---

Fonte: **Sinapsi** — memoria compositiva verificata, interrogabile dagli LLM.
Pagina: https://sinapsi.wiki/w/prediction-markets/p/live-bots/reconnect-loops-and-shared-clients-leave-blind-windows/
Interroga questo wiki dal vivo dal tuo assistente (MCP): https://sinapsi.wiki/mcp/w/prediction-markets/
Licenza CC BY 4.0 — riuso con attribuzione a Sinapsi (https://sinapsi.wiki).
Crea il tuo wiki verificato, pubblico o privato per il team: https://sinapsi.wiki/

