File system synchronization problem

Hello all,
I have a file with a list of json objects and my program receive messages at a quite high rate. Messages consists in an object and its value. For each message I have to iterate through my file and, if the object already exists, update its value with the one from the message, otherwise, add the new object to the file.
My problem is that, after the first message, when I receive the second, I just finished to update the file but probably Linux did not already syncronize the caches and, when reading the file, I get an EOF error while parsing list.

In C language we have the sync function to force the OS to commit the file system caches. Is there anything equivalent in Rust?

Sounds to me like you need to flush a buffer rather than a real "sync." But if you do actually need sync, File::sync_all and File::sync_data exist.

1 Like

Thank you! Indeed the problem was elsewhere...In particular the file is parsed and written in a separated thread, started when a message arrives. The solution was simply joining the thread before handing the next message.
In any case, strange enough, a File object obtained as

	let mut file = match OpenOptions::new().create(true).write(true).truncate(true)
		.open(setup_file) {
		Ok(f) => f,
		Err(e) => {
			println!("error opening output file [{e}]");
			return;
		}
	};

does not have the methods sync_all and sync_data (and many others)

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.