-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
websocket disconnect and reconnect every 10 minutes #3258
Comments
Hmm, I can't think of anything in Tornado that would close a connection every 10 minutes (which suggests to me the possibility of proxy or NAT issues). What did the traffic look like in between these close packets? Were pings being sent back and forth ever 20 seconds as configured? |
I have also encountered this, I think there may be an issue with the way ping/pong responses are timed. E.G, when I use this diff: diff --git a/tornado/websocket.py b/tornado/websocket.py
index fbfd7008..0de0a439 100644
--- a/tornado/websocket.py
+++ b/tornado/websocket.py
@@ -11,6 +11,8 @@ defined in `RFC 6455 <http://tools.ietf.org/html/rfc6455>`_.
Removed support for the draft 76 protocol version.
"""
+from time import time
+
import abc
import asyncio
import base64
@@ -1230,6 +1232,7 @@ class WebSocketProtocol13(WebSocketProtocol):
self.close(self.close_code)
elif opcode == 0x9:
# Ping
+ print(f':ping: {time()}')
try:
self._write_frame(True, 0xA, data)
except StreamClosedError:
@@ -1237,6 +1240,7 @@ class WebSocketProtocol13(WebSocketProtocol):
self._run_callback(self.handler.on_ping, data)
elif opcode == 0xA:
# Pong
+ print(f':pong: {time()}')
self.last_pong = IOLoop.current().time()
return self._run_callback(self.handler.on_pong, data)
else:
@@ -1326,13 +1330,16 @@ class WebSocketProtocol13(WebSocketProtocol):
since_last_ping = now - self.last_ping
assert self.ping_interval is not None
assert self.ping_timeout is not None
+ print(f'{since_last_ping=} {since_last_pong=} {self.ping_interval=} {self.ping_timeout=}')
if (
since_last_ping < 2 * self.ping_interval
and since_last_pong > self.ping_timeout
):
+ print('CLOSE')
self.close()
return
+ print(f':ping: {time()}')
self.write_ping(b"")
self.last_ping = now With
The connection is closed before the first ping is even sent. When the connection is re-established it does the same thing again. I think the startup logic is causing this. When I repeat this experiment with the following diff: diff --git a/tornado/websocket.py b/tornado/websocket.py
index 0de0a439..9e4230ba 100644
--- a/tornado/websocket.py
+++ b/tornado/websocket.py
@@ -1307,7 +1307,7 @@ class WebSocketProtocol13(WebSocketProtocol):
"""Start sending periodic pings to keep the connection alive"""
assert self.ping_interval is not None
if self.ping_interval > 0:
- self.last_ping = self.last_pong = IOLoop.current().time()
+ self.last_ping = self.last_pong = IOLoop.current().time() + self.ping_interval # we won't get a pong until *after* the first ping is sent
self.ping_callback = PeriodicCallback(
self.periodic_ping, self.ping_interval * 1000
) I get different results:
Now, we get ping/pong responses as expected, however, the connection is eventually closed by the timeout logic despite the pong being received a fraction of a second within the ping (well within the timeout). I think the way ping/pong timings are worked out is a bit off. |
* Closes tornadoweb#3258 * Fixes an issue with the calculation of ping timeout interval that could cause connections to be erroneously timed out and closed from the server end.
* Closes tornadoweb#3258 * Fixes an issue with the calculation of ping timeout interval that could cause connections to be erroneously timed out and closed from the server end.
* Closes tornadoweb#3258 * Fixes an issue with the calculation of ping timeout interval that could cause connections to be erroneously timed out and closed from the server end.
* Closes tornadoweb#3258 * Fixes an issue with the calculation of ping timeout interval that could cause connections to be erroneously timed out and closed from the server end.
code:
Application(urlpatterns,
websocket_ping_interval=20,
websocket_ping_timeout=60)
Connection is disconnect and reconnect every 10 minutes
If:
delete websocket_ping_interval and websocket_ping_timeout
The connection has been normal
The text was updated successfully, but these errors were encountered: