Recent

Author Topic: Potential lost user data.  (Read 1404 times)

dbannon

  • Hero Member
  • *****
  • Posts: 3667
    • tomboy-ng, a rewrite of the classic Tomboy
Potential lost user data.
« on: November 18, 2025, 05:46:29 am »
When an application closes down, in some cases it needs to save some state information, possibly user data, settings, files to reopen on startup. Under Linux, your LCL apps probably (depending distro, widget set) will not get a chance to do so if that close down happens as the computer is being powered off.

Executive summary -
  • If the user manually closes the app before powering down the compute, LCL events like FormCloseQuery() works fine. No problems.
  • If the app is still running when the user closed down the computer, its most likely a "graceful shutdown" of the app will not happen. In this case, TApplication.OnQueryEndSession is supposed to help, but probably not, read on  -
  • On Gnome, TApplication.OnQueryEndSession is not fired at PowerDown, any widget set.
  • With gtk2, apparently TApplication.OnQueryEndSession never happens with any desktop.
  • Non-GUI apps, built with FPC but not LCL can divert SigTERM and handle it successfully. They use TCustomApplication, it does not introduce the problem that TApplication does.
  • Windows and MacOS don't seem to have a problem.
Details -
Gnome Desktop. The default desktop for the major players in Linux, Debain, Ubuntu, Fedora, SUSE all do not trigger TApplication.OnQueryEndSession. This may be Wayland related, not certain. Installing XLibre gets Qt5/6 working in (only) some cases Fred.

GTK2 Widgetset. Does not trigger TApplication.OnQueryEndSession. The forum has discussions about this going back many years. However, there is a workaround, re-directing SigTERM to a handler of your choosing does seem to work in some systems for gtk2 only. Not Gnome, not Ubuntu (and derivatives, ie Linux Mint). See below.

Other Desktops. ie KDE, Mate, XFCe Cinnamon etc. Do seem to trigger TApplication.OnQueryEndSession when using Qt5 and Qt6, unpredictably a few GTK3 hits too. Not gtk2.

Given most of (linux) end uses use Gnome and most linux programmers use gtk2 (tbc), its fair to say, user data is at risk.

The really grotty details

Attached source will build gtk2, gtk3, Qt5 and Qt6 versions of a test app. Launch one by double clicking from your file manager (not from command line, other issues). Then restart (not log off, other issues) and inspect the app_shutdown.log file in your home directory.

I created the https://wiki.freepascal.org/Shutdown_and_Powerdown after some tests using Qt5 and XFCE, sadly that was grossly optimistic and will need a massive rewrite. I will, shortly, get my chart up there showing what does, and does not work. Its not pretty.

Anyone who cares to correct my mistakes is very welcome !

Davo

See also https://forum.lazarus.freepascal.org/index.php/topic,72232.0.html
« Last Edit: November 18, 2025, 05:50:10 am by dbannon »
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Potential lost user data.
« Reply #1 on: November 18, 2025, 06:08:46 am »
Isn't TApplication.OnQueryEndSession to respond to SigTERM in GUI apps too? Only with SigKILL you are supposed to be on your own. I will have a look.
First thought: running xWayland on top of Wayland?
Second thought: the cancose/cancel variables need to be explicitly set to false, rep. true at the earliest opportunitity. Likewise it must be explicitly set to true when done processing. The var canclose may otherwise be undefined.
Code: Pascal  [Select][+][-]
  1. procedure TFormMain.OnQueryEndSession(var Cancel: Boolean);
  2. begin
  3.     cancel := true;// cancel endsession
  4.     MyLog('Application.QueryEndSession');
  5.     Close;
  6.     MyLog('Application.QueryEndSession finished');
  7.     cancel := false; // allow endsession
  8. end;
  9.  
  10. procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  11. begin
  12.      Canclose := False; // do not close yet
  13.      MyLog('TFormMain.FormCloseQuery Main Window');
  14.      CloseChildWindows();
  15.      MyLog('TFormMain.FormCloseQuery Main Window Finished');
  16.      CanClose := True; // close is OK.
  17. end;

Third thought: why do you query the environment all the time in every log call? Get that outside of it. It does not change while the app is running.
Code: Pascal  [Select][+][-]
  1. // do not call this every time, call it once:
  2. GetEnvironmentVariableUTF8('HOME') + '/app_shutdown.log';
  3.  
Fourth thought: why do you use FormActivate to create child forms? That is wrong. Activation occurs every time the form has activation not a single time. (Although you have a mechanism to test) Should be in  formcreate.

Will run some tests.
- Wayland won't listen...indeed. I will have a look if I have a true X-Windows system somewhere.
« Last Edit: November 18, 2025, 07:59:09 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

