Weird behaviour with files not being written predictably.
Closed this issue ยท 6 comments
I've got a weird issue with an in-memory filesystem behaving irrationally. It's probably easier explained in code. This test case will pass:
#[test]
fn dumb_test() -> VfsResult<()> {
let root: VfsPath = MemoryFS::new().into();
let path = root.join("somefile.txt")?;
// This test case passes. Remove these braces and it will fail!
{
let mut file = path.create_file()?;
file.write_all(b"Hello world\n")?;
file.flush()?;
}
let mut buf = String::new();
path.open_file()?.read_to_string(&mut buf)?;
assert_eq!("Hello world\n", buf);
Ok(())
}If you remove the braces, it fails. I'm baffled as to why adding/removing the block changes the behaviour, and also worried that the program's behaviour depends on something that no-one would pick up in code review. Any idea why this happens?
Notes:
$ rustc --version
rustc 1.82.0 (f6e511eec 2024-10-15) (built from a source tarball)Here's a repo with the test case: https://github.com/krisajenkins/rust_execution_weirdness
Thanks for the thorough report!
I had a quick look, and the probable cause is that the inmemory implementation of flush() does not actually update the file entry in the internal hashmap. That only happens on drop() which is why it works with the surrounding braces.
I'll try my hand at a fix later tonight.
Should be working now. Let me know if you have any further issues.
I'd be happy to cut a point release as well.
Thanks for responding so quickly! Yes, that works perfectly, thanks.
I can use that commit directly for now, but when you get the time a point release would be very helpful.
Thanks again. ๐
Fix is in release 0.12.1 - enjoy! ๐
Awesome - thanks so much!