Skip to content

WS MessageQueue: add deque(maxlen=maxsize+1) as defense-in-depth ceiling #173

Description

@TexasCoding

From #106 F-P-15 (Wave 5 polish backlog). Severity: low (defense-in-depth, no observed regression).

Current behavior

kalshi/ws/backpressure.pyMessageQueue uses a collections.deque with manual size tracking via a counter updated on put / get / DROP_OLDEST eviction. The counter is the sole bound on memory; if a future bug breaks the counter (e.g. a put path that forgets to increment, an exception between deque-append and counter-update) the deque can grow without bound.

The O(1) qsize() work from #103 already relies on this counter for correctness — a single drift point bounds both queue size and memory.

Fix

Construct the underlying deque with a hard ceiling:

self._buffer: deque[T] = deque(maxlen=maxsize + 1)

The maxlen is enforced by deque itself — independent of the counter. +1 is the safety margin so the manual DROP_OLDEST / RAISE logic still gets to observe the overflow boundary one slot before deque silently evicts.

Tests

  • Existing tests/ws/test_backpressure.py should continue to pass unchanged (the maxlen is loose enough to never trigger in correct operation).
  • Add a regression test that injects a corrupted counter state (q._size = 0 after multiple puts) and asserts the deque cannot exceed maxsize + 1 regardless.

Acceptance

  • One-line change in kalshi/ws/backpressure.py.
  • New regression test covering counter-drift safety.
  • No behavior change in passing path; tests/ws/test_backpressure.py green unchanged.

Metadata

Metadata

Assignees

No one assigned

    Labels

    polishCode-quality and DX improvements; non-functionalwsWebSocket-related

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions