updated: close lock file on exit (#31285)

* with open

* those too

* just 1 file

* move this to another pr
old-commit-hash: fd07fc3ba4
pull/32199/head
Justin Newberry 1 year ago committed by GitHub
parent 9205780c13
commit a6ed630fb5
  1. 160
      selfdrive/updated.py

@ -413,96 +413,96 @@ def main() -> None:
cloudlog.warning("updates are disabled by the DisableUpdates param") cloudlog.warning("updates are disabled by the DisableUpdates param")
exit(0) exit(0)
ov_lock_fd = open(LOCK_FILE, 'w') with open(LOCK_FILE, 'w') as ov_lock_fd:
try: try:
fcntl.flock(ov_lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB) fcntl.flock(ov_lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except OSError as e: except OSError as e:
raise RuntimeError("couldn't get overlay lock; is another instance running?") from e raise RuntimeError("couldn't get overlay lock; is another instance running?") from e
# Set low io priority # Set low io priority
proc = psutil.Process() proc = psutil.Process()
if psutil.LINUX: if psutil.LINUX:
proc.ionice(psutil.IOPRIO_CLASS_BE, value=7) proc.ionice(psutil.IOPRIO_CLASS_BE, value=7)
# Check if we just performed an update # Check if we just performed an update
if Path(os.path.join(STAGING_ROOT, "old_openpilot")).is_dir(): if Path(os.path.join(STAGING_ROOT, "old_openpilot")).is_dir():
cloudlog.event("update installed") cloudlog.event("update installed")
if not params.get("InstallDate"): if not params.get("InstallDate"):
t = datetime.datetime.utcnow().isoformat() t = datetime.datetime.utcnow().isoformat()
params.put("InstallDate", t.encode('utf8')) params.put("InstallDate", t.encode('utf8'))
updater = Updater() updater = Updater()
update_failed_count = 0 # TODO: Load from param? update_failed_count = 0 # TODO: Load from param?
wait_helper = WaitTimeHelper() wait_helper = WaitTimeHelper()
# invalidate old finalized update # invalidate old finalized update
set_consistent_flag(False) set_consistent_flag(False)
# set initial state # set initial state
params.put("UpdaterState", "idle") params.put("UpdaterState", "idle")
# Run the update loop # Run the update loop
first_run = True first_run = True
while True: while True:
wait_helper.ready_event.clear() wait_helper.ready_event.clear()
# Attempt an update # Attempt an update
exception = None exception = None
try: try:
# TODO: reuse overlay from previous updated instance if it looks clean # TODO: reuse overlay from previous updated instance if it looks clean
init_overlay() init_overlay()
# ensure we have some params written soon after startup # ensure we have some params written soon after startup
updater.set_params(False, update_failed_count, exception) updater.set_params(False, update_failed_count, exception)
if not system_time_valid() or first_run: if not system_time_valid() or first_run:
first_run = False first_run = False
wait_helper.sleep(60) wait_helper.sleep(60)
continue continue
update_failed_count += 1 update_failed_count += 1
# check for update # check for update
params.put("UpdaterState", "checking...") params.put("UpdaterState", "checking...")
updater.check_for_update() updater.check_for_update()
# download update # download update
last_fetch = read_time_from_param(params, "UpdaterLastFetchTime") last_fetch = read_time_from_param(params, "UpdaterLastFetchTime")
timed_out = last_fetch is None or (datetime.datetime.utcnow() - last_fetch > datetime.timedelta(days=3)) timed_out = last_fetch is None or (datetime.datetime.utcnow() - last_fetch > datetime.timedelta(days=3))
user_requested_fetch = wait_helper.user_request == UserRequest.FETCH user_requested_fetch = wait_helper.user_request == UserRequest.FETCH
if params.get_bool("NetworkMetered") and not timed_out and not user_requested_fetch: if params.get_bool("NetworkMetered") and not timed_out and not user_requested_fetch:
cloudlog.info("skipping fetch, connection metered") cloudlog.info("skipping fetch, connection metered")
elif wait_helper.user_request == UserRequest.CHECK: elif wait_helper.user_request == UserRequest.CHECK:
cloudlog.info("skipping fetch, only checking") cloudlog.info("skipping fetch, only checking")
else: else:
updater.fetch_update() updater.fetch_update()
write_time_to_param(params, "UpdaterLastFetchTime") write_time_to_param(params, "UpdaterLastFetchTime")
update_failed_count = 0 update_failed_count = 0
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
cloudlog.event( cloudlog.event(
"update process failed", "update process failed",
cmd=e.cmd, cmd=e.cmd,
output=e.output, output=e.output,
returncode=e.returncode returncode=e.returncode
) )
exception = f"command failed: {e.cmd}\n{e.output}" exception = f"command failed: {e.cmd}\n{e.output}"
OVERLAY_INIT.unlink(missing_ok=True) OVERLAY_INIT.unlink(missing_ok=True)
except Exception as e: except Exception as e:
cloudlog.exception("uncaught updated exception, shouldn't happen") cloudlog.exception("uncaught updated exception, shouldn't happen")
exception = str(e) exception = str(e)
OVERLAY_INIT.unlink(missing_ok=True) OVERLAY_INIT.unlink(missing_ok=True)
try: try:
params.put("UpdaterState", "idle") params.put("UpdaterState", "idle")
update_successful = (update_failed_count == 0) update_successful = (update_failed_count == 0)
updater.set_params(update_successful, update_failed_count, exception) updater.set_params(update_successful, update_failed_count, exception)
except Exception: except Exception:
cloudlog.exception("uncaught updated exception while setting params, shouldn't happen") cloudlog.exception("uncaught updated exception while setting params, shouldn't happen")
# infrequent attempts if we successfully updated recently # infrequent attempts if we successfully updated recently
wait_helper.user_request = UserRequest.NONE wait_helper.user_request = UserRequest.NONE
wait_helper.sleep(5*60 if update_failed_count > 0 else 1.5*60*60) wait_helper.sleep(5*60 if update_failed_count > 0 else 1.5*60*60)
if __name__ == "__main__": if __name__ == "__main__":

Loading…
Cancel
Save