dbannon

  • Hero Member
  • *****
  • Posts: 3667
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Potential lost user data.
« Reply #2 on: November 18, 2025, 07:13:32 am »
Isn't TApplication.OnQueryEndSession to respond to SigTERM in GUI apps too?
I cannot establish what "signal" is sent at powerdown, I don't believe it is SigTERM. Maybe some  X protcol, now replaced with some Wayland protocol. I have been searching, endlessly, for something about what what is sent from OS||DE||Wayland||X at powerdown. A non-GUI app gets sent SigTERM and is quite manageable. If it is SigTERM, then TApplication gets to it first. If I could fnd where, I'd disable it to test. 

Quote
First thought: running xWayland on top of Wayland?
I have tried the Qt5/6 with libxcb, no diference. gtk2 does not know about Wayland.

Second thought: the cancose/cancel variables need to be explicitly set to false, rep. true at the earliest opportunitity. Likewise it must be explicitly set to true when done processing. The var canclose may otherwise be undefined.
Code: Pascal  [Select][+][-]
  1. procedure TFormMain.OnQueryEndSession(var Cancel: Boolean);
  2. begin
  3.     cancel := true;// cancel endsession
  4.  
Wow, thats confronting, the calling process would not know about it until it returns ?? Or does being var ake it accessible to another thread ??   I will investigate ....

Your other two thoughts ?  Historical accidents, no significance.

I'll test that "cancel := True" ....

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

dbannon

  • Hero Member
  • *****
  • Posts: 3667
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Potential lost user data.
« Reply #3 on: November 18, 2025, 07:57:25 am »
No luck, I tried setting CanClose and Cancel to False/True at the immediate start of handler, made no difference.

Aside : I found earlier that redirecing SigTERM to my own handler AND using OnQueryEndSession was bad thing with at least Qt5. The OnQueryEndSession method always was the one to respond but "sometimes" resulted in an early termination while some child forms remained un-closed. But that does not happen with gtk2, leaving me thinking that gtk2 is just not doing the OnQueryEndSession thing at all.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Potential lost user data.
« Reply #4 on: November 18, 2025, 08:04:20 am »
No luck here either.
It fails with both configurations. Just a one line log.
It works in both cases if the user terminates the program.
Don't confuse wayland with Qt though. Qt should be compared with gtk2.

What does this say?
Code: Bash  [Select][+][-]
  1. export | grep wayland

I see if I have a true XWindows system somewhere in that case.
« Last Edit: November 18, 2025, 08:06:15 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

dbannon

  • Hero Member
  • *****
  • Posts: 3667
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Potential lost user data.
« Reply #5 on: November 18, 2025, 08:40:31 am »
Don't confuse wayland with Qt though. Qt should be compared with gtk2.
Sloppy wording on my part. My point was : Qt5/6 do talk directly to Wayland unless set to use libxcb (which I do automatically in my installs).
gtk2 on the the hand ignores wayland, thats why, for example, it can still determine the location of a new window/form.

Quote
What does this say?
Code: Bash  [Select][+][-]
  1. export | grep wayland
I would have used 'set', never saw export used like that. Works ...
I am testing on, probably 20 plus OS/Desktop/options. So, anything you want ! But I cannot copy n paste from a VM to desktop so, just the important stuff -

U2504, gnome
QT_IM_MODUALS='wayland;ibus'
WAYLAND_DISPLAY=wayland-0
XAUTHORITY=/run/user/1000/.mutter-Xwaylandauth.LDWRF3
XDG_SESSION_TYPE=wayland

U2504, gnome with XLibra installed
Same as above

U2404, Mate (ie no wayland)
nothing !

Trixie KDE  (where gtk3, Qt5 and Qt6 gracefull powerdown works but, wait for it, FormCloseQuery is called directly, no OnQueryEndSession involved)
WAYLAND_DISPLAY=wayland-0
XDG_SESSION_TYPE=wayland


Quote
I see if I have a true XWindows system somewhere in that case.
I am unsure just where XWayland fits in. Its a layer between pure X apps and Wayland ?  libxcb in other words ?

Anyway, the effect I am seeing happens, even in a wayland free zone (such as a Mate Desktop) and people were complaining about this problem (including me) on our forum before most of us had even heard the term wayland (or Qt5/6).

Davo



Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Potential lost user data.
« Reply #6 on: November 18, 2025, 08:51:48 am »
Try your mate, because that seems a true x-windows system.
If that works, well, then it is wayland again.

(Yes, xwayland is some X on top of wayland: sometimes works....but not here since I tested that.)

Anyway: gtk2 itself is end of life and won't get any updates and it seems to me that "fixing" this for gtk2 in Lazarus itself would be a big ask.
As I wrote earlier this week: frankly Lazarus should move away from gtk2 as default for that reason alone.
« Last Edit: November 18, 2025, 09:08:16 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

dbannon

  • Hero Member
  • *****
  • Posts: 3667
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Potential lost user data.
« Reply #7 on: November 18, 2025, 09:05:16 am »
No, sorry, rushed posting again Davo.

I have tested against U2404Mate,  here is an extract from my table -

