#30642·ccxt

C# WebSocket reconnect ignores its backoff delay and retains history after exchange close

Author: alert101Created Sep 25, 2026Updated Sep 25, 2026
Labelsbugwsc#

Operating System

Windows 11 x64

Programming Language

C#

CCXT Version

4.5.84

Description

Affected file(s)

  • cs/ccxt/ws/Client.cs (connect, Connect)
  • cs/ccxt/ws/Exchange.WsBridge.cs (calculateWsBackoffDelay, watch, watchMultiple)
  • cs/ccxt/base/Exchange.cs (Close)

Steps to reproduce

With a local in-process WebSocket server accepting connections, construct a WebSocketClient for its URL and call client.connect(60000). On v4.5.84, the server receives a connection immediately instead of waiting 60 seconds: connect passes no backoffDelay to the background Connect() method. The production watch paths calculate and pass a delay, so this affects reconnects rather than only direct client use.

The retained-history failure needs no server:

csharp
var exchange = new BaseExchange();
var first = exchange.calculateWsBackoffDelay("ws://localhost:1234/retry");
await exchange.Close();
var afterClose = exchange.calculateWsBackoffDelay("ws://localhost:1234/retry");
Console.WriteLine(first); // 0
Console.WriteLine(afterClose); // nonzero on v4.5.84; should restart at 0

Finally, invoke calculateWsBackoffDelay concurrently for the same URL while watching for exceptions or lost increments. Its backing Dictionary<string, long[]> and separate ContainsKey, indexed read, and write operations have no synchronization. An offline test can use 32 simultaneous callers and assert that only one gets the immediate first attempt; a stress test should cover concurrent URLs too.

Expected behavior

The first connection attempt should dial immediately. Subsequent attempts should honor the computed, capped reconnect delay, without one failed endpoint hammering its venue. Concurrent attempts should update backoff history consistently, and exchange close should discard that history so no stale URL entries or retry penalties survive its lifecycle. Closing during a scheduled delay should cancel the pending dial.

Actual behavior

watch and watchMultiple pass backoffDelay to client.connect(backoffDelay), but connect calls Connect() without forwarding or awaiting the delay; Connect starts a background ConnectAsync immediately. calculateWsBackoffDelay stores each URL in an unsynchronized instance dictionary that BaseExchange.Close never clears. A closed exchange therefore retains URL entries and computes a penalty on a subsequent call within stableAfter, even with no active clients.

Root cause

The backoff calculation and connection transport are disconnected: the calculated argument is unused. The dictionary update is a read-modify-write sequence without a lock, and exchange close has no backoff-state cleanup. The untracked connection tasks also prevent cancellation of a pending delay when a client is retired.

Suggested fix

Forward backoffDelay into an awaited, cancellable connect task and delay before attempting ConnectAsync; track that task as part of the client lifetime. Serialize the history read/update for each attempt and clear the state during exchange close. Add an offline local-server test that verifies a delayed reconnect does not dial immediately and closing cancels it; add deterministic first-attempt-after-close and concurrent-attempt tests. Keep the transport-lifetime work coordinated with the separate task-shutdown report rather than introducing a second connection owner.