lanoxx / tilda

A Gtk based drop down terminal for Linux and Unix
GNU General Public License v2.0
1.28k stars 161 forks source link

Tilda improper configuration file behaviour on reset/exit #155

Closed pik closed 9 years ago

pik commented 9 years ago

I actually think there was an issue for this somewhere - but for some reason I couldn't find it (feel free to close and reference there otherwise). Sometimes after exiting Tilda or rebooting, opening Tilda again will fail to load the old-config file and open with a default one.

More likely I think something goes wrong in writing the current state back to disk on exit/shutdown and the instance cannot start properly from the configuration file; but I'm really not sure why this is going on...

Edit: I don't know how to reproduce this consistently, if someone does please leave a comment.

lanoxx commented 9 years ago

I have really no idea but here is a wild guess: The config of tilda is kept open all the time until tilda is closed. Maybe depending on how tilda is closed it does not have enough time to properly close the file handler and this causes the file to be truncated somehow. When you look at src/tilda.c:725 you see that tilda registers the termination_handler to all kinds of signals.

One way to try and reproduce this issue would be to manually kill tilda with the different signals and see if the config is properly closed. Maybe that gives us an indication whats going wrong.

lanoxx commented 9 years ago

I just had another idea and came up with the following command:

(tilda &) && sleep 1 && kill `pidof tilda` && ls -la ~/.config/tilda/config_0 && sleep 1 && ls -la ~/.config/tilda/config_0

When you run that multiple times you will sometimes see the size of the config file becomes zero byes:

[...] 2531 Jun 17 20:44 ~/.config/tilda/config_0
[...] 2531 Jun 17 20:44 ~/.config/tilda/config_0

[...]      0 Jun 17 20:44 ~/.config/tilda/config_0
[...] 2531 Jun 17 20:44 ~/.config/tilda/config_0

I think when tilda is closed or terminated, then it tries to save the config file one more time and does that in a way that first trunkates and then rewrites the config file. If tilda is killed by the system before it has time to write, flush and close the file again, then we end up with a zero byte big config file.

I am thinking of two ways to fix this. First, we could change config_write in configsys.c to write to a temporary file and then rename the file when finished. Since rename is atomic it would ensure that we endup with either the old file or the new file but never a file with zero size.

Second option would be to use config_free(NULL) at the end of main() to just free the config structure but not rewrite it. The downside of this would be, that changes made by command line options would not be persisted anymore. A quick look at the code also shows that there is one occurrence of config_set* tilda_terminal.c and tilda_window.c (both outside the wizard) and so this change also would not be persisted on exit.

I will prepare a commit for the first option and then we need to see if that fixes the problem, and if not, we can still pursue the second option.