Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Connection.recv(timeout=0) in the sync implementation. #1573

Merged
merged 1 commit into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/project/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ notice.
Bug fixes
.........

* Fixed ``connection.recv(timeout=0)`` in the :mod:`threading` implementation.
If a message is already received, it is returned. Previously,
:exc:`TimeoutError` was raised incorrectly.

* Wrapped errors when reading the opening handshake request or response in
:exc:`~exceptions.InvalidMessage` so that :func:`~asyncio.client.connect`
raises :exc:`~exceptions.InvalidHandshake` or a subclass when the opening
Expand Down
13 changes: 10 additions & 3 deletions src/websockets/sync/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ def get_next_frame(self, timeout: float | None = None) -> Frame:
raise EOFError("stream of frames ended") from None
else:
try:
frame = self.frames.get(block=True, timeout=timeout)
# Check for a frame that's already received if timeout <= 0.
# SimpleQueue.get() doesn't support negative timeout values.
if timeout is not None and timeout <= 0:
frame = self.frames.get(block=False)
else:
frame = self.frames.get(block=True, timeout=timeout)
except queue.Empty:
raise TimeoutError(f"timed out in {timeout:.1f}s") from None
if frame is None:
Expand Down Expand Up @@ -143,7 +148,7 @@ def get(self, timeout: float | None = None, decode: bool | None = None) -> Data:
deadline = Deadline(timeout)

# First frame
frame = self.get_next_frame(deadline.timeout())
frame = self.get_next_frame(deadline.timeout(raise_if_elapsed=False))
with self.mutex:
self.maybe_resume()
assert frame.opcode is OP_TEXT or frame.opcode is OP_BINARY
Expand All @@ -154,7 +159,9 @@ def get(self, timeout: float | None = None, decode: bool | None = None) -> Data:
# Following frames, for fragmented messages
while not frame.fin:
try:
frame = self.get_next_frame(deadline.timeout())
frame = self.get_next_frame(
deadline.timeout(raise_if_elapsed=False)
)
except TimeoutError:
# Put frames already received back into the queue
# so that future calls to get() can return them.
Expand Down
6 changes: 6 additions & 0 deletions tests/sync/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ def test_get_timeout_after_first_frame(self):
message = self.assembler.get()
self.assertEqual(message, "café")

def test_get_if_received(self):
"""get returns a text message if it's already received."""
self.assembler.put(Frame(OP_TEXT, b"caf\xc3\xa9"))
message = self.assembler.get(timeout=0)
self.assertEqual(message, "café")

# Test get_iter

def test_get_iter_text_message_already_received(self):
Expand Down
Loading