From: Oliver Sanders Date: Thu, 19 Jun 2025 10:06:29 +0000 (+0100) Subject: websocket_ping: fix ping interval with non-zero timeout and improve docs. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=56fc23fe20b4f19ac6cfbbb25611ad692e45082b;p=thirdparty%2Ftornado.git websocket_ping: fix ping interval with non-zero timeout and improve docs. * Fix a bug that caused the ping interval to be less frequent than configured. * Fix erroneous documentation of the websocket_ping_timeout default and clarify units for the ping interval. --- diff --git a/docs/web.rst b/docs/web.rst index 956336bd..00066ccd 100644 --- a/docs/web.rst +++ b/docs/web.rst @@ -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: diff --git a/tornado/websocket.py b/tornado/websocket.py index b719547b..2e406369 100644 --- a/tornado/websocket.py +++ b/tornado/websocket.py @@ -1371,7 +1371,7 @@ class WebSocketProtocol13(WebSocketProtocol): 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()) class WebSocketClientConnection(simple_httpclient._HTTPConnection):