Skip to content

Commit c4d6885

Browse files
authored
Replace time.time with time.monotonic to avoid clock drift issues (#4492)
Using time.time() can be inaccurate if the system clock gets updated in between calls.
1 parent 8a929f6 commit c4d6885

3 files changed

Lines changed: 9 additions & 9 deletions

File tree

archinstall/lib/installer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,10 @@ def _verify_service_stop(self, offline: bool, skip_ntp: bool, skip_wkd: bool) ->
189189
if not skip_ntp:
190190
info(tr('Waiting for time sync (timedatectl show) to complete.'))
191191

192-
started_wait = time.time()
192+
started_wait = time.monotonic()
193193
notified = False
194194
while True:
195-
if not notified and time.time() - started_wait > 5:
195+
if not notified and time.monotonic() - started_wait > 5:
196196
notified = True
197197
warn(tr('Time synchronization not completing, while you wait - check the docs for workarounds: https://archinstall.readthedocs.io/'))
198198

archinstall/lib/networking.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ def __enter__(self) -> Self:
4646
self.previous_handler = signal.signal(signal.SIGALRM, self.raise_timeout) # type: ignore[assignment]
4747
self.previous_timer = signal.alarm(self.timeout)
4848

49-
self.start_time = time.time()
49+
self.start_time = time.monotonic()
5050
return self
5151

5252
def __exit__(self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None) -> None:
5353
if self.start_time:
54-
time_delta = time.time() - self.start_time
54+
time_delta = time.monotonic() - self.start_time
5555
signal.alarm(0)
5656
self.time = time_delta
5757
if self.timeout > 0:
@@ -165,7 +165,7 @@ def build_icmp(payload: bytes) -> bytes:
165165

166166
def ping(hostname: str, timeout: int = 5) -> int:
167167
watchdog = select.epoll()
168-
started = time.time()
168+
started = time.monotonic()
169169
random_identifier = f'archinstall-{random.randint(1000, 9999)}'.encode()
170170

171171
# Create a raw socket (requires root, which should be fine on archiso)
@@ -180,15 +180,15 @@ def ping(hostname: str, timeout: int = 5) -> int:
180180

181181
# Gracefully wait for X amount of time
182182
# for a ICMP response or exit with no latency
183-
while latency == -1 and time.time() - started < timeout:
183+
while latency == -1 and time.monotonic() - started < timeout:
184184
try:
185185
for _fileno, _event in watchdog.poll(0.1):
186186
response, _ = icmp_socket.recvfrom(1024)
187187
icmp_type = struct.unpack('!B', response[20:21])[0]
188188

189189
# Check if it's an Echo Reply (ICMP type 0)
190190
if icmp_type == 0 and response[-len(random_identifier) :] == random_identifier:
191-
latency = round((time.time() - started) * 1000)
191+
latency = round((time.monotonic() - started) * 1000)
192192
break
193193
except OSError as e:
194194
debug(f'Error: {e}')

archinstall/lib/pacman/pacman.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ def run(args: str, default_cmd: str = 'pacman') -> SysCommand:
2929
if pacman_db_lock.exists():
3030
warn(tr('Pacman is already running, waiting maximum 10 minutes for it to terminate.'))
3131

32-
started = time.time()
32+
started = time.monotonic()
3333
while pacman_db_lock.exists():
3434
time.sleep(0.25)
3535

36-
if time.time() - started > (60 * 10):
36+
if time.monotonic() - started > (60 * 10):
3737
error(tr('Pre-existing pacman lock never exited. Please clean up any existing pacman sessions before using archinstall.'))
3838
sys.exit(1)
3939

0 commit comments

Comments
 (0)