chromelyapps / Chromely

Build Cross Platform HTML Desktop Apps on .NET using native GUI, HTML5, JavaScript, CSS, Owin, AspNetCore (MVC, RazorPages, Blazor)
MIT License
2.98k stars 279 forks source link

Changes to LocalStorage lost if Chromely app is closed too quickly after writing value #293

Closed RobertBouillon closed 3 years ago

RobertBouillon commented 3 years ago

If the Chromely application host is closed within about 5 seconds of writing data to LocalStorage, the latest writes are lost.

There appears to be some sort of lazy process which persists the contents of LocalStorage to disk, which is being aborted too soon.

Do I need to invoke some method or explicitly dispose of some object when the application quits?

mattkol commented 3 years ago

@RobertBouillon This may help -https://github.com/chromelyapps/Chromely/issues/168

RobertBouillon commented 3 years ago

@mattkol

I believe my issue is different.

Consider the following two scenarios:

Scenario 1

  1. Write data to LocalStorage
  2. Immediately close Chromely
  3. Open Application
  4. Saved data is lost

Scenario 2

  1. Write data to LocalStorage
  2. Wait 10 seconds to close, then close application
  3. Open Application
  4. Saved data is Retained

It seems there's a lazy writer which I'm preempting when I close the application. If I "save and close," there's a chance that I haven't actually saved, and I've lost data.

private static void SetupFileSystem(IChromelyConfiguration config)
{
  var docs = new OsDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
  var tempus = docs.GetDirectory("Tempus");
  var cache = tempus.GetDirectory("Data");
  var reports = tempus.GetDirectory("Reports");

  if (!tempus.Exists())
    tempus.Create();

  if (!cache.Exists())
    cache.Create();

  if (!reports.Exists())
    reports.Create();

  config.CustomSettings["CachePath"] = cache.FullName;
}
mattkol commented 3 years ago

This is the closest I see to what you described - https://www.magpcss.org/ceforum/viewtopic.php?f=6&t=13645

You have to release all browser references (and probably other CefRefPtr as well) when closing app before calling CefShutdown

On what OS is this happening? There was an outstanding issue with shutdown process in MacOS - may be related.

RobertBouillon commented 3 years ago

This is Win 10. Happens in dev and prod.

Is there anything I can call that's exposed out of Chromely? What if I pull the source and add a reference that way? Anything I might be able to poke when the app shuts down?

mattkol commented 3 years ago

Is there anything I can call that's exposed out of Chromely?

Nothing that I am aware of. Chromely does not touch localStorage implementation but some of our actions may have prevented a proper shutdown that is impacting that.

Anything I might be able to poke when the app shuts down?

This is usually a conflict between Windows (host) shutdown and CEF shutdown. We may have to look at the steps closely - starting from - Close or Destroy

I looked around and it seems not an uncommon problem - Electron issue. Electron has a solution - FlushStorageData. This is similar to what was done for cookies - https://github.com/chromelyapps/Chromely/issues/259#issuecomment-732829390

The CEF community had use of enable-aggressive-domstorage-flushing switch. Maybe a temporary fix if it works.

My suggestion is that you help create a simple one page html and we can debug using base Chromely implementation to track it and see if it is something we can resolve here, otherwise we resort to CefGlue or CEF for help.

RobertBouillon commented 3 years ago

I like the idea of creating a simple one-page HTML app to reproduce the issue. I'll need a couple of days, as this is a lower priority (we have a workaround), but I'll put something together. Thanks!

mattkol commented 3 years ago

@RobertBouillon I could not reproduce this. Please reopen if need be. Attached is what I used. localStorage.zip