Recent

Author Topic: JSON Config sometimes file is empty  (Read 2379 times)

lainz

  • Hero Member
  • *****
  • Posts: 4449
    • https://lainz.github.io/
JSON Config sometimes file is empty
« on: March 25, 2019, 09:22:56 pm »
Hi, this is a strange bug, we have this function:

Code: Pascal  [Select][+][-]
  1. function GuardarConfig(sKey: string; sValue:string):boolean;
  2. var
  3.     JsonConf: TJSONConfig;
  4.     sSeccion: string;
  5. begin
  6.  try   // TJSONConf is component, so needs an owner.
  7.     JsonConf:=TJSONConfig.Create(nil);
  8.     if gsRealm='' then
  9.         sSeccion:='local'
  10.     else
  11.         sSeccion:=LowerCase(gsRealm);
  12.     Try
  13.        // Set filename. This will read the file.
  14.        JsonConf.FileName:=DirectorioAplicacion+'config.json';
  15.        JsonConf.SetValue('/'+sSeccion+'/'+sKey,sValue);
  16.        if sSeccion<>'local' then //siempre guardo en local para q se use cuando no hay realm
  17.           JsonConf.SetValue('/local/'+sKey,sValue);
  18.        JsonConf.Flush;
  19.     finally
  20.        JsonConf.Free;
  21.     end;
  22.  
  23.  except
  24.  
  25.  end;
  26. end;

With that method we store the config in the application config folder.

But sometimes the file is empty, there is any reason because this error can happen?

lainz

  • Hero Member
  • *****
  • Posts: 4449
    • https://lainz.github.io/
Re: JSON Config sometimes file is empty
« Reply #1 on: March 25, 2019, 10:56:53 pm »
Here is the broken config file attached, I renamed it from json to txt to be able to upload it to the forum.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: JSON Config sometimes file is empty
« Reply #2 on: March 26, 2019, 01:50:10 am »
How to reproduce?

The broken file is full of zeros. From its size, it does not seem to take long to have this happen.

Does gsRealm change, or is it a fixed value?

Can you save sSeccion, sKey and sValue in another file to track the sequence of values needed to cause this?

Your code seems to catch any exception, but does not report them. Is that intentional?

Finally, the usual basic info: Laz/FPC versions, OS, GUI or console app?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11352
  • FPC developer.
Re: JSON Config sometimes file is empty
« Reply #3 on: March 26, 2019, 08:20:14 pm »
Note that I also have this sometimes with TXMLConfig.

lainz

  • Hero Member
  • *****
  • Posts: 4449
    • https://lainz.github.io/
Re: JSON Config sometimes file is empty
« Reply #4 on: March 26, 2019, 09:07:32 pm »
How to reproduce?

The broken file is full of zeros. From its size, it does not seem to take long to have this happen.

Does gsRealm change, or is it a fixed value?

Can you save sSeccion, sKey and sValue in another file to track the sequence of values needed to cause this?

Your code seems to catch any exception, but does not report them. Is that intentional?

Finally, the usual basic info: Laz/FPC versions, OS, GUI or console app?

I don't know how to reproduce.

gsRealm changes by company, so it's the same in the same machine.

There are a lot of sections and key values.

Yes since it's just a config thing we will not show this to the user if fails.

Laz/FPC trunk, Windows, GUI app.

Note that I also have this sometimes with TXMLConfig.

Thanks, now I know is a bug and not related with that simple code I posted.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11352
  • FPC developer.
Re: JSON Config sometimes file is empty
« Reply #5 on: March 26, 2019, 11:18:39 pm »
Note that I use 2008's lazxml files + xmlconfig. The reason for that is that back then I modified them for a customers nonstandard XML.

Anyway, it might be simply a race in overwriting a file.  Usually I have this problem during unexpected forms of abnormal termination.

Maybe one should never overwrite the file, but rename to .bak, save the new, and then only delete the .bak if the save fully completed.

lainz

  • Hero Member
  • *****
  • Posts: 4449
    • https://lainz.github.io/
Re: JSON Config sometimes file is empty
« Reply #6 on: March 26, 2019, 11:29:24 pm »
Maybe one should never overwrite the file, but rename to .bak, save the new, and then only delete the .bak if the save fully completed.

How that can be done using the component?

Edit: The way we use it maybe is not the best, but it saves the file each time an unique setting is stored, as you can see in the function of the first post. Maybe this is the reason this fails as well: when a setting is saved then another is saved too, then as you say the race condition can be triggered by the same app.

So a bak maybe is not the best thing, since a lot of bak files will need to be created when every single setting is beign saved.
« Last Edit: March 26, 2019, 11:48:34 pm by Lainz »

 

TinyPortal © 2005-2018