Bug 488520

Summary: kde-plasma/plasma-workspace 5.27.11.1: ksmserver still sometimes fails to save and later restore a session
Product: [Plasma] plasmashell Reporter: i.Dark_Templar <idarktemplar>
Component: Session ManagementAssignee: Plasma Bugs List <plasma-bugs>
Status: REPORTED ---    
Severity: normal CC: avg, grahamperrin, imaginator, kde, kishore96, natalie_clarius, nate, o.freyermuth
Priority: NOR    
Version: 6.1.3   
Target Milestone: 1.0   
Platform: Gentoo Packages   
OS: Linux   
See Also: https://bugs.kde.org/show_bug.cgi?id=490469
https://bugs.kde.org/show_bug.cgi?id=487912
https://bugs.kde.org/show_bug.cgi?id=491421
https://bugs.kde.org/show_bug.cgi?id=491130
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description i.Dark_Templar 2024-06-15 09:54:53 UTC
SUMMARY
plasma-workspace 5.27.11.1 with https://invent.kde.org/plasma/plasma-workspace/-/commit/918cb38c3522ffcc241ac6a22b6741e41cdd3c30 still sometimes fail to restore previous session.

It doesn't happen every time. For me it happens only once every 3-4 boots. I didn't investigate it further yet.

STEPS TO REPRODUCE
1. Set 'restore last session' in session control kcm.
2. Open a couple of KDE applications, like kate, konsole, dolphin
3. Reboot
4. Login into plasma session

OBSERVED RESULT
Sometimes, once in 3-4 tries, session doesn't restore

EXPECTED RESULT
Session is always restored

SOFTWARE/OS VERSIONS
Linux/KDE Plasma: 6.6.30-gentoo / X11
(available in About System)
KDE Plasma Version:  5.27.11
KDE Frameworks Version: 5.116.0
Qt Version: 5.15.14

ADDITIONAL INFORMATION
I can only assume some part of saving previous session doesn't work reliably anymore.
Comment 1 i.Dark_Templar 2024-06-19 07:58:12 UTC
Yesterday all applications were restored, but dolphin didn't restore any tabs it had in last session.
Today no applications were restored.
Comment 2 i.Dark_Templar 2024-07-20 08:45:14 UTC
Still reproduces every couple of boots with plasma 6.1.3.
Comment 3 Oliver Freyermuth 2024-07-24 20:25:05 UTC
I also observed this mostly randomly, especially if many applications are open, in those cases:
~/.config/ksmserverrc
was cut off for me. Using OpenRC here and I am wondering whether the file was cut off due to too fast / unexpected process termination when logging off.
Comment 4 imaginator 2024-09-10 20:52:27 UTC
I do confirm this for a BLFS-system. [1]  X11-sessions are sometimes only partially restored or not at all.  Plasma is practically unusable in this state.

It's clear that session-restore with patched plasma-workspace-2.27.11.1 is still broken.  Besides the errors fixed by the patch some other problem must have been introduced that has not yet been addressed.

------------------
[1] Plasma-5.27.11
      QT-5.15.14
      FW-5.116.0