Code: Pascal  [Select][+][-]
  1.                    gtk2     gtk3     Qt5       Qt6
  2. U2404 Mate         No       No       Yes      n.a.
  3. U2404 Mate         Part(6) Yes(6)  

The table is too big to post on Forum, will appear on Wiki soon.

The (6)  says that using the redirect SigTERM model works (ie good for gtk3) and the 'Part' says that with gtk2 only part of the child form list was closed, So, the signal was intercepted but something decided to close it all down before my handler was finished.

I have done a lot of tests Thaddy, I a getting very good at typing my password.....

Davo

OK, so even the code columns don't line up, sigh ....
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Potential lost user data.
« Reply #8 on: November 18, 2025, 09:14:50 am »
So Qt5 works. I need to stop further tests then, because imm it is Wayland and GTK2 itself is no longer supported by gtk itself.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

dbannon

  • Hero Member
  • *****
  • Posts: 3667
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Potential lost user data.
« Reply #9 on: November 18, 2025, 09:27:07 am »
Well, no, not that simple.

Nothing works on a Gnome Desktop. The default desktop for the major players in Linux, Debian, Ubuntu, Fedora, SUSE.

Further, gtk2 is still the default build for Lazarus and people read that as saying its the one they should use. Wrong but its what happens. Personally, I'd be happy to drop the gtk2 version of my app but gtk2 users download a lot more than the Qt5/6 people. Probably because of the nexus between the libqt5pas and lazarus release confues people.

Further still, its likely that wayland is the problem on Gnome and most Desktops report "progress" with getting a wayland version out.

Sigh ....

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Thausand

  • Sr. Member
  • ****
  • Posts: 458
Re: Potential lost user data.
« Reply #10 on: November 18, 2025, 09:30:30 am »
@dbannon:
Normal is have use libportal notify for gnome. Is DBUS implement. As write Thaddy gtk no support gtk2 (then is problem ?).

xdg website have list for support widget. libortal no work wayland.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Potential lost user data.
« Reply #11 on: November 18, 2025, 09:37:53 am »
Sigh ....

Davo
I totally agree with that conclusion.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

dbannon

  • Hero Member
  • *****
  • Posts: 3667
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Potential lost user data.
« Reply #12 on: November 18, 2025, 10:13:51 am »
Normal is have use libportal notify for gnome.

Sorry, Thausand, I don't understand. libportal is a flatpak to communicate between flatpak application. I just checked Debian Trixie, a wayland system, that does do a graceful shutdown for gtk3, qt5 and qt6. It does not have libportal installed. 

Quote
As write Thaddy gtk no support gtk2 (then is problem ?).
I have already put that idea to bed.

Quote
xdg website have list for support widget. libortal no work wayland.

did you mean xdp ?  It lists the functions available calls in libportal but shutting down is not one of them.

Again, I do no understand. But thanks for trying to help.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

d2010

  • Sr. Member
  • ****
  • Posts: 251
Re: Potential lost user data.
« Reply #13 on: November 18, 2025, 10:17:37 am »
Please you rewrite, (you lose too much  your-time of mylog)
You keep name of file , otherway of outside of function. %)
 because you will need that name.
How to save state of InitFile : boolean outside of function myLog?
Code: Pascal  [Select][+][-]
  1. Var retMyLog:String='';
  2. procedure MyLog(content : string; InitFile : boolean = false);    // debug actions at OS shutdown
  3. var T : TextFile;
  4. begin
  5.     if (length(retMyLog)<03) then
  6.       Begin  {$ifdef WINDOWS}
  7.              retmyLog:=GetEnvironmentVariableUTF8('HOMEPATH') + '/app_shutdown.log');
  8.       {$else}
  9.              retmylog:=GetEnvironmentVariableUTF8('HOME') + '/app_shutdown.log');
  10.       End;{$endif}
  11.     assignFile(T,retMyLog);
  12.     if InitFile then
  13.         rewrite(T)              // create or clear
  14.     else append(T);
  15.     writeln(T, FormatDateTime('hh:nn:ss.zzz', now()) + '  ' + Content);
  16.     flush(T);
  17.     closeFile(T);
  18. end;
  19.  
« Last Edit: November 18, 2025, 10:27:13 am by d2010 »

dbannon

  • Hero Member
  • *****
  • Posts: 3667
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Potential lost user data.
« Reply #14 on: November 18, 2025, 11:36:05 am »
Please you rewrite, (you lose too much  your-time of mylog)

Thats really, really unimportant. I sleep for a full second in each child window to simulate the time a really slow disk would write a really long file. Getting env vars is actually a very quick process anyway, even without the sleep(), a write to disk will take a lot longer than GetEnvironmentVariableUTF8() call.

What is important is that some Distro/DE/widgetset combinations do not call the necessary functions to start the process. 

And some Desktops, specifically Gnome, just shut any LCL app down giving it no chance to save data. And Gnome is the default DE for the bigger linux distros.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

 

TinyPortal © 2005-2018