Skip to content

NTP fix infinite loop #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 14, 2023
Merged

NTP fix infinite loop #82

merged 4 commits into from
Jan 14, 2023

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