Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

1073 řádky
41 KiB

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