From eab42d9fe0b05260387a05ede4543a3604616f6e Mon Sep 17 00:00:00 2001 From: Neil Fuller Date: Tue, 5 Apr 2022 11:47:12 +0100 Subject: [PATCH] Document a section of code Document a section of code that prevents an NTP-related CTS test working if the device is not connected to Wifi. The reason the code exists took a little bit of research, so here it is. Bug: 213393821 Test: Comment-only change Change-Id: I0b5d9f298ef36e861f780a29fdf74f0226fa1567 --- core/java/android/util/NtpTrustedTime.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/java/android/util/NtpTrustedTime.java b/core/java/android/util/NtpTrustedTime.java index 01a037ae34950..4e7b3a51d7584 100644 --- a/core/java/android/util/NtpTrustedTime.java +++ b/core/java/android/util/NtpTrustedTime.java @@ -193,6 +193,16 @@ public class NtpTrustedTime implements TrustedTime { } final Network network = connectivityManager.getActiveNetwork(); final NetworkInfo ni = connectivityManager.getNetworkInfo(network); + + // This connectivity check is to avoid performing a DNS lookup for the time server on a + // unconnected network. There are races to obtain time in Android when connectivity + // changes, which means that forceRefresh() can be called by various components before + // the network is actually available. This led in the past to DNS lookup failures being + // cached (~2 seconds) thereby preventing the device successfully making an NTP request + // when connectivity had actually been established. + // A side effect of check is that tests that run a fake NTP server on the device itself + // will only be able to use it if the active network is connected, even though loopback + // addresses are actually reachable. if (ni == null || !ni.isConnected()) { if (LOGD) Log.d(TAG, "forceRefresh: no connectivity"); return false;