Skip to content

Commit 136787e

Browse files
committed
Make SNTP connection failure (more) reliable.
There are two reliability issues in the SNTP code. Right now if we fail in the last step of `ntp_time_update` (when we do `Smtp_ReceiveTimeResponse`) because of a network error, we run into an infinite loop of retries. Even worse, the loop does not print anything (the system looks frozen). Also, although `sntp_update` is run in a loop in the examples, we do not reset the timeout before calling `sntp_update`, which leads subsequent calls to always fail. Address this by not calling again `Smtp_ReceiveTimeResponse` if we reach the timeout and reset-ing the timeout before calling again `sntp_update` in examples. Signed-off-by: Hugo Lefeuvre <[email protected]>
1 parent b0d8d00 commit 136787e

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

examples/01.SNTP/sntp.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ void __cheri_compartment("sntp_example") example()
1919
{
2020
Debug::log("Failed to update NTP time");
2121
Timeout oneSecond{MS_TO_TICKS(1000)};
22+
t = Timeout{MS_TO_TICKS(5000)};
2223
}
2324
Debug::log("Updating NTP took {} ticks", t.elapsed);
2425
t = UnlimitedTimeout;

examples/03.HTTPS/https.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void __cheri_compartment("https_example") example()
3232
{
3333
Debug::log("Failed to update NTP time");
3434
Timeout oneSecond{MS_TO_TICKS(1000)};
35+
t = Timeout{MS_TO_TICKS(5000)};
3536
}
3637
Debug::log("Updating NTP took {} ticks", t.elapsed);
3738
t = UnlimitedTimeout;

examples/04.MQTT/mqtt.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ void __cheri_compartment("mqtt_example") example()
9696
{
9797
Debug::log("Failed to update NTP time");
9898
Timeout oneSecond{MS_TO_TICKS(1000)};
99+
t = Timeout{MS_TO_TICKS(5000)};
99100
}
100101
Debug::log("Updating NTP took {} ticks", t.elapsed);
101102
t = UnlimitedTimeout;

lib/sntp/sntp.cc

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ namespace
410410
SntpStatus_t lastStatus = SntpSuccess;
411411
do
412412
{
413+
// `timeout` is updated through `context`.
413414
status = Sntp_ReceiveTimeResponse(&context, 0);
414415
if (status != lastStatus)
415416
{
@@ -423,8 +424,21 @@ namespace
423424
thread_sleep(&t);
424425
timeout->elapse(t.elapsed);
425426
}
426-
} while (status == SntpNoResponseReceived);
427-
Debug::log("Received new time fron NTP!");
427+
} while (status == SntpNoResponseReceived &&
428+
timeout->may_block());
429+
430+
if (status != SntpSuccess)
431+
{
432+
Debug::log("Failed to receive SNTP time response: {}",
433+
status);
434+
Timeout t{UnlimitedTimeout};
435+
network_socket_close(
436+
&t, MALLOC_CAPABILITY, udpContext.socket);
437+
timeout->elapse(t.elapsed);
438+
return ntp_error_to_errno(status);
439+
}
440+
441+
Debug::log("Received new time from NTP!");
428442
}
429443
else
430444
{

0 commit comments

Comments
 (0)