Skip to content

WS: stale orderbook frames mutate local book before subscription-existence check #255

Closed

Description

@TexasCoding

Summary

Frames arriving with an old sid after resubscribe_one tears down the book and assigns a new sid (or after a server-initiated unsubscribe reaped the sub) still get applied to the local OrderbookManager. A stale snapshot can re-create a book under the old sid index and a stale delta can mutate the freshly-resynced book under the same ticker, producing inconsistent state that the consumer's get(ticker) reads.

Location

  • kalshi/ws/client.py:322-336 — apply happens before sid check
  • kalshi/ws/dispatch.py:240-243 — sid routing check (downstream)

Evidence

# client.py _process_frame:
if msg_type == "orderbook_snapshot" and self._orderbook_mgr:
    snapshot = OrderbookSnapshotMessage.model_validate(data)
    self._orderbook_mgr._apply_snapshot_inplace(snapshot, sid=snapshot.sid)
    pre_validated = snapshot
elif msg_type == "orderbook_delta" and self._orderbook_mgr:
    delta = OrderbookDeltaMessage.model_validate(data)
    self._orderbook_mgr._apply_delta_inplace(delta)
# Apply happens BEFORE dispatch.py:240-243 which drops unknown sid.

Recommended fix

Gate the orderbook-apply on self._sub_mgr.get_subscription_by_sid(sid) is not None before validating + mutating. Stale-sid frames should hit the dispatcher's existing debug-log path and short-circuit without touching the book.

Severity & category

medium / correctness, ws

Activity

  1. added
    bugSomething isn't working
    wsWebSocket-related
    on May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingwsWebSocket-related

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions