Socket.recv loop stop randomly

Hi!
I’m not good at English, please understand. :slight_smile:

I have a problem with socket communication.

Here’s my code.
I’m using thread to recv data but thread stops randomly.
What’s wrong? Or is there are another way?
Help me.

class SocketConnection:
    def __init__(
        self,
        hass=None,
        entry=None,
        socket_ip=None,
        socket_port=None,
    ):
        self._hass = hass
        self._entry = entry
        self._ip = socket_ip
        self._port = socket_port
        self._connected = False
        self._socket = self.connect(self._ip, self._port)
        if self._socket:
            self._connected = True
                self._polling = threading.Thread(target=self.poll)
                self._polling.start()

    def read(self):
        if self._connected:
            try:
                return self._socket.recv(1)
            except:
                self._connected = False

    def connect(self, server, port):
        soc = socket.socket()
        soc.settimeout(10)
        try:
            soc.connect((server, int(port)))
        except Exception as e:
            return False
        soc.settimeout(None)
        return soc

    def poll(self):
        PACKET = []
        FINDING = "aa"
        PACKET_LENGTH = 21
        START_FLAG = False
        while True:
            try:
                row_data = self.read()
                hex_d = row_data.hex()
                if hex_d in FINDING:
                    START_FLAG = True
                if START_FLAG == True:
                    PACKET.append(hex_d)
                if len(PACKET) >= PACKET_LENGTH:
                    # do something
                    PACKET = []
                    START_FLAG = False
            except:
                print("poll error")
                pass