connection.py 49 KB

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