Skip to content

Commit

Permalink
fix stalled thread due to recv no data avail after exit.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgineer85 committed Nov 22, 2024
1 parent 6fa89c9 commit 2063441
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion wigglecam/services/backends/io/virtualio.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import select
import socket
import struct
import time
Expand Down Expand Up @@ -81,6 +82,14 @@ def _gpio_fun(self):
logger.info("_gpio_fun left")

def _trigger_fun(self):
def recv_timeout(sock: socket.socket, bytes_to_read: int, timeout_seconds: float = 1.0):
sock.setblocking(0)
ready = select.select([sock], [], [], timeout_seconds)
if ready[0]:
return sock.recv(bytes_to_read)

raise TimeoutError()

logger.debug("starting _trigger_fun to trigger when multicast message is received")

# Multicast receiver, reference https://gist.github.com/dksmiffs/96ddbfd11ad7349ab4889b2e79dc2b22
Expand All @@ -92,7 +101,11 @@ def _trigger_fun(self):
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

while not current_thread().stopped():
msg = sock.recv(1024)
try:
msg = recv_timeout(sock, 1024)
except TimeoutError:
# to allow trigger_fun to finish regular, use timeout
continue

if msg == b"triggerON":
self._on_trigger_in()
Expand Down

0 comments on commit 2063441

Please sign in to comment.