From ee71319497c9321ac74a65aa5578a8d04cad2114 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 | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/ant-networking/src/record_store.rs b/ant-networking/src/record_store.rs index 5cee127a61..26bf7aae89 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 } } }; @@ -1057,11 +1058,14 @@ mod tests { async fn testing_thread(r: ArbitraryRecord) { let r = r.0; + let self_id = PeerId::random(); + + // Create channels with proper receivers let (network_event_sender, mut network_event_receiver) = mpsc::channel(1); - let (swarm_cmd_sender, _) = mpsc::channel(1); + let (swarm_cmd_sender, mut swarm_cmd_receiver) = mpsc::channel(1); let mut store = NodeRecordStore::with_config( - PeerId::random(), + self_id, Default::default(), network_event_sender, swarm_cmd_sender,