adafruit / Adafruit_CircuitPython_NTP

Network Time Protocol (NTP) Helper for CircuitPython
MIT License
9 stars 18 forks source link

NTP datetime calls hang as no socket timeout is specified #21

Closed mattura closed 2 years ago

mattura commented 2 years ago

My code frequently hangs during the ntp call (eg if the connection is lost) Passing a socket timeout through the function could resolve this or at least allow my code to continue after a short delay

eg:

def __init__(
        self,
        socketpool,
        *,
        server: str = "0.adafruit.pool.ntp.org",
        port: int = 123,
        tz_offset: int = 0,
        socket_timeout = 10
    ) -> None:
   ...
   self._socket_timeout = socket_timeout
   ...

def datetime(self) -> time.struct_time:
...
with self._pool.socket(self._pool.AF_INET, self._pool.SOCK_DGRAM) as sock:
                sock.settimeout(self._socket_timeout)
                sock.sendto(self._packet, (self._server, self._port))
                ...
regulatre commented 2 years ago

Created PR https://github.com/adafruit/Adafruit_CircuitPython_NTP/pull/22

tekktrik commented 2 years ago

Resolved via #22