Skip to content

Commit

Permalink
fix(record-store): ensure record sync before marking as stored
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dirvine authored and maqi committed Jan 9, 2025
1 parent a685123 commit 65c956f
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions ant-networking/src/record_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
};
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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:?}"),
}
}

Expand Down

0 comments on commit 65c956f

Please sign in to comment.