diff --git a/infrastructure/src/bin/bat_official_sync.rs b/infrastructure/src/bin/bat_official_sync.rs index 47943eb..1be917d 100644 --- a/infrastructure/src/bin/bat_official_sync.rs +++ b/infrastructure/src/bin/bat_official_sync.rs @@ -3042,6 +3042,19 @@ fn read_daemon_status_file(path: &Path) -> anyhow::Result = Mutex::new(()); + +fn lock_daemon_status_file() -> std::sync::MutexGuard<'static, ()> { + DAEMON_STATUS_FILE_LOCK + .lock() + .unwrap_or_else(|poison| poison.into_inner()) +} + fn write_daemon_status_file(path: &Path, status: &DaemonStatusFile) -> anyhow::Result<()> { write_file_atomic( path, @@ -3054,6 +3067,7 @@ fn write_daemon_status_file(path: &Path, status: &DaemonStatusFile) -> anyhow::R } fn update_daemon_status(state_dir: &Path, update: DaemonStatusUpdate<'_>) -> anyhow::Result<()> { + let _guard = lock_daemon_status_file(); let status_path = daemon_status_path(state_dir); let mut status = read_daemon_status_file(&status_path)?.unwrap_or_else(|| DaemonStatusFile { version: DAEMON_STATUS_VERSION, @@ -3099,6 +3113,7 @@ fn update_daemon_status(state_dir: &Path, update: DaemonStatusUpdate<'_>) -> any } fn update_daemon_progress(state_dir: &Path, event: &OfficialUpdateProgress) -> anyhow::Result<()> { + let _guard = lock_daemon_status_file(); let status_path = daemon_status_path(state_dir); let Some(mut status) = read_daemon_status_file(&status_path)? else { return Ok(()); @@ -3128,6 +3143,7 @@ fn update_daemon_progress(state_dir: &Path, event: &OfficialUpdateProgress) -> a } fn update_daemon_state_only(state_dir: &Path, state: &str) -> anyhow::Result<()> { + let _guard = lock_daemon_status_file(); let status_path = daemon_status_path(state_dir); let Some(mut status) = read_daemon_status_file(&status_path)? else { return Ok(());