-
Notifications
You must be signed in to change notification settings - Fork 27.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[shared_preferences] [windows,linux] Data can be corrupted due to lack of atomic file writes #89211
Comments
@kirill-21 |
Rename .txt -> .json (github does not allow to upload .json) (if you open it with notepad++, you'll see a lot of NUL NUL NUL NUL...) add this to dependencies
place renamed shared preferences file to your's application folder (%appdata%/com.yourname/yourname [Example: C:\Users\user\AppData\Roaming\com.gskinner\flutter_folio\shared_preferences.json]) and add this into the main() function
|
@kirill-21 |
%appdata%/com.yourname/yourname |
That seems to be a Windows path. I am using Mac and tried putting the file in project's root path and called |
Windows and macOS have completely unrelated implementations of preference storage. You cannot reproduce a Windows storage issue on macOS regardless of where you try to put that file. |
That seems like it's likely the correct behavior, but we'd need to document that and provide a reset option.
Doing so automatically would give developers no chance to explain to users that their settings are lost, do some other fallback, etc. That doesn't seem desirable.
Is there anything unusual about how you are using prefs? The other thing we could consider is doing an atomic write (save then move) if Dart doesn't do that internally. |
You can create a backup copy of shared_preferences file on each call of getInstance() method. The next time app tries to call it again and catches an error - delete broken shared_preferences file and restore a backup copy with almost up-to-date settings. "Is there anything unusual about how you are using prefs?" - No, i've questioned users, who faced this issue and many of them told that they did not even change any settings in the app. And while the app was in the development stage(that's something around 5 months), i've never faced this issue. "That seems like it's likely the correct behavior" - but there is no way to clear preferences before calling getInstance() method. |
Please read the whole sentence, not just the half that you quoted. |
I've tried to make a temporary solution by making a backup copy of settings right after each getInstance() call. After some tests i found out that sometimes this backup copy also creates broken. I guess the break of the file where it fills with nul nul nul nul instead of the actual preferences happens in this getInstance() method. |
I have the same issue and this happens to my users a lot and i only tell them to delete the file and open the app again and this is so annoying for them cos they saved data has gone . |
You can try to backup the file on each getInstance() successful call and restore it if something went wrong. kAppFolderPath is the path to your application's folder (C:\Users\user\AppData\Roaming\com.test\test)
|
This comment was marked as off-topic.
This comment was marked as off-topic.
That doesn't make the write atomic, so would not prevent things like file corruption due to the computer suddenly powering off. |
But this prevent damaging setting.json while application simply crashes or process terminated by task manager |
How? That flag makes the future not return until the output is flushed; the app can crash or be terminated regardless of whether a Dart future is still outstanding. |
Trust me crashing is definitely much worse then settings file damaging, because with damaging you can simply use a backup copy. I'm using #89211 (comment) this approach for 2 years in a project with large audience and it's working perfectly without issues. |
the same problem happens with me, I find out that it's happening after windows update or restart. |
The |
This issue is assigned to @tarrinneal but has had no recent status updates. Please consider unassigning this issue if it is not going to be addressed in the near future. This allows people to have a clearer picture of what work is actually planned. Thanks! |
This code simply fails with an error if config file is broken. Maybe it's good to clear preferences file if it contains an error. I do not know why the preferences file breaks but since i've published my app, there were some users, who reported this issue.
Platform where i faced this issue: Windows.
Flutter version: 2.2.3 from stable channel.
I have no idea why but file becomes something like this
The text was updated successfully, but these errors were encountered: