Skip to content

Conversation

@BiffoBear
Copy link
Contributor

fixes #56 NTP.get_time calls self._sock.recv() from a while True: loop. This results in an infinite loop if no data is received. The UDP protocol can lose packets, so this is possible.

Added retries using exponential fallback to give slow networks a chance. If there is no response, a TimeoutError is raised.

        max_retries = 4
        for retry in range(max_retries):
            self._sock.sendto(self._pkt_buf_, (self._ntp_server, 123))
            end_time = time.monotonic() + 0.2 * 2**retry
            while time.monotonic() < end_time:
                data = self._sock.recv()

Copy link
Member

@anecdata anecdata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Tested on W5100S. Observed replies instantly, after retries, and also observed TimeoutError: No reply from NTP server after 4 attempts..

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Tested successfully on Feather TFT ESP32-S2

I got some successes and some failures that now report the new error instead of hanging forever.

@FoamyGuy FoamyGuy merged commit abdec8a into adafruit:main Jan 14, 2023
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jan 15, 2023
Updating https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k to 1.13.3 from 1.13.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_Wiznet5k#82 from BiffoBear/NTP_fix_infinite_loop
  > Merge pull request adafruit/Adafruit_CircuitPython_Wiznet5k#86 from BiffoBear/_GLOBAL_constants

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Updated download stats for the libraries
@BiffoBear BiffoBear deleted the NTP_fix_infinite_loop branch January 17, 2023 00:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Intermittent hangs in NTP.get_time()

3 participants