peterhinch / micropython-mqtt

A 'resilient' asynchronous MQTT driver. Recovers from WiFi and broker outages.
MIT License
549 stars 116 forks source link

For mqtt broker is error, the noblock sock alway catch 'EINPROGRESS' #65

Closed wangkangman closed 2 years ago

wangkangman commented 2 years ago

maybe set the socket as block to catch the error of connection, then set as noblock.

` async def _connect(self, clean):

during connecting, set as noblocking to advoid err 'EINPROGRESS'

    while True:
        try:
            self._sock = socket.socket()
            self._sock.settimeout(3.0)
            self._sock.connect(self._addr)
            break
        except OSError as e:
            print("sock.connect:",e)
            self._sock.close()
            await asyncio.sleep_ms(8000)

   self._sock.settimeout(0)  `
peterhinch commented 2 years ago

I see two problems with this.

If EINPROGRESS occurs, the existing code waits 20ms then continues on the assumption that connection is successful. Your patch implies that, in your network, delays of many seconds are required to achieve successful connection. No other user has reported this problem. It seems wrong: my approach would be to thoroughly investigate the LAN.

Secondly your solution blocks for up to 3 seconds: this defeats an object of mqtt_as which is to use nonblocking code.

wangkangman commented 2 years ago

during the script starting, if the WLAN is up, but sock.connect(self._addr) working fail. the existing code catch EINPROGRESS, and assumption the conneciton is successful(actually, it's fail), then run continue. it will meet some faltal error in following code.

I just wana to add error catch and connection retry. when the connection is finish, I will do self._sock.settimeout(0) to set it as nonblocking(I'm not sure if this will work).

peterhinch commented 2 years ago

Sorry but I'm not prepared to merge these changes unless other users report this problem. At the moment I believe it is specific to your site and represents a fault elsewhere, possibly in your AP.

Your options are to maintain your own fork with these changes or to locate and fix the actual cause.