Potential lost user data. lazarus / 32-Linux ---------------------------------------------------------------------- dbannon · 2025-11-18 04:46 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 ---------------------------------------------------------------------- Thaddy · 2025-11-18 05:08 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TFormMain.OnQueryEndSession(var Cancel: Boolean);begin    cancel := true;// cancel endsession    MyLog('Application.QueryEndSession');    Close;    MyLog('Application.QueryEndSession finished');    cancel := false; // allow endsessionend; procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);begin     Canclose := False; // do not close yet     MyLog('TFormMain.FormCloseQuery Main Window');     CloseChildWindows();     MyLog('TFormMain.FormCloseQuery Main Window Finished');     CanClose := True; // close is OK.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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // do not call this every time, call it once:GetEnvironmentVariableUTF8('HOME') + '/app_shutdown.log'; 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. ---------------------------------------------------------------------- dbannon · 2025-11-18 06:13 Quote from: Thaddy on November 18, 2025, 06:08:46 amIsn'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.  QuoteFirst 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TFormMain.OnQueryEndSession(var Cancel: Boolean);begin    cancel := true;// cancel endsession 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 ---------------------------------------------------------------------- dbannon · 2025-11-18 06:57 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 ---------------------------------------------------------------------- Thaddy · 2025-11-18 07:04 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i export | grep wayland I see if I have a true XWindows system somewhere in that case. ---------------------------------------------------------------------- dbannon · 2025-11-18 07:40 Quote from: Thaddy on November 18, 2025, 08:04:20 amDon'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. QuoteWhat does this say?Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i export | grep waylandI 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 QuoteI 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 ---------------------------------------------------------------------- Thaddy · 2025-11-18 07:51 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. ---------------------------------------------------------------------- dbannon · 2025-11-18 08:05 No, sorry, rushed posting again Davo. I have tested against U2404Mate,  here is an extract from my table - Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i                  gtk2     gtk3     Qt5       Qt6U2404 Mate         No       No     Yes      n.a. 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 .... ---------------------------------------------------------------------- Thaddy · 2025-11-18 08:14 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. ---------------------------------------------------------------------- dbannon · 2025-11-18 08:27 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 ---------------------------------------------------------------------- Thausand · 2025-11-18 08:30 @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 · 2025-11-18 08:37 Quote from: dbannon on November 18, 2025, 09:27:07 amSigh .... Davo I totally agree with that conclusion. ---------------------------------------------------------------------- dbannon · 2025-11-18 09:13 Quote from: Thausand on November 18, 2025, 09:30:30 amNormal 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.  QuoteAs write Thaddy gtk no support gtk2 (then is problem ?). I have already put that idea to bed. Quotexdg 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 ---------------------------------------------------------------------- d2010 · 2025-11-18 09:17 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Var retMyLog:String='';procedure MyLog(content : string; InitFile : boolean = false);    // debug actions at OS shutdownvar T : TextFile;begin    if (length(retMyLog)<03) then       Begin  {$ifdef WINDOWS}             retmyLog:=GetEnvironmentVariableUTF8('HOMEPATH') + '/app_shutdown.log');      {$else}             retmylog:=GetEnvironmentVariableUTF8('HOME') + '/app_shutdown.log');      End;{$endif}    assignFile(T,retMyLog);    if InitFile then      rewrite(T)              // create or clear    else append(T);    writeln(T, FormatDateTime('hh:nn:ss.zzz', now()) + '  ' + Content);  flush(T);    closeFile(T);end; ---------------------------------------------------------------------- dbannon · 2025-11-18 10:36 Quote from: d2010 on November 18, 2025, 10:17:37 amPlease 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 ---------------------------------------------------------------------- Thaddy · 2025-11-18 15:08 Davo, Gnome is not running GTK2 anymore... THAT's the issue. Lazarus has probably a default that is increasingly unsupported. On some distro's GTK2 is an optional install. Together with the Wayland issues, this will be a weekly occurrence on the forum in some form or other. GTK2 relies on X. It works on X. Not - completely - on Wayland. I haven't found a way yet to have Wayland listen to SigTERM. That is not a Lazarus fault persé. That used to work on X. ---------------------------------------------------------------------- dbannon · 2025-11-18 23:33 EDIT : I have posted my tests table in https://wiki.freepascal.org/Shutdown_and_Powerdown subject to revision  Quote from: Thaddy on November 18, 2025, 04:08:35 pmDavo, Gnome is not running GTK2 anymore... THAT's the issue. Lazarus has probably a default that is increasingly unsupported. On some distro's GTK2 is an optional install. You are confusing the "Gnome Shell", (what we call the Desktop or DE, Desktop Environment) AND the gtk libraries. There are two clear, distinct issues (and some other, less distinct ones) -  Any Gnome DE system will not trigger any of the graceful powerdown mechanisms in an LCL app, any widget set. A gtk2 LCL app will not triggering the OnQueryEndSession but does respond well to the redirecting SigTERM model (except, of course, with Gnome DE). Given gtk2's status, we could and should consider the second one a documentation issue, as long as programmers know not to rely on OnQueryEndSession then it OK.  Way past time they moved off it anyway. But the first point is much more serious. A very quick search tells me that the most popular Linux DE is either Gnome or KDE. Gnome, being the default, is certainly the one new users are guided too. Hopefully, as they learn more about Linux, they migrate off it, good ! There is NO Workaround for the Gnome DE issue that I can find. I'm hoping someone can suggest one. QuoteI haven't found a way yet to have Wayland listen to SigTERM. That is not a Lazarus fault persé. That used to work on X. Wrong ! Totally wrong !  gtk2 does not do a graceful power down on every completely Wayland-free system I have tested. There are unresolved eight year old forum postings about this issue. Further, Qt5/6 apps do a graceful powerdown on, eg Trixie KDE using wayland (indeed, switching to XOrg stops gtk3 and Qt6 ?? ). These issues cannot be dismissed as wayland and cannot be dismissed as old technology ! Davo ---------------------------------------------------------------------- WayneSherman · 2025-12-31 21:57 Quote from: dbannon on November 19, 2025, 12:33:56 amThere is NO Workaround for the Gnome DE issue that I can find. I'm hoping someone can suggest one. gtk2 does not do a graceful power down on every completely Wayland- free system I have tested Have you considered listening for signals directly from session managers? To see dbus session messages use: dbus-monitor --session Listen for dbus signals from systemd logind, e.g. PrepareForShutdown and PrepareForShutdownWithMetadata (optionally use delay inhibitor) https://www.freedesktop.org/software/systemd/man/latest/org.freedeskto p.login1.html https://systemd.io/INHIBITOR_LOCKS/ QT session management (or register with ksmserver via dbus directly) https://doc.qt.io/qt-6/session.html https://doc.qt.io/qt-6/qguiapplication.html#commitDataRequest https://develop.kde.org/docs/features/session-managment/ gnome-session https://gnome.pages.gitlab.gnome.org/gnome-session/ https://gnome.pages.gitlab.gnome.org/gnome-session/re05.html X11 Session Management (also works for KDE and XFCE) (does not use dbus) https://www.x.org/releases/X11R7.7/doc/libSM/SMlib.html https://www.x.org/releases/X11R7.7/doc/libSM/xsmp.html ---------------------------------------------------------------------- dbannon · 2026-01-01 11:07 No, I have not Wayne, if I find myself with some time, I might look through your list of references. But note that is not the LCL way, there is an LCL module that claims to handle this stuff in a platform independent way.  So, if you are on the (or 'a') right track, whatever works would need to be pushed to LCL, not your or my app. Davo