Skip to content

fix(ticker): invoke sendPing in ping loop to detect connection drops - #245

Open
AnkitaAdvitot wants to merge 1 commit into
zerodha:masterfrom
AnkitaAdvitot:fix-ticker-ping-loop
Open

AnkitaAdvitot wants to merge 1 commit into
zerodha:masterfrom
AnkitaAdvitot:fix-ticker-ping-loop

Conversation

@AnkitaAdvitot

Copy link
Copy Markdown

Description

Fixes #237

In kiteconnect/ticker.py, KiteTickerClientProtocol._loop_ping was scheduled during onOpen to maintain connection health and refresh _last_ping_time. However, the method only scheduled the next callback and never transmitted a ping frame (self.sendPing() was never called).

Because of this:

  1. self._last_pong_time remained None indefinitely, rendering the _loop_pong_check connection drop detector inactive.
  2. Silent network drops (e.g. WiFi/ISP drop without TCP RST) left the socket in an undetected zombie state until OS TCP timeouts killed it.

Solution

  1. Call self.sendPing() inside _loop_ping() when self.state == self.STATE_OPEN, wrapped in a safe try...except block.
  2. Initialize self._last_pong_time = time.time() in onOpen() so timeout tracking starts immediately from handshake completion.
  3. In onClose(), safely check DelayedCall.active() before cancelling _next_ping and _next_pong_check.
  4. Add unit tests in tests/unit/test_ticker.py verifying that:
    • _loop_ping invokes sendPing and updates _last_ping_time.
    • _loop_pong_check calls dropConnection when pong timeout is exceeded.
    • _loop_pong_check remains connected when heartbeats are healthy.
    • onOpen initializes _last_pong_time and triggers _loop_ping.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KiteTicker connection drop is not detected due to non-functional ping loop (_loop_ping does not send pings)

2 participants