lesnitsky / flutter_localstorage

📦 LocalStorage for Flutter
MIT License
301 stars 60 forks source link

Unhandled Exception: FileSystemException: An async operation is currently pending #56

Closed nilevars closed 3 years ago

nilevars commented 3 years ago

I updated the localstorage version to ^3.0.6+9 but I get the following exception

[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: FileSystemException: An async operation is currently pending, path = '/Users/username/Library/Developer/CoreSimulator/Devices/5C337305-95CC-4BB3-926F-B607E154A1A9/data/Containers/Data/Application/CD793806-AF33-4755-B7E2-D110B346032A/Documents/filename.json'

This is the output of Flutter doctor :

flutter doctor Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel unknown, v1.17.5, on macOS 11.1 20C69, locale en-KW)

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2) [✓] Xcode - develop for iOS and macOS (Xcode 12.2) [✓] Android Studio (version 4.0) [✓] VS Code (version 1.52.1) [✓] Connected device (1 available)

• No issues found!

If this exception is not caused by this library, Kindly let me know.

ppshobi commented 3 years ago

We are also facing this issue. Downgraded to 3.0.1+4 to fix it for now

lesnitsky commented 3 years ago

this usually means that you have unawaited call. Make sure you don't have any unawaited calls to the methods which are async (return futures)

On Tue, Dec 22, 2020, 5:27 PM Shobi notifications@github.com wrote:

We are also facing this issue. Downgraded to 3.0.1+4 to fix it for now

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/lesnitsky/flutter_localstorage/issues/56#issuecomment-749566120, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABPYUNVISEIQOLPZBAGK4WDSWCUGPANCNFSM4VFWOZYA .

nilevars commented 3 years ago

Thanks @lesnitsky I missed some await statements

acamenhas commented 3 years ago

I have this problem too. Downgraded to 3.0.1+4 solved my problem. Thanks @ppshobi

lesnitsky commented 3 years ago

@acamenhas downgrading is bad idea :) Please let me know if this comment is clear enough and solves the problem

pierrea commented 3 years ago

This issue is happening to me as well after upgrading to 3.0.6+9. It is possible there are some unawaited calls in my code (digging into that now) but regardless, this didn't happen before. I'm curious if you made it become less tolerant towards unawaited calls in the latest updates. Thanks!

felix-ht commented 3 years ago

We have the same issue with 3.0.6+9 - with 3.0.3+6 everything is good. What changed between the releases? @lesnitsky

ltphy commented 3 years ago

The problem will occur If two operation write at the same time to the file stored in local storage, while the first operation has not finished to write. (The write operation is still locked) This means you have to check whether is any operation did not use await before the error happened.

felix-ht commented 3 years ago

i don't think that await is enough to guarantee that this does not happen. If you run some code and this code causes a write to local storage and awaits it, the rest of the app keeps running. If there is any other code scheduled to run it will run, and this code can access local storage again - causing this error.

tmorone-rezi commented 3 years ago

This package works well to provide mutually exclusive access to a resource. https://pub.dev/packages/mutex

await mutex.protect(() async => await _cache?.setItem(itemKey, item, toEncodable));

felix-ht commented 3 years ago

@lesnitsky would i be possible to get a solution similar to the one given by @tmorone-rezi in the library, so that this error fundamentally cannot happen any longer?

A very common use case for this library is to store state. State changes on user input, so even with the trivial increment flutter example, where you press the increment button and a value gets incremented, you can get this error. Assuming that you press it fast enough and you use setItem to save the state on any modification.

I checked the exact same code that was causing issues with this code and simply replaced it with sembast and the issues where gone.

pauline-jubiliz commented 2 years ago

Hello,

@lesnitsky

I have the same problem ! This is my function :

final LocalStorage _localStorageNotification = LocalStorage('Notifications'); Future<void> setItemNotification(final Map<String, String> mapDates) async { bool ready = await _localStorageNotification.ready; if (ready) { await _localStorageNotification.setItem(dates, mapDates); } }

But even if the storage is ready, I have the error on setItem : FileSystemException (FileSystemException: An async operation is currently pending, path = '')

My version : 4.0.0+1

Any idea where the problem might come from?

Thank you!

felix-ht commented 2 years ago

quite frankly i would suggest to move to sembast - its a bit more complicated to use - but has no problems such as this. It is also much more future proof if you ever need more advanced features.

SeaRoth commented 2 years ago

I keep running into this problem and unfortunately will be moving away from LocalStorage

garyodonoghue commented 2 years ago

If it's any use to anyone, but i was having the same trouble, and thought i had added all the necessary awaits, but i was using a forEach loop which will not wait until each async function is executed before moving on, so that was the problem, changed it to not use forEach and then it waits correctly, and i dont get this exception, see https://stackoverflow.com/questions/51106934/my-async-call-is-returning-before-list-is-populated-in-foreach-loop

lesnitsky commented 2 years ago

https://github.com/lesnitsky/flutter_localstorage/issues/56#issuecomment-749711501