| 1 |
Both sides |
| 2 |
========== |
| 3 |
|
| 4 |
.. currentmodule:: websockets.asyncio.connection |
| 5 |
|
| 6 |
What does ``ConnectionClosedError: no close frame received or sent`` mean? |
| 7 |
-------------------------------------------------------------------------- |
| 8 |
|
| 9 |
If you're seeing this traceback in the logs of a server: |
| 10 |
|
| 11 |
.. code-block:: pytb |
| 12 |
|
| 13 |
connection handler failed |
| 14 |
Traceback (most recent call last): |
| 15 |
... |
| 16 |
websockets.exceptions.ConnectionClosedError: no close frame received or sent |
| 17 |
|
| 18 |
or if a client crashes with this traceback: |
| 19 |
|
| 20 |
.. code-block:: pytb |
| 21 |
|
| 22 |
Traceback (most recent call last): |
| 23 |
... |
| 24 |
websockets.exceptions.ConnectionClosedError: no close frame received or sent |
| 25 |
|
| 26 |
it means that the TCP connection was lost. As a consequence, the WebSocket |
| 27 |
connection was closed without receiving and sending a close frame, which is |
| 28 |
abnormal. |
| 29 |
|
| 30 |
You can catch and handle :exc:`~websockets.exceptions.ConnectionClosed` to |
| 31 |
prevent it from being logged. |
| 32 |
|
| 33 |
There are several reasons why long-lived connections may be lost: |
| 34 |
|
| 35 |
* End-user devices tend to lose network connectivity often and unpredictably |
| 36 |
because they can move out of wireless network coverage, get unplugged from |
| 37 |
a wired network, enter airplane mode, be put to sleep, etc. |
| 38 |
* HTTP load balancers or proxies that aren't configured for long-lived |
| 39 |
connections may terminate connections after a short amount of time, usually |
| 40 |
30 seconds, despite websockets' keepalive mechanism. |
| 41 |
|
| 42 |
If you're facing a reproducible issue, :doc:`enable debug logs |
| 43 |
<../howto/debugging>` to see when and how connections are closed. |
| 44 |
|
| 45 |
What does ``ConnectionClosedError: sent 1011 (internal error) keepalive ping timeout; no close frame received`` mean? |
| 46 |
--------------------------------------------------------------------------------------------------------------------- |
| 47 |
|
| 48 |
If you're seeing this traceback in the logs of a server: |
| 49 |
|
| 50 |
.. code-block:: pytb |
| 51 |
|
| 52 |
connection handler failed |
| 53 |
Traceback (most recent call last): |
| 54 |
... |
| 55 |
websockets.exceptions.ConnectionClosedError: sent 1011 (internal error) keepalive ping timeout; no close frame received |
| 56 |
|
| 57 |
or if a client crashes with this traceback: |
| 58 |
|
| 59 |
.. code-block:: pytb |
| 60 |
|
| 61 |
Traceback (most recent call last): |
| 62 |
... |
| 63 |
websockets.exceptions.ConnectionClosedError: sent 1011 (internal error) keepalive ping timeout; no close frame received |
| 64 |
|
| 65 |
it means that the WebSocket connection suffered from excessive latency and was |
| 66 |
closed after reaching the timeout of websockets' keepalive mechanism. |
| 67 |
|
| 68 |
You can catch and handle :exc:`~websockets.exceptions.ConnectionClosed` to |
| 69 |
prevent it from being logged. |
| 70 |
|
| 71 |
There are two main reasons why latency may increase: |
| 72 |
|
| 73 |
* Poor network connectivity. |
| 74 |
* More traffic than the recipient can handle. |
| 75 |
|
| 76 |
See the discussion of :doc:`keepalive <../topics/keepalive>` for details. |
| 77 |
|
| 78 |
If websockets' default timeout of 20 seconds is too short for your use case, |
| 79 |
you can adjust it with the ``ping_timeout`` argument. |
| 80 |
|
| 81 |
How do I set a timeout on :meth:`~Connection.recv`? |
| 82 |
--------------------------------------------------- |
| 83 |
|
| 84 |
Use :func:`asyncio.timeout`:: |
| 85 |
|
| 86 |
async with asyncio.timeout(timeout=10): |
| 87 |
message = await websocket.recv() |
| 88 |
|
| 89 |
This technique works for most APIs. When it doesn't, for example with |
| 90 |
asynchronous context managers, websockets provides an ``open_timeout`` argument. |
| 91 |
|
| 92 |
How do I keep idle connections open? |
| 93 |
------------------------------------ |
| 94 |
|
| 95 |
websockets sends pings at 20 seconds intervals to keep the connection open. |
| 96 |
|
| 97 |
It closes the connection if it doesn't get a pong within 20 seconds. |
| 98 |
|
| 99 |
You can adjust this behavior with ``ping_interval`` and ``ping_timeout``. |
| 100 |
|
| 101 |
See :doc:`../topics/keepalive` for details. |
| 102 |
|
| 103 |
How do I respond to pings? |
| 104 |
-------------------------- |
| 105 |
|
| 106 |
If you are referring to Ping_ and Pong_ frames defined in the WebSocket |
| 107 |
protocol, don't bother, because websockets handles them for you. |
| 108 |
|
| 109 |
.. _Ping: https://datatracker.ietf.org/doc/html/rfc6455.html#section-5.5.2 |
| 110 |
.. _Pong: https://datatracker.ietf.org/doc/html/rfc6455.html#section-5.5.3 |
| 111 |
|
| 112 |
If you are connecting to a server that defines its own heartbeat at the |
| 113 |
application level, then you need to build that logic into your application. |
| 114 |
|
| 115 |
How can I pass arguments to a custom connection subclass? |
| 116 |
--------------------------------------------------------- |
| 117 |
|
| 118 |
You can bind additional arguments to the connection factory with |
| 119 |
:func:`functools.partial`:: |
| 120 |
|
| 121 |
import asyncio |
| 122 |
import functools |
| 123 |
from websockets.asyncio.server import ServerConnection, serve |
| 124 |
|
| 125 |
class MyServerConnection(ServerConnection): |
| 126 |
def __init__(self, *args, extra_argument=None, **kwargs): |
| 127 |
super().__init__(*args, **kwargs) |
| 128 |
# do something with extra_argument |
| 129 |
|
| 130 |
create_connection = functools.partial(ServerConnection, extra_argument=42) |
| 131 |
async with serve(..., create_connection=create_connection): |
| 132 |
... |
| 133 |
|
| 134 |
This example was for a server. The same pattern applies on a client. |