Skip to content

Restart the server ping wait on any data from the server - #68

Closed
damirych wants to merge 1 commit into
centrifugal:masterfrom
damirych:restart-server-ping-wait-on-any-data
Closed

damirych wants to merge 1 commit into
centrifugal:masterfrom
damirych:restart-server-ping-wait-on-any-data

Conversation

@damirych

Copy link
Copy Markdown
Contributor

What changed

Client._process_incoming_data now restarts the server ping wait (_restart_ping_wait()) for every frame that carries at least one reply, as long as the wait is armed. Before, only a ping (_handle_ping) and a successful connect restarted it.

Why

The client drops a connection with "no ping" once ping interval + max_server_ping_delay passes without a ping, whatever else arrived meanwhile. A busy server may not ping while it is writing other messages, and then a connection that keeps delivering publications is dropped every 35 seconds (with ping: 25 and the default delay of 10).

I ran into this against a production Centrifugo 5.4.9 (connect reply ping: 25, pong: true) on a busy public channel:

  • A raw client, without this SDK, saw pings every 25.0 seconds while 1-6 publications a second arrived, and none from 57 s to the end of the run at 88 s while 30-75 a second arrived. The server kept that connection open and delivering the whole time.
  • Through the SDK, three connections in a row lived about 35 seconds each, never saw a ping, and ended with DisconnectedContext(code=2, reason='no ping').

I have not checked the server code to see why pings stop under load. The client side does not depend on it: centrifuge-js already restarts its server ping wait on any replies, in _dataReceived():

// Only data from the server proves the connection alive: not e.g. blank keep-alive
// lines an intermediary writes.
if (this._serverPing > 0 && replies.length > 0) {
  this._waitServerPing();
}

This may be what #33 ran into as well (a high-traffic client, with "no ping" among the disconnect reasons).

Only an armed wait is restarted. _do_disconnect cancels the timer and sets _ping_timer = None, then awaits the cancelled listen task. The message-processing task can still handle a queued frame in that window, and restarting the wait there would arm a new timer after the disconnect has cleared it, which could then fire as a second "no ping" during a slow reconnect. _handle_ping has the same window for pings. I left it as it is to keep this change to one thing.

Verification

Added tests/test_server_ping.py, which needs no Docker (it uses the in-process fake server, which never sends pings). The connect reply sets ping: 1 and the client max_server_ping_delay=0.5, so the client waits 1.5 seconds for a ping:

  • test_data_without_pings_keeps_the_connection publishes every 0.2 seconds for 3 seconds and asserts that the client never disconnected and is still CONNECTED. On master it fails with [(2, 'no ping')] != [].
  • test_silence_still_ends_the_connection sends nothing for 2.5 seconds and asserts that the client disconnected with (2, "no ping"). It passes on both, and guards the watchdog itself against this change.

Also ran:

  • tests/test_client.py against this repo's docker-compose Centrifugo: 62 tests OK.
  • The Docker-free suites (test_filter, test_fossil, test_proxy, test_ssl, test_state_invalidation, test_sub_refresh, test_background_tasks, test_ready, test_refresh, test_unsubscribe, test_utils) together with the new one: 63 tests OK.
  • ruff check . and ruff format --check: clean.

The same change, as a Client subclass override in an application, saw no "no ping" drops against that server in a 150-second busy run, in which the server sent a single ping.

🤖 Generated with Claude Code

The client dropped a connection with "no ping" once ping interval +
max_server_ping_delay passed without a ping, whatever else arrived
meanwhile. A busy server may skip pings while it writes other messages,
so a connection that kept delivering publications was dropped every
ping interval + delay. centrifuge-js restarts its server ping wait on
every batch of replies it receives; this does the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@FZambia

FZambia commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Hi @damirych

I'd prefer not to merge this right away. In Centrifuge, server pings go through the same per-connection write queue as publications, so the server never skips a ping because it's busy. A ping arrives late only when it's queued behind other messages, which means the connection or the client isn't keeping up. Then publications are delayed by the same amount. Reconnecting on "no ping" in that case is generally intended: it's a form of backpressure, and it usually points to an application that can't process the stream fast enough (e.g. slow on_publication handlers).

You're right that centrifuge-js restarts the wait on any data. That's an inconsistency between our SDKs (centrifuge-go waits for pings only). Probably it was a mistake to introduce this in centrifuge-js, and better to align with the rest of SDKs removing the restart instead of adding it in other SDKs 🤔 Sth to think about.

Getting no pings for ~30 seconds while publications keep arriving is unusual, though. If this happens without slow app, then maybe it's a signal of wrong server behavior. Would be nice to see a reproducer.

@damirych

Copy link
Copy Markdown
Contributor Author

Thanks for the explanation. Measurements confirm it, so this PR is closed.

A probe ran for 24 hours on the same machine as the SDK client: two raw JSON connections to the same Centrifugo 5.4.9 server (behind Cloudflare). It recorded every server ping, the round-trip time (RTT) of a WebSocket ping sent every 10 seconds, and the delay of each publication against a timestamp in its payload.

  • Server pings came more than 35 s apart 29 times in about 6,300 intervals. In 3 of them the client process itself was stalled. In all the others, WebSocket pongs on the same connection were late too, by seconds to tens of seconds, or never came before the connection was cut. The pings were late together with the stream, not skipped.
  • 239 of 274 drops followed such a backlog of over 5 s. They came in episodes that hit every connection from the machine, and the SDK client dropped within a minute of 222 of them. They ended as TCP cuts without a close frame, 3012 no pong or 3008 slow.
  • The client kept up: low CPU, the event loop on time. The backlogs also came at low publication rates, so the throughput of the network path dropped. Which segment of the path was not established.

Restarting the ping wait on data would only keep a connection that is tens of seconds behind for longer. The earlier observation of no pings while publications kept arriving was the same backlog seen from the client side.

🤖 Generated with Claude Code

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.

2 participants