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

Unclear behaviour of recv(timeout=0) for Server (threading) #1552

Open
savejeff opened this issue Nov 19, 2024 · 3 comments
Open

Unclear behaviour of recv(timeout=0) for Server (threading) #1552

savejeff opened this issue Nov 19, 2024 · 3 comments
Labels

Comments

@savejeff
Copy link

savejeff commented Nov 19, 2024

I think the behavior of recv(timeout=0) for the Thread-based Server is unclear.
Due to the design of my application, I can't use the "for message in websocket" and have to resort to an update() function that I call frequently to check if messages were received:


def update(self) -> None:
	"""
	Check for received data with proper handling of timeouts and connection closure.
	"""
	while True:
		try:
			# Attempt to receive data with a timeout
			data_rx = self.ws_client.recv(timeout=0)
		except TimeoutError:
			# No data received within the timeout period
			break
		except websockets.exceptions.ConnectionClosed as e:
			# Handle connection closure
			Log(f"Connection closed: {e}")
			self._is_connected = False
			break
		except Exception as e:
			# Handle any other exceptions
			Log(f"Error occurred: {e}")
			self._is_connected = False
			break

		# If no data received, exit the loop
		if data_rx is None:
			break

		self.log(f"RX: {data_rx}")


I was expecting when I set timeout=0 that only when a message was received I get it otherwise TimeoutError is raised so it's not blocking.
the behavior I'm getting is that with very low values like timeout=0.001 (1ms) it works, but with 0 I don't receive any messages.

Have I understood timeout=0 incorrectly? In the documentation it says:
"Set timeout to 0 to check if a message was already received." see

@aaugustin
Copy link
Member

Probably

frame = self.get_next_frame(deadline.timeout())
should be changed to:

deadline.timeout(raise_if_elapsed=False)

Further changes may be needed if queue.SimpleQueue.get() chokes on negative timeouts — I think it does.

@aaugustin aaugustin added the bug label Nov 19, 2024
@aaugustin
Copy link
Member

(This is also an opportunity to reconsider raise_if_elapsed — is it a good idea at all?)

@aaugustin
Copy link
Member

(Maybe we need zero_if_elapsed?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants