-
Notifications
You must be signed in to change notification settings - Fork 98
Description
I'm using this crate in a project running on esp32 device, and it's running on several devices now.
I'm using the async branch.
All in all its working well.
However, I witness rare cases (on different devices with different sd cards) where a file written to from time to time, ends up being zero length. The file is normally a few kb in length.
In the code there's no path that could lead to zero length file, it's just getting a string from serde_json and writing it, so zero length data can't be there. Also, there are no other file activities in parallel.
Also, in case of an error writing returned, the code would retry after a few seconds, so the fact that at some point its noticed the file is zero it means that likely all writes (potentially different content) ended up with a zero file length. Otherwise, the probability of catching it zero length later is really low.
What my code does is open the file in ReadWriteCreateOrTruncate
mode, and then write the bytes in a single write and then flush and close.
I could always blame the async, but I'm not sure this is the case since in the past I already found a bug in writing (that I fixed here), so I don't want to rush into that conclusion.
So my questions:
- Maybe my simplistic sequence or mode used could lead to such result? Am I using the correct mode for overwriting a file from scratch?
- If it's a bug in the crate, any directions how to search for it, any ideas what could potentially cause it? I tried reproducing this various scenarios (less data on 2nd write, same data, more data) but didn't get that issue.
-
- Is there a way to workaround it?
- I thought for example to check after write the file length and trying again. Would that go through any internal cache potentially making this pointless (getting the requested length rather than what's written on the sdcard)? Is there a way to clear any internal cache and then check file size and see if it was written ok and if not do it again until it succeeds?
- Maybe removing the file before write would be safer (I couldn't find an option to rename a file)
Any other ideas are welcomed.