From e8b23affe934a85f97f46e3c3b7e42223c700e9a Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Sun, 30 Jun 2024 11:46:09 +0800 Subject: [PATCH] fix systemd availability check when the command is running, resource properties are always available. --- lilac2/systemd.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/lilac2/systemd.py b/lilac2/systemd.py index 09c9db9..91257d6 100644 --- a/lilac2/systemd.py +++ b/lilac2/systemd.py @@ -49,17 +49,22 @@ def _check_availability() -> bool | dict[str, bool]: return False try: - ps: dict[str, Optional[int]] = { - 'CPUUsageNSec': None, - 'MemoryPeak': None, - } - _read_service_int_properties('lilac-check', ps) - - ret = {} - for k, v in ps.items(): - ret[k] = v is not None - - return ret + while True: + ps: dict[str, Optional[int]] = { + 'CPUUsageNSec': None, + 'MemoryPeak': None, + 'MainPID': None, + } + _read_service_int_properties('lilac-check', ps) + if ps['MainPID'] != 0: + time.sleep(0.01) + continue + + ret = {} + for k, v in ps.items(): + ret[k] = v is not None + + return ret finally: subprocess.run(['systemctl', '--user', 'stop', '--quiet', 'lilac-check'])