connection.py 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  1. from __future__ import annotations
  2. import concurrent.futures
  3. import contextlib
  4. import logging
  5. import random
  6. import socket
  7. import struct
  8. import threading
  9. import time
  10. import traceback
  11. import uuid
  12. import weakref
  13. from collections.abc import Iterable, Iterator, Mapping
  14. from types import TracebackType
  15. from typing import Any, Literal, Self, overload
  16. from ..exceptions import (
  17. ConcurrencyError,
  18. ConnectionClosed,
  19. ConnectionClosedOK,
  20. ProtocolError,
  21. )
  22. from ..frames import DATA_OPCODES, PONG, CloseCode, Frame
  23. from ..http11 import Request, Response
  24. from ..protocol import CLOSED, CONNECTING, OPEN, Event, Protocol, State
  25. from ..typing import BytesLike, Data, DataLike, LoggerLike, Subprotocol
  26. from .messages import Assembler
  27. from .utils import Deadline
  28. __all__ = ["Connection"]
  29. class Connection:
  30. """
  31. :mod:`threading` implementation of a WebSocket connection.
  32. :class:`Connection` provides APIs shared between WebSocket servers and
  33. clients.
  34. You shouldn't use it directly. Instead, use
  35. :class:`~websockets.sync.client.ClientConnection` or
  36. :class:`~websockets.sync.server.ServerConnection`.
  37. """
  38. recv_bufsize = 65536
  39. def __init__(
  40. self,
  41. sock: socket.socket,
  42. protocol: Protocol,
  43. *,
  44. ping_interval: float | None = 20,
  45. ping_timeout: float | None = 20,
  46. close_timeout: float | None = 10,
  47. max_queue: int | None | tuple[int | None, int | None] = 16,
  48. ) -> None:
  49. self.socket = sock
  50. self.protocol = protocol
  51. self.ping_interval = ping_interval
  52. self.ping_timeout = ping_timeout
  53. self.close_timeout = close_timeout
  54. if isinstance(max_queue, int) or max_queue is None:
  55. max_queue_high, max_queue_low = max_queue, None
  56. else:
  57. max_queue_high, max_queue_low = max_queue
  58. # Inject reference to this instance in the protocol's logger.
  59. self.protocol.logger = logging.LoggerAdapter(
  60. self.protocol.logger,
  61. {"websocket": weakref.proxy(self)},
  62. )
  63. # Copy attributes from the protocol for convenience.
  64. self.id: uuid.UUID = self.protocol.id
  65. """Unique identifier of the connection. Useful in logs."""
  66. self.logger: LoggerLike = self.protocol.logger
  67. """Logger for this connection."""
  68. self.debug = self.protocol.debug
  69. # HTTP handshake request and response.
  70. self.request: Request | None = None
  71. """Opening handshake request."""
  72. self.response: Response | None = None
  73. """Opening handshake response."""
  74. # Mutex serializing interactions with the protocol.
  75. self.protocol_mutex = threading.Lock()
  76. # Lock stopping reads when the assembler buffer is full.
  77. self.recv_flow_control = threading.Lock()
  78. # Assembler turning frames into messages and serializing reads.
  79. self.recv_messages = Assembler(
  80. max_queue_high,
  81. max_queue_low,
  82. pause=self.recv_flow_control.acquire,
  83. resume=self.recv_flow_control.release,
  84. )
  85. # Deadline for the closing handshake.
  86. self.close_deadline: Deadline | None = None
  87. # Whether we are busy sending a fragmented message.
  88. self.send_in_progress = False
  89. # Mapping of ping IDs to pong waiters, in chronological order.
  90. self.pending_pings: dict[bytes, tuple[threading.Event, float, bool]] = {}
  91. self.latency: float = 0.0
  92. """
  93. Latency of the connection, in seconds.
  94. Latency is defined as the round-trip time of the connection. It is
  95. measured by sending a Ping frame and waiting for a matching Pong frame.
  96. Before the first measurement, :attr:`latency` is ``0.0``.
  97. By default, websockets enables a :ref:`keepalive <keepalive>` mechanism
  98. that sends Ping frames automatically at regular intervals. You can also
  99. send Ping frames and measure latency with :meth:`ping`.
  100. """
  101. # Thread that sends keepalive pings. None when ping_interval is None.
  102. self.keepalive_thread: threading.Thread | None = None
  103. # Exception raised while reading from the connection, to be chained to
  104. # ConnectionClosed in order to show why the TCP connection dropped.
  105. self.recv_exc: BaseException | None = None
  106. # Receiving events from the socket. This thread is marked as daemon to
  107. # allow creating a connection in a non-daemon thread and using it in a
  108. # daemon thread. This mustn't prevent the interpreter from exiting.
  109. self.recv_events_thread = threading.Thread(
  110. target=self.recv_events,
  111. daemon=True,
  112. )
  113. # Start recv_events only after all attributes are initialized.
  114. self.recv_events_thread.start()
  115. # Public attributes
  116. @property
  117. def local_address(self) -> Any:
  118. """
  119. Local address of the connection.
  120. For IPv4 connections, this is a ``(host, port)`` tuple.
  121. The format of the address depends on the address family.
  122. See :meth:`~socket.socket.getsockname`.
  123. """
  124. return self.socket.getsockname()
  125. @property
  126. def remote_address(self) -> Any:
  127. """
  128. Remote address of the connection.
  129. For IPv4 connections, this is a ``(host, port)`` tuple.
  130. The format of the address depends on the address family.
  131. See :meth:`~socket.socket.getpeername`.
  132. """
  133. return self.socket.getpeername()
  134. @property
  135. def state(self) -> State:
  136. """
  137. State of the WebSocket connection, defined in :rfc:`6455`.
  138. This attribute is provided for completeness. Typical applications
  139. shouldn't check its value. Instead, they should call :meth:`~recv` or
  140. :meth:`send` and handle :exc:`~websockets.exceptions.ConnectionClosed`
  141. exceptions.
  142. """
  143. return self.protocol.state
  144. @property
  145. def subprotocol(self) -> Subprotocol | None:
  146. """
  147. Subprotocol negotiated during the opening handshake.
  148. :obj:`None` if no subprotocol was negotiated.
  149. """
  150. return self.protocol.subprotocol
  151. @property
  152. def close_code(self) -> int | None:
  153. """
  154. State of the WebSocket connection, defined in :rfc:`6455`.
  155. This attribute is provided for completeness. Typical applications
  156. shouldn't check its value. Instead, they should inspect attributes
  157. of :exc:`~websockets.exceptions.ConnectionClosed` exceptions.
  158. """
  159. return self.protocol.close_code
  160. @property
  161. def close_reason(self) -> str | None:
  162. """
  163. State of the WebSocket connection, defined in :rfc:`6455`.
  164. This attribute is provided for completeness. Typical applications
  165. shouldn't check its value. Instead, they should inspect attributes
  166. of :exc:`~websockets.exceptions.ConnectionClosed` exceptions.
  167. """
  168. return self.protocol.close_reason
  169. # Public methods
  170. def __enter__(self) -> Self:
  171. return self
  172. def __exit__(
  173. self,
  174. exc_type: type[BaseException] | None,
  175. exc_value: BaseException | None,
  176. traceback: TracebackType | None,
  177. ) -> None:
  178. if exc_type is None:
  179. self.close()
  180. else:
  181. self.close(CloseCode.INTERNAL_ERROR)
  182. def __iter__(self) -> Iterator[Data]:
  183. """
  184. Iterate on incoming messages.
  185. The iterator calls :meth:`recv` and yields messages in an infinite loop.
  186. It exits when the connection is closed normally. It raises a
  187. :exc:`~websockets.exceptions.ConnectionClosedError` exception after a
  188. protocol error or a network failure.
  189. """
  190. try:
  191. while True:
  192. yield self.recv()
  193. except ConnectionClosedOK:
  194. return
  195. # This overload structure is required to avoid the error:
  196. # "parameter without a default follows parameter with a default"
  197. @overload
  198. def recv(self, timeout: float | None, decode: Literal[True]) -> str: ...
  199. @overload
  200. def recv(self, timeout: float | None, decode: Literal[False]) -> bytes: ...
  201. @overload
  202. def recv(self, timeout: float | None = None, *, decode: Literal[True]) -> str: ...
  203. @overload
  204. def recv(
  205. self, timeout: float | None = None, *, decode: Literal[False]
  206. ) -> bytes: ...
  207. @overload
  208. def recv(
  209. self, timeout: float | None = None, decode: bool | None = None
  210. ) -> Data: ...
  211. def recv(self, timeout: float | None = None, decode: bool | None = None) -> Data:
  212. """
  213. Receive the next message.
  214. When the connection is closed, :meth:`recv` raises
  215. :exc:`~websockets.exceptions.ConnectionClosed`. Specifically, it raises
  216. :exc:`~websockets.exceptions.ConnectionClosedOK` after a normal closure
  217. and :exc:`~websockets.exceptions.ConnectionClosedError` after a protocol
  218. error or a network failure. This is how you detect the end of the
  219. message stream.
  220. If ``timeout`` is :obj:`None`, block until a message is received. If
  221. ``timeout`` is set, wait up to ``timeout`` seconds for a message to be
  222. received and return it, else raise :exc:`TimeoutError`. If ``timeout``
  223. is ``0`` or negative, check if a message has been received already and
  224. return it, else raise :exc:`TimeoutError`.
  225. When the message is fragmented, :meth:`recv` waits until all fragments
  226. are received, reassembles them, and returns the whole message.
  227. Args:
  228. timeout: Timeout for receiving a message in seconds.
  229. decode: Set this flag to override the default behavior of returning
  230. :class:`str` or :class:`bytes`. See below for details.
  231. Returns:
  232. A string (:class:`str`) for a Text_ frame or a bytestring
  233. (:class:`bytes`) for a Binary_ frame.
  234. .. _Text: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  235. .. _Binary: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  236. You may override this behavior with the ``decode`` argument:
  237. * Set ``decode=False`` to disable UTF-8 decoding of Text_ frames and
  238. return a bytestring (:class:`bytes`). This improves performance
  239. when decoding isn't needed, for example if the message contains
  240. JSON and you're using a JSON library that expects a bytestring.
  241. * Set ``decode=True`` to force UTF-8 decoding of Binary_ frames and
  242. return strings (:class:`str`). This may be useful for servers that
  243. send binary frames instead of text frames.
  244. Raises:
  245. ConnectionClosed: When the connection is closed.
  246. ConcurrencyError: If two threads call :meth:`recv` or
  247. :meth:`recv_streaming` concurrently.
  248. """
  249. self.maybe_raise_legacy_warning()
  250. try:
  251. return self.recv_messages.get(timeout, decode)
  252. except EOFError:
  253. pass
  254. # fallthrough
  255. except ConcurrencyError:
  256. raise ConcurrencyError(
  257. "cannot call recv while another thread "
  258. "is already running recv or recv_streaming"
  259. ) from None
  260. except UnicodeDecodeError as exc:
  261. with self.send_context():
  262. self.protocol.fail(
  263. CloseCode.INVALID_DATA,
  264. f"{exc.reason} at position {exc.start}",
  265. )
  266. # fallthrough
  267. # Wait for the protocol state to be CLOSED before accessing close_exc.
  268. self.recv_events_thread.join()
  269. raise self.protocol.close_exc from self.recv_exc
  270. @overload
  271. def recv_streaming(self, decode: Literal[True]) -> Iterator[str]: ...
  272. @overload
  273. def recv_streaming(self, decode: Literal[False]) -> Iterator[bytes]: ...
  274. @overload
  275. def recv_streaming(self, decode: bool | None = None) -> Iterator[Data]: ...
  276. def recv_streaming(self, decode: bool | None = None) -> Iterator[Data]:
  277. """
  278. Receive the next message frame by frame.
  279. This method is designed for receiving fragmented messages. It returns an
  280. iterator that yields each fragment as it is received. This iterator must
  281. be fully consumed. Else, future calls to :meth:`recv` or
  282. :meth:`recv_streaming` will raise
  283. :exc:`~websockets.exceptions.ConcurrencyError`, making the connection
  284. unusable.
  285. :meth:`recv_streaming` raises the same exceptions as :meth:`recv`.
  286. Args:
  287. decode: Set this flag to override the default behavior of returning
  288. :class:`str` or :class:`bytes`. See below for details.
  289. Returns:
  290. An iterator of strings (:class:`str`) for a Text_ frame or
  291. bytestrings (:class:`bytes`) for a Binary_ frame.
  292. .. _Text: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  293. .. _Binary: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  294. You may override this behavior with the ``decode`` argument:
  295. * Set ``decode=False`` to disable UTF-8 decoding of Text_ frames and
  296. yield bytestrings (:class:`bytes`). This improves performance
  297. when decoding isn't needed.
  298. * Set ``decode=True`` to force UTF-8 decoding of Binary_ frames and
  299. yield strings (:class:`str`). This may be useful for servers that
  300. send binary frames instead of text frames.
  301. Raises:
  302. ConnectionClosed: When the connection is closed.
  303. ConcurrencyError: If two threads call :meth:`recv` or
  304. :meth:`recv_streaming` concurrently.
  305. """
  306. self.maybe_raise_legacy_warning()
  307. try:
  308. yield from self.recv_messages.get_iter(decode)
  309. return
  310. except EOFError:
  311. pass
  312. # fallthrough
  313. except ConcurrencyError:
  314. raise ConcurrencyError(
  315. "cannot call recv_streaming while another thread "
  316. "is already running recv or recv_streaming"
  317. ) from None
  318. except UnicodeDecodeError as exc:
  319. with self.send_context():
  320. self.protocol.fail(
  321. CloseCode.INVALID_DATA,
  322. f"{exc.reason} at position {exc.start}",
  323. )
  324. # fallthrough
  325. # Wait for the protocol state to be CLOSED before accessing close_exc.
  326. self.recv_events_thread.join()
  327. raise self.protocol.close_exc from self.recv_exc
  328. def send(
  329. self,
  330. message: DataLike | Iterable[DataLike],
  331. *,
  332. text: bool | None = None,
  333. ) -> None:
  334. """
  335. Send a message.
  336. A string (:class:`str`) is sent as a Text_ frame. A bytestring or
  337. bytes-like object (:class:`bytes`, :class:`bytearray`, or
  338. :class:`memoryview`) is sent as a Binary_ frame.
  339. .. _Text: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  340. .. _Binary: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  341. You may override this behavior with the ``text`` argument:
  342. * Set ``text=True`` to send an UTF-8 bytestring or bytes-like object
  343. (:class:`bytes`, :class:`bytearray`, or :class:`memoryview`) in a
  344. Text_ frame. This improves performance when the message is already
  345. UTF-8 encoded, for example if the message contains JSON and you're
  346. using a JSON library that produces a bytestring.
  347. * Set ``text=False`` to send a string (:class:`str`) in a Binary_
  348. frame. This may be useful for servers that expect binary frames
  349. instead of text frames.
  350. :meth:`send` also accepts an iterable of strings, bytestrings, or
  351. bytes-like objects to enable fragmentation_. Each item is treated as a
  352. message fragment and sent in its own frame. All items must be of the
  353. same type, or else :meth:`send` will raise a :exc:`TypeError` and the
  354. connection will be closed.
  355. .. _fragmentation: https://datatracker.ietf.org/doc/html/rfc6455#section-5.4
  356. :meth:`send` rejects dict-like objects because this is often an error.
  357. (If you really want to send the keys of a dict-like object as fragments,
  358. call its :meth:`~dict.keys` method and pass the result to :meth:`send`.)
  359. When the connection is closed, :meth:`send` raises
  360. :exc:`~websockets.exceptions.ConnectionClosed`. Specifically, it
  361. raises :exc:`~websockets.exceptions.ConnectionClosedOK` after a normal
  362. connection closure and
  363. :exc:`~websockets.exceptions.ConnectionClosedError` after a protocol
  364. error or a network failure.
  365. Args:
  366. message: Message to send.
  367. text: Force sending in a Text_ or Binary_ frame.
  368. Raises:
  369. ConnectionClosed: When the connection is closed.
  370. ConcurrencyError: If the connection is sending a fragmented message.
  371. TypeError: If ``message`` doesn't have a supported type.
  372. """
  373. self.maybe_raise_legacy_warning()
  374. # Unfragmented message — this case must be handled first because
  375. # strings and bytes-like objects are iterable.
  376. if isinstance(message, str):
  377. with self.send_context():
  378. if self.send_in_progress:
  379. raise ConcurrencyError(
  380. "cannot call send while another thread is already running send"
  381. )
  382. if text is False:
  383. self.protocol.send_binary(message.encode())
  384. else:
  385. self.protocol.send_text(message.encode())
  386. elif isinstance(message, BytesLike):
  387. with self.send_context():
  388. if self.send_in_progress:
  389. raise ConcurrencyError(
  390. "cannot call send while another thread is already running send"
  391. )
  392. if text is True:
  393. self.protocol.send_text(message)
  394. else:
  395. self.protocol.send_binary(message)
  396. # Catch a common mistake — passing a dict to send().
  397. elif isinstance(message, Mapping):
  398. raise TypeError("data is a dict-like object")
  399. # Fragmented message — regular iterator.
  400. elif isinstance(message, Iterable):
  401. chunks = iter(message)
  402. try:
  403. chunk = next(chunks)
  404. except StopIteration:
  405. return
  406. try:
  407. # First fragment.
  408. if isinstance(chunk, str):
  409. with self.send_context():
  410. if self.send_in_progress:
  411. raise ConcurrencyError(
  412. "cannot call send while another thread "
  413. "is already running send"
  414. )
  415. self.send_in_progress = True
  416. if text is False:
  417. self.protocol.send_binary(chunk.encode(), fin=False)
  418. else:
  419. self.protocol.send_text(chunk.encode(), fin=False)
  420. encode = True
  421. elif isinstance(chunk, BytesLike):
  422. with self.send_context():
  423. if self.send_in_progress:
  424. raise ConcurrencyError(
  425. "cannot call send while another thread "
  426. "is already running send"
  427. )
  428. self.send_in_progress = True
  429. if text is True:
  430. self.protocol.send_text(chunk, fin=False)
  431. else:
  432. self.protocol.send_binary(chunk, fin=False)
  433. encode = False
  434. else:
  435. raise TypeError("iterable must contain bytes or str")
  436. # Other fragments
  437. for chunk in chunks:
  438. if isinstance(chunk, str) and encode:
  439. with self.send_context():
  440. assert self.send_in_progress
  441. self.protocol.send_continuation(chunk.encode(), fin=False)
  442. elif isinstance(chunk, BytesLike) and not encode:
  443. with self.send_context():
  444. assert self.send_in_progress
  445. self.protocol.send_continuation(chunk, fin=False)
  446. else:
  447. raise TypeError("iterable must contain uniform types")
  448. # Final fragment.
  449. with self.send_context():
  450. self.protocol.send_continuation(b"", fin=True)
  451. self.send_in_progress = False
  452. except ConcurrencyError:
  453. # We didn't start sending a fragmented message.
  454. # The connection is still usable.
  455. raise
  456. except Exception:
  457. # We're half-way through a fragmented message and we can't
  458. # complete it. This makes the connection unusable.
  459. with self.send_context():
  460. self.protocol.fail(
  461. CloseCode.INTERNAL_ERROR,
  462. "error in fragmented message",
  463. )
  464. raise
  465. else:
  466. raise TypeError("data must be str, bytes, or iterable")
  467. def close(
  468. self,
  469. code: CloseCode | int = CloseCode.NORMAL_CLOSURE,
  470. reason: str = "",
  471. ) -> None:
  472. """
  473. Perform the closing handshake.
  474. :meth:`close` waits for the other end to complete the handshake and
  475. for the TCP connection to terminate.
  476. :meth:`close` is idempotent: it doesn't do anything once the
  477. connection is closed.
  478. Args:
  479. code: WebSocket close code.
  480. reason: WebSocket close reason.
  481. """
  482. self.maybe_raise_legacy_warning()
  483. try:
  484. # The context manager takes care of waiting for the TCP connection
  485. # to terminate after calling a method that sends a close frame.
  486. with self.send_context():
  487. if self.send_in_progress:
  488. self.protocol.fail(
  489. CloseCode.INTERNAL_ERROR,
  490. "close during fragmented message",
  491. )
  492. else:
  493. self.protocol.send_close(code, reason)
  494. except ConnectionClosed:
  495. # Ignore ConnectionClosed exceptions raised from send_context().
  496. # They mean that the connection is closed, which was the goal.
  497. pass
  498. def ping(
  499. self,
  500. data: DataLike | None = None,
  501. *,
  502. ack_on_close: bool = False,
  503. ) -> threading.Event:
  504. """
  505. Send a Ping_.
  506. .. _Ping: https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.2
  507. A ping may serve as a keepalive or as a check that the remote endpoint
  508. received all messages up to this point
  509. Args:
  510. data: Payload of the ping. A :class:`str` will be encoded to UTF-8.
  511. If ``data`` is :obj:`None`, the payload is four random bytes.
  512. ack_on_close: when this option is :obj:`True`, the event will also
  513. be set when the connection is closed. While this avoids getting
  514. stuck waiting for a pong that will never arrive, it requires
  515. checking that the state of the connection is still ``OPEN`` to
  516. confirm that a pong was received, rather than the connection
  517. being closed.
  518. Returns:
  519. An event that will be set when the corresponding pong is received.
  520. You can ignore it if you don't intend to wait.
  521. ::
  522. pong_received = ws.ping()
  523. # only if you want to wait for the corresponding pong
  524. pong_received.wait()
  525. Raises:
  526. ConnectionClosed: When the connection is closed.
  527. ConcurrencyError: If another ping was sent with the same data and
  528. the corresponding pong wasn't received yet.
  529. """
  530. self.maybe_raise_legacy_warning()
  531. if isinstance(data, BytesLike):
  532. data = bytes(data)
  533. elif isinstance(data, str):
  534. data = data.encode()
  535. elif data is not None:
  536. raise TypeError("data must be str or bytes-like")
  537. with self.send_context():
  538. # Protect against duplicates if a payload is explicitly set.
  539. if data in self.pending_pings:
  540. raise ConcurrencyError("already waiting for a pong with the same data")
  541. # Generate a unique random payload otherwise.
  542. while data is None or data in self.pending_pings:
  543. data = struct.pack("!I", random.getrandbits(32))
  544. pong_received = threading.Event()
  545. ping_timestamp = time.monotonic()
  546. self.pending_pings[data] = (pong_received, ping_timestamp, ack_on_close)
  547. self.protocol.send_ping(data)
  548. return pong_received
  549. def pong(self, data: DataLike = b"") -> None:
  550. """
  551. Send a Pong_.
  552. .. _Pong: https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.3
  553. An unsolicited pong may serve as a unidirectional heartbeat.
  554. Args:
  555. data: Payload of the pong. A :class:`str` will be encoded to UTF-8.
  556. Raises:
  557. ConnectionClosed: When the connection is closed.
  558. """
  559. self.maybe_raise_legacy_warning()
  560. if isinstance(data, BytesLike):
  561. data = bytes(data)
  562. elif isinstance(data, str):
  563. data = data.encode()
  564. else:
  565. raise TypeError("data must be str or bytes-like")
  566. with self.send_context():
  567. self.protocol.send_pong(data)
  568. # Private methods
  569. def maybe_raise_legacy_warning(self) -> None:
  570. pass # see override in ClientConnection
  571. def process_event(self, event: Event) -> None:
  572. """
  573. Process one incoming event.
  574. This method is overridden in subclasses to handle the handshake.
  575. """
  576. assert isinstance(event, Frame)
  577. if event.opcode in DATA_OPCODES:
  578. self.recv_messages.put(event)
  579. if event.opcode is PONG:
  580. self.acknowledge_pings(bytes(event.data))
  581. def acknowledge_pings(self, data: bytes) -> None:
  582. """
  583. Acknowledge pings when receiving a pong.
  584. """
  585. with self.protocol_mutex:
  586. # Ignore unsolicited pong.
  587. if data not in self.pending_pings:
  588. return
  589. pong_timestamp = time.monotonic()
  590. # Sending a pong for only the most recent ping is legal.
  591. # Acknowledge all previous pings too in that case.
  592. ping_id = None
  593. ping_ids = []
  594. for ping_id, (
  595. pong_received,
  596. ping_timestamp,
  597. _ack_on_close,
  598. ) in self.pending_pings.items():
  599. ping_ids.append(ping_id)
  600. pong_received.set()
  601. if ping_id == data:
  602. self.latency = pong_timestamp - ping_timestamp
  603. break
  604. else:
  605. raise AssertionError("solicited pong not found in pings")
  606. # Remove acknowledged pings from self.pending_pings.
  607. for ping_id in ping_ids:
  608. del self.pending_pings[ping_id]
  609. def terminate_pending_pings(self) -> None:
  610. """
  611. Acknowledge pending pings when the connection is closed.
  612. """
  613. assert self.protocol_mutex.locked()
  614. assert self.protocol.state is CLOSED
  615. for pong_received, _ping_timestamp, ack_on_close in self.pending_pings.values():
  616. if ack_on_close:
  617. pong_received.set()
  618. self.pending_pings.clear()
  619. def keepalive(self) -> None:
  620. """
  621. Send a Ping frame and wait for a Pong frame at regular intervals.
  622. """
  623. assert self.ping_interval is not None
  624. try:
  625. while True:
  626. # If self.ping_timeout > self.latency > self.ping_interval,
  627. # pings will be sent immediately after receiving pongs.
  628. # The period will be longer than self.ping_interval.
  629. self.recv_events_thread.join(self.ping_interval - self.latency)
  630. if not self.recv_events_thread.is_alive():
  631. break
  632. try:
  633. pong_received = self.ping(ack_on_close=True)
  634. except ConnectionClosed:
  635. break
  636. if self.debug:
  637. self.logger.debug("% sent keepalive ping")
  638. if self.ping_timeout is not None:
  639. if pong_received.wait(self.ping_timeout):
  640. if self.debug:
  641. self.logger.debug("% received keepalive pong")
  642. else:
  643. if self.debug:
  644. self.logger.debug("- timed out waiting for keepalive pong")
  645. with self.send_context():
  646. self.protocol.fail(
  647. CloseCode.INTERNAL_ERROR,
  648. "keepalive ping timeout",
  649. )
  650. break
  651. except Exception:
  652. self.logger.error("keepalive ping failed", exc_info=True)
  653. def start_keepalive(self) -> None:
  654. """
  655. Run :meth:`keepalive` in a thread, unless keepalive is disabled.
  656. """
  657. if self.ping_interval is not None:
  658. # This thread is marked as daemon like self.recv_events_thread.
  659. self.keepalive_thread = threading.Thread(
  660. target=self.keepalive,
  661. daemon=True,
  662. )
  663. self.keepalive_thread.start()
  664. def recv_events(self) -> None:
  665. """
  666. Read incoming data from the socket and process events.
  667. Run this method in a thread as long as the connection is alive.
  668. ``recv_events()`` exits immediately when ``self.socket`` is closed.
  669. """
  670. # When the opening handshake fails, we cannot trust rules in RFC 6455
  671. # for closing TCP connections will be followed. The HTTP server could
  672. # keep the connection alive after sending the response. We attempt to
  673. # close the connection immediately. Unfortunately, this is unreliable
  674. # on macOS; recv() may block until close_timeout elapses:
  675. # https://github.com/python-websockets/websockets/issues/1596
  676. # https://github.com/python/cpython/issues/154224
  677. # In that case, break out of the loop to prevent recv() from blocking
  678. # until close_timeout elapses.
  679. close_expected_while_connecting = False
  680. try:
  681. while not close_expected_while_connecting:
  682. try:
  683. # If the assembler buffer is full, block until it drains.
  684. with self.recv_flow_control:
  685. pass
  686. if self.close_deadline is not None:
  687. self.socket.settimeout(self.close_deadline.timeout())
  688. data = self.socket.recv(self.recv_bufsize)
  689. except Exception as exc:
  690. if self.debug:
  691. self.logger.debug(
  692. "! error while receiving data",
  693. exc_info=True,
  694. )
  695. # When the closing handshake is initiated by our side,
  696. # recv() may block until send_context() closes the socket.
  697. # In that case, send_context() already set recv_exc.
  698. # Calling set_recv_exc() avoids overwriting it.
  699. with self.protocol_mutex:
  700. self.set_recv_exc(exc)
  701. break
  702. if data == b"":
  703. break
  704. # Acquire the connection lock.
  705. with self.protocol_mutex:
  706. # Feed incoming data to the protocol.
  707. self.protocol.receive_data(data)
  708. # This isn't expected to raise an exception.
  709. events = self.protocol.events_received()
  710. # Write outgoing data to the socket.
  711. try:
  712. self.send_data()
  713. except Exception as exc:
  714. if self.debug:
  715. self.logger.debug(
  716. "! error while sending data",
  717. exc_info=True,
  718. )
  719. # Similarly to the above, avoid overriding an exception
  720. # set by send_context(), in case of a race condition
  721. # i.e. send_context() closes the socket after recv()
  722. # returns above but before send_data() calls send().
  723. self.set_recv_exc(exc)
  724. break
  725. # If needed, set the close deadline based on the close timeout.
  726. if self.protocol.close_expected():
  727. if self.close_deadline is None:
  728. self.close_deadline = Deadline(self.close_timeout)
  729. if self.protocol.state is CONNECTING:
  730. close_expected_while_connecting = True
  731. # Unlock conn_mutex before processing events. Else, the
  732. # application can't send messages in response to events.
  733. # If self.send_data raised an exception, then events are lost.
  734. # Given that automatic responses write small amounts of data,
  735. # this should be uncommon, so we don't handle the edge case.
  736. for event in events:
  737. # This isn't expected to raise an exception.
  738. self.process_event(event)
  739. # Breaking out of the while not close_expected_while_connecting: ...
  740. # loop means that we believe that the socket doesn't work anymore.
  741. with self.protocol_mutex:
  742. # Feed the end of the data stream to the protocol.
  743. self.protocol.receive_eof()
  744. # This isn't expected to raise an exception.
  745. events = self.protocol.events_received()
  746. # There is no error handling because send_data() can only write
  747. # the end of the data stream and it handles errors by itself.
  748. self.send_data()
  749. # This code path is triggered when receiving an HTTP response
  750. # without a Content-Length header. This is the only case where
  751. # reading until EOF generates an event; all other events have
  752. # a known length. Ignore for coverage measurement because tests
  753. # are in test_client.py rather than test_connection.py.
  754. for event in events: # pragma: no cover
  755. # This isn't expected to raise an exception.
  756. self.process_event(event)
  757. except Exception as exc:
  758. # This branch should never run. It's a safety net in case of bugs.
  759. self.logger.error("unexpected internal error", exc_info=True)
  760. with self.protocol_mutex:
  761. self.set_recv_exc(exc)
  762. finally:
  763. # This isn't expected to raise an exception.
  764. self.close_socket()
  765. @contextlib.contextmanager
  766. def send_context(
  767. self,
  768. *,
  769. expected_state: State = OPEN, # CONNECTING during the opening handshake
  770. ) -> Iterator[None]:
  771. """
  772. Create a context for writing to the connection from user code.
  773. On entry, :meth:`send_context` acquires the connection lock and checks
  774. that the connection is open; on exit, it writes outgoing data to the
  775. socket and releases the connection lock::
  776. with self.send_context():
  777. self.protocol.send_text(message.encode())
  778. When the connection isn't open on entry, when the connection is expected
  779. to close on exit, or when an unexpected error happens, terminating the
  780. connection, :meth:`send_context` waits until the connection is closed
  781. then raises :exc:`~websockets.exceptions.ConnectionClosed`.
  782. """
  783. # Should we wait until the connection is closed?
  784. wait_for_close = False
  785. # Should we close the socket and raise ConnectionClosed?
  786. raise_close_exc = False
  787. # What exception should we chain ConnectionClosed to?
  788. original_exc: BaseException | None = None
  789. # Acquire the protocol lock.
  790. with self.protocol_mutex:
  791. if self.protocol.state is expected_state:
  792. # Let the caller interact with the protocol.
  793. try:
  794. yield
  795. except (ProtocolError, ConcurrencyError):
  796. # The protocol state wasn't changed. Exit immediately.
  797. raise
  798. except Exception as exc:
  799. self.logger.error("unexpected internal error", exc_info=True)
  800. # This branch should never run. It's a safety net in case of
  801. # bugs. Since we don't know what happened, we will close the
  802. # connection and raise the exception to the caller.
  803. wait_for_close = False
  804. raise_close_exc = True
  805. original_exc = exc
  806. else:
  807. # Check if the connection is expected to close soon.
  808. if self.protocol.close_expected():
  809. wait_for_close = True
  810. # Set the close deadline based on the close timeout.
  811. # Since we tested earlier that protocol.state is OPEN
  812. # (or CONNECTING) and we didn't release protocol_mutex,
  813. # self.close_deadline is still None.
  814. assert self.close_deadline is None
  815. self.close_deadline = Deadline(self.close_timeout)
  816. # Write outgoing data to the socket.
  817. try:
  818. self.send_data()
  819. except Exception as exc:
  820. if self.debug:
  821. self.logger.debug(
  822. "! error while sending data",
  823. exc_info=True,
  824. )
  825. # While the only expected exception here is OSError,
  826. # other exceptions would be treated identically.
  827. wait_for_close = False
  828. raise_close_exc = True
  829. original_exc = exc
  830. else: # self.protocol.state is not expected_state
  831. # Minor layering violation: we assume that the connection
  832. # will be closing soon if it isn't in the expected state.
  833. wait_for_close = True
  834. # Calculate close_deadline if it wasn't set yet.
  835. if self.close_deadline is None:
  836. self.close_deadline = Deadline(self.close_timeout)
  837. raise_close_exc = True
  838. # To avoid a deadlock, release the connection lock by exiting the
  839. # context manager before waiting for recv_events() to terminate.
  840. # If the connection is expected to close soon and the close timeout
  841. # elapses, close the socket to terminate the connection.
  842. if wait_for_close:
  843. # Thread.join() returns immediately if timeout is negative.
  844. assert self.close_deadline is not None
  845. timeout = self.close_deadline.timeout(raise_if_elapsed=False)
  846. self.recv_events_thread.join(timeout)
  847. if self.recv_events_thread.is_alive():
  848. # There's no risk of overwriting another error because
  849. # original_exc is never set when wait_for_close is True.
  850. assert original_exc is None
  851. original_exc = TimeoutError("timed out while closing connection")
  852. # Set recv_exc before closing the socket in order to get
  853. # proper exception reporting.
  854. raise_close_exc = True
  855. with self.protocol_mutex:
  856. self.set_recv_exc(original_exc)
  857. # If an error occurred, close the socket to terminate the connection and
  858. # raise an exception.
  859. if raise_close_exc:
  860. self.close_socket()
  861. # Wait for the protocol state to be CLOSED before accessing close_exc.
  862. self.recv_events_thread.join()
  863. raise self.protocol.close_exc from original_exc
  864. def send_data(self) -> None:
  865. """
  866. Send outgoing data.
  867. This method requires holding protocol_mutex.
  868. """
  869. assert self.protocol_mutex.locked()
  870. for data in self.protocol.data_to_send():
  871. if data:
  872. if self.close_deadline is not None:
  873. self.socket.settimeout(self.close_deadline.timeout())
  874. self.socket.sendall(data)
  875. else:
  876. try:
  877. self.socket.shutdown(socket.SHUT_WR)
  878. except OSError: # socket already closed
  879. pass
  880. def set_recv_exc(self, exc: BaseException | None) -> None:
  881. """
  882. Set recv_exc, if not set yet.
  883. This method requires holding protocol_mutex and must be called only from
  884. the thread running recv_events().
  885. """
  886. assert self.protocol_mutex.locked()
  887. if self.recv_exc is None:
  888. self.recv_exc = exc
  889. def close_socket(self) -> None:
  890. """
  891. Shutdown and close socket. Close message assembler.
  892. Calling close_socket() guarantees that recv_events() terminates. Indeed,
  893. recv_events() may block only on socket.recv() or on recv_messages.put().
  894. """
  895. # shutdown() is required to interrupt recv() on Linux.
  896. try:
  897. self.socket.shutdown(socket.SHUT_RDWR)
  898. except OSError: # socket already closed
  899. pass
  900. try:
  901. self.socket.close()
  902. except OSError: # socket already closed # pragma: no cover
  903. # OSError: [Errno 9] Bad file descriptor
  904. # (only observed on free-threaded Python)
  905. pass
  906. # Calling protocol.receive_eof() is safe because it's idempotent.
  907. # This guarantees that the protocol state becomes CLOSED.
  908. with self.protocol_mutex:
  909. self.protocol.receive_eof()
  910. assert self.protocol.state is CLOSED
  911. # Abort recv() with a ConnectionClosed exception.
  912. self.recv_messages.close()
  913. # Acknowledge pings sent with the ack_on_close option.
  914. self.terminate_pending_pings()
  915. # broadcast() is defined in the connection module even though it's primarily
  916. # used by servers and documented in the server module because it works with
  917. # client connections too and because it's easier to test together with the
  918. # Connection class.
  919. def broadcast(
  920. connections: Iterable[Connection],
  921. message: DataLike,
  922. *,
  923. raise_exceptions: bool = False,
  924. text: bool | None = None,
  925. **kwargs: Any,
  926. ) -> None:
  927. """
  928. Broadcast a message to several WebSocket connections.
  929. A string (:class:`str`) is sent as a Text_ frame. A bytestring or bytes-like
  930. object (:class:`bytes`, :class:`bytearray`, or :class:`memoryview`) is sent
  931. as a Binary_ frame.
  932. .. _Text: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  933. .. _Binary: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6
  934. You may override this behavior with the ``text`` argument:
  935. * Set ``text=True`` to send an UTF-8 bytestring or bytes-like object
  936. (:class:`bytes`, :class:`bytearray`, or :class:`memoryview`) in a
  937. Text_ frame. This improves performance when the message is already
  938. UTF-8 encoded, for example if the message contains JSON and you're
  939. using a JSON library that produces a bytestring.
  940. * Set ``text=False`` to send a string (:class:`str`) in a Binary_
  941. frame. This may be useful for servers that expect binary frames
  942. instead of text frames.
  943. :func:`broadcast` relies on :class:`concurrent.futures.ThreadPoolExecutor`
  944. to send the messages. Make sure the thread pool is large enough relative to
  945. the number of clients, so that slow or stuck connections don't clog it. If
  946. that's an issue, then you should be using an asynchronous implementation.
  947. You can configure the thread pool by passing additional keyword arguments to
  948. :func:`broadcast`, such as ``max_workers``.
  949. Unlike :meth:`~websockets.asyncio.connection.Connection.send`,
  950. :func:`broadcast` doesn't support sending fragmented messages. Indeed,
  951. fragmentation is useful for sending large messages without buffering them in
  952. memory, while :func:`broadcast` buffers one copy per connection as fast as
  953. possible.
  954. :func:`broadcast` skips connections that aren't open in order to avoid
  955. errors on connections where the closing handshake is in progress.
  956. :func:`broadcast` ignores failures to write the message on some connections.
  957. It continues writing to other connections. You may set ``raise_exceptions``
  958. to :obj:`True` to record failures and raise all exceptions in a :pep:`654`
  959. :exc:`ExceptionGroup`.
  960. While :func:`broadcast` makes more sense for servers, it works identically
  961. with clients, if you have a use case for opening connections to many servers
  962. and broadcasting a message to them.
  963. Args:
  964. websockets: WebSocket connections to which the message will be sent.
  965. message: Message to send.
  966. raise_exceptions: Whether to raise an exception in case of failures.
  967. text: Force sending in Text_ or Binary_ frames.
  968. Raises:
  969. TypeError: If ``message`` doesn't have a supported type.
  970. """
  971. if isinstance(message, str):
  972. send_method = "send_binary" if text is False else "send_text"
  973. message = message.encode()
  974. elif isinstance(message, BytesLike):
  975. send_method = "send_text" if text is True else "send_binary"
  976. else:
  977. raise TypeError("data must be str or bytes")
  978. if raise_exceptions:
  979. exceptions: list[Exception] = []
  980. def send_message(connection: Connection) -> None:
  981. exception: Exception
  982. with connection.protocol_mutex:
  983. if connection.protocol.state is not OPEN:
  984. return
  985. if connection.send_in_progress:
  986. if raise_exceptions:
  987. exception = ConcurrencyError("sending a fragmented message")
  988. exceptions.append(exception)
  989. else:
  990. connection.logger.warning(
  991. "skipped broadcast: sending a fragmented message",
  992. )
  993. return
  994. try:
  995. # Call connection.protocol.send_text or send_binary.
  996. # Either way, message is already converted to bytes.
  997. getattr(connection.protocol, send_method)(message)
  998. connection.send_data()
  999. except Exception as write_exception:
  1000. if raise_exceptions:
  1001. exception = RuntimeError("failed to write message")
  1002. exception.__cause__ = write_exception
  1003. exceptions.append(exception)
  1004. else:
  1005. connection.logger.warning(
  1006. "skipped broadcast: failed to write message: %s",
  1007. traceback.format_exception_only(write_exception)[0].strip(),
  1008. )
  1009. with concurrent.futures.ThreadPoolExecutor(**kwargs) as executor:
  1010. executor.map(send_message, connections)
  1011. if raise_exceptions and exceptions:
  1012. raise ExceptionGroup("skipped broadcast", exceptions)
  1013. # Pretend that broadcast is actually defined in the server module.
  1014. broadcast.__module__ = "websockets.sync.server"