Comment 5 imaginator 2024-09-16 10:14:28 UTC
(In reply to Oliver Freyermuth from comment #3)
> I also observed this mostly randomly, especially if many applications are
> open, in those cases:
> ~/.config/ksmserverrc
> was cut off for me. Using OpenRC here and I am wondering whether the file
> was cut off due to too fast / unexpected process termination when logging
> off.

I doubt that the number of open programs has something to do with it.  Even with many programs open (16), session-restore has been working flawlessly here since my last post.

I rather suspect that under certain circumstances a mismatch occurs between the files referenced in ~/.config/ksmserverrc and the status-files in ~/.config/session.  An incomplete ksmserverrc (if you mean that by "cut off") may indeed be a cause.  So if this Heisenbug strikes again a closer look at ksmserverrc and the files in /session may be worthwile.
Comment 6 Oliver Freyermuth 2024-09-16 19:05:14 UTC
(In reply to imaginator from comment #5)
> (In reply to Oliver Freyermuth from comment #3)
> I rather suspect that under certain circumstances a mismatch occurs between
> the files referenced in ~/.config/ksmserverrc and the status-files in
> ~/.config/session.  An incomplete ksmserverrc (if you mean that by "cut
> off") may indeed be a cause.  So if this Heisenbug strikes again a closer
> look at ksmserverrc and the files in /session may be worthwile.

Yes, in fact I mean "incomplete" when mentioning "cut off", as if the program writing out the file was terminated hard while writing... 

I had the bug occur again on a system with Plasma 6.1, with wayland this time (but the system also uses openrc, no systemd). Full ksmserverrc:
```
[$Version]
update_info=ksmserver_update_loginMode_value.upd:ksmserver_update_loginMode_value_default_enum

[General]
screenCount=1

[LegacySession: saved at previous logout]
count=0

[Session: saved at previous logout]
clientId1=10ab6d6731000147059923000000123150016
clientId2=10ab6d6731000172641278200000120020004
count=2
program1=/usr/bin/gkrellm
program2=/home/olifre/.dropbox-dist/dropbox-lnx.x86_64-207.4.5821/dropbox
restartCommand1=/usr/bin/gkrellm,--sm-client-id,10ab6d6731000147059923000000123150016
restartCommand2=/home/olifre/.dropbox-dist/dropbox-lnx.x86_64-207.4.5821/dropbox,-session,10ab6d6731000172641278200000120020004_1726451308_783776
restartStyleHint1=0
restartStyleHint2=0
userId1=olifre
userId2=olifre
```
The session had KeepassXC and some Konsole terminals open when I shut down, but this was not stored in the ksmserverrc. 

Checking the "session" directory, what should I be looking for? Dropbox and gkrellm don't save any state, and the Konsole sessions were not persisted. I only find one file in the "session" directory matching in time with the last shutdown of the system:
```
-rw------- 1 olifre olifre  10254 16. Sep 03:48 'kwin_saved at previous logout_'
```
Nothing else was saved.
Comment 7 imaginator 2024-09-17 09:32:59 UTC
(In reply to Oliver Freyermuth from comment #6)
> (In reply to imaginator from comment #5)
> > (In reply to Oliver Freyermuth from comment #3)
> > I rather suspect that under certain circumstances a mismatch occurs between
> > the files referenced in ~/.config/ksmserverrc and the status-files in
> > ~/.config/session.  An incomplete ksmserverrc (if you mean that by "cut
> > off") may indeed be a cause.  So if this Heisenbug strikes again a closer
> > look at ksmserverrc and the files in /session may be worthwile.
> 
> Yes, in fact I mean "incomplete" when mentioning "cut off", as if the
> program writing out the file was terminated hard while writing... 
> 
> I had the bug occur again on a system with Plasma 6.1, with wayland this
> time (but the system also uses openrc, no systemd). Full ksmserverrc:
> ```
> [$Version]
> update_info=ksmserver_update_loginMode_value.upd:
> ksmserver_update_loginMode_value_default_enum
> 
> [General]
> screenCount=1
> 
> [LegacySession: saved at previous logout]
> count=0
> 
> [Session: saved at previous logout]
> clientId1=10ab6d6731000147059923000000123150016
> clientId2=10ab6d6731000172641278200000120020004
> count=2
> program1=/usr/bin/gkrellm
> program2=/home/olifre/.dropbox-dist/dropbox-lnx.x86_64-207.4.5821/dropbox
> restartCommand1=/usr/bin/gkrellm,--sm-client-id,
> 10ab6d6731000147059923000000123150016
> restartCommand2=/home/olifre/.dropbox-dist/dropbox-lnx.x86_64-207.4.5821/
> dropbox,-session,10ab6d6731000172641278200000120020004_1726451308_783776
> restartStyleHint1=0
> restartStyleHint2=0
> userId1=olifre
> userId2=olifre
> ```
> The session had KeepassXC and some Konsole terminals open when I shut down,
> but this was not stored in the ksmserverrc. 
> 
> Checking the "session" directory, what should I be looking for? Dropbox and
> gkrellm don't save any state, and the Konsole sessions were not persisted. I
> only find one file in the "session" directory matching in time with the last
> shutdown of the system:
> ```
> -rw------- 1 olifre olifre  10254 16. Sep 03:48 'kwin_saved at previous
> logout_'
> ```
> Nothing else was saved.

Information on how Plasma's session-restore actually works is hard to come by.  The gist of what I've figured out so far: for a successful session-restore there have to be files in ~/.config/session which describe the (KDE-) programs' state at logout and whose ID must match those of the "restartCommand*" in ~/.config/ksmserverrc.  The files are created by the programs when a session ends and are used by ksmserver to recreate their state at login.  (The files should be purged at logout ("discardCommand*") but that doesn't seem to work consistently.)  If either the restart-command in ksmserverrc or the matching file is missing, session-restore for that program will fail.  So, according to your observation we should look for both, the restart-command and the matching file.

That ksmserverrc was incomplete and the files for Konsole were not created at all is important information and should give KDE-devs enough of a clue already which of the changes in plasma-workspace-5.27.11.1 may cause the problem.
Comment 8 Oliver Freyermuth 2024-09-17 19:00:50 UTC
Thanks for confirming that this is helpful!

Since I encountered this once more, this time on 5.27.11.1 with X11 again, I wanted to add a final dump of the ksmserverrc:
```
[$Version]
update_info=ksmserver_update_loginMode_value.upd:ksmserver_update_loginMode_value_default_enum

[General]
screenCount=1

[LegacySession: saved at previous logout]
clientMachine1=localhost
clientMachine2=localhost
clientMachine3=localhost
command1=thunderbird
command2=gkrellm
command3=firefox
count=3

[Session: saved at previous logout]
count=0
```
So "everything" was lost. The session directory, again, only contains a fresh "'kwin_saved at previous logout_'" but nothing else with a matching modification time. 

So at least in my case, I see this both with 5.27.11 and X11 and 6.1 and Wayland. Note that all my systems use OpenRC, not systemd, in case this is relevant.
Comment 9 Oliver Freyermuth 2024-09-17 20:03:14 UTC
Sorry for the double post, but this is likely also relevant:
I realized that on the 5.27.11.1 system, the shell history was also cut off after 6.8 MB /it is 17 MB large in my case) during the last "session restore failure" boot. I could restore it from a backup, but it looks like the shell was force-killed during session teardown and could not flush the history file (maybe a symptom caused by the same bug?).
Comment 10 imaginator 2024-09-19 09:52:59 UTC
(In reply to Oliver Freyermuth from comment #9)
> Sorry for the double post, but this is likely also relevant:
> I realized that on the 5.27.11.1 system, the shell history was also cut off
> after 6.8 MB /it is 17 MB large in my case) during the last "session restore
> failure" boot. I could restore it from a backup, but it looks like the shell
> was force-killed during session teardown and could not flush the history
> file (maybe a symptom caused by the same bug?).

Maybe, but I can't confirm this.  
BTW: your .bash_history is rather large.  You may try to keep it in check a little with HISTSIZE, HISTIGNORE and HISTCONTROL (-> man bash).
Comment 11 Graham Perrin 2024-11-15 22:23:12 UTC
No longer reproducible on FreeBSD 15.0-CURRENT.

<https://github.com/FreeBSD/freebsd-ports/commit/f00a2219b531ace1f2b3f53f61a53348c0a9d882> (2024-11-08)

> x11/plasma5-plasma-workspace: add upstream patch ยท freebsd/freebsd-ports@f00a221

> to fix saving Plasma session on logout.