Skip to content

websocket_ping: fix ping interval with non-zero timeout and improve docs #3513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: branch6.5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions docs/web.rst
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,22 @@
of `UIModule` or UI methods to be made available to templates.
May be set to a module, dictionary, or a list of modules
and/or dicts. See :ref:`ui-modules` for more details.
* ``websocket_ping_interval``: If set to a number, all websockets will
be pinged every n seconds. This can help keep the connection alive
through certain proxy servers which close idle connections, and it
can detect if the websocket has failed without being properly closed.
* ``websocket_ping_timeout``: If the ping interval is set, and the
server doesn't receive a 'pong' in this many seconds, it will close
the websocket. The default is three times the ping interval, with a
minimum of 30 seconds. Ignored if the ping interval is not set.
* ``websocket_ping_interval``: If the ping interval has a non-zero
value, a ping will be sent periodically every
``websocket_ping_interval`` seconds, and the connection will be
closed if a response is not received before the
``websocket_ping_timeout``.
This can help keep the connection alive through certain proxy
servers which close idle connections, and it can detect if the
websocket has failed without being properly closed.
* ``websocket_ping_timeout``: For use with ``websocket_ping_interval``,
if the server does not receive a pong within this many seconds, it
will close the websocket_ping_timeout.
The default timeout is equal to the ping interval. The ping timeout
will be turned off if the ping interval is not set or if the
timeout is set to ``0``.
This can help to detect disconnected clients to avoid keeping
inactive connections open.

Authentication and security settings:

Expand Down
2 changes: 1 addition & 1 deletion tornado/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1371,7 +1371,7 @@ async def periodic_ping(self) -> None:
return

# wait until the next scheduled ping
await asyncio.sleep(IOLoop.current().time() - ping_time + interval)
await asyncio.sleep(ping_time + interval - IOLoop.current().time())
Copy link
Contributor Author

@oliver-sanders oliver-sanders Jun 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well this is embarrassing, the calculation of the next ping time was wrong, my apologies.

This caused pings to be less frequent than configured.



class WebSocketClientConnection(simple_httpclient._HTTPConnection):
Expand Down