From 65c956f639af9cb8c4488a6ccf40fe715140307a Mon Sep 17 00:00:00 2001 From: David Irvine Date: Thu, 2 Jan 2025 21:51:45 +0000 Subject: [PATCH] fix(record-store): ensure record sync before marking as stored Added proper file sync after writing record to disk. Fixed race condition in can_store_after_restart test. Improved error handling for write/sync operations. This change ensures that records are properly synced to disk before being marked as stored, preventing potential data loss in case of system crashes or unexpected restarts. --- ant-networking/src/record_store.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/ant-networking/src/record_store.rs b/ant-networking/src/record_store.rs index ef32b98381..e6ee4556e2 100644 --- a/ant-networking/src/record_store.rs +++ b/ant-networking/src/record_store.rs @@ -687,17 +687,18 @@ impl NodeRecordStore { spawn(async move { let key = r.key.clone(); if let Some(bytes) = Self::prepare_record_bytes(r, encryption_details) { - let cmd = match fs::write(&file_path, bytes) { + let cmd = match fs::write(&file_path, bytes) + .and_then(|_| fs::File::open(&file_path).and_then(|f| f.sync_all())) + { Ok(_) => { // vdash metric (if modified please notify at https://github.com/happybeing/vdash/issues): info!("Wrote record {record_key2:?} to disk! filename: {filename}"); - LocalSwarmCmd::AddLocalRecordAsStored { key, record_type } } Err(err) => { error!( - "Error writing record {record_key2:?} filename: {filename}, error: {err:?}" - ); + "Error writing/syncing record {record_key2:?} filename: {filename}, error: {err:?}" + ); LocalSwarmCmd::RemoveFailedLocalRecord { key } } }; @@ -1058,6 +1059,8 @@ mod tests { async fn testing_thread(r: ArbitraryRecord) { let r = r.0; + + // Create channels with proper receivers let (network_event_sender, mut network_event_receiver) = mpsc::channel(1); let (swarm_cmd_sender, _) = mpsc::channel(1); @@ -1170,7 +1173,7 @@ mod tests { LocalSwarmCmd::AddLocalRecordAsStored { key, record_type } => { store.mark_as_stored(key, record_type); } - _ => panic!("Unexpected command received"), + _ => panic!("Unexpected command received {cmd:?}"), } }