Bug 464119

Summary: Systemd/logind sleep inhibitions inhibit both sleep and screen locking.
Product: [Plasma] Powerdevil Reporter: Victor J. Orlikowski <vjorlikowski>
Component: generalAssignee: Plasma Bugs List <plasma-bugs>
Status: RESOLVED FIXED    
Severity: normal CC: andygait, bugs.kde, jnerin, kde, me, nate, porkepix
Priority: NOR    
Version: 5.24.7   
Target Milestone: ---   
Platform: Neon   
OS: Linux   
Latest Commit: Version Fixed In: 5.27
Sentry Crash Report:

Description Victor J. Orlikowski 2023-01-10 21:09:54 UTC
SUMMARY
***
When attempting to force the system to stay awake while performing a background activity - but *not* lock the screen - PowerDevil disables both sleep *and* screen locking.

System was upgraded from KUbuntu 20.04 to KUbuntu 22.04.

I expect that this is an unintended consequence of this commit:
https://invent.kde.org/plasma/powerdevil/-/commit/b5dfb286a4d9b43ac74862218beb5dd7f58ea7a8
***


STEPS TO REPRODUCE
1. On a laptop (or other system that can suspend) - set up screen auto-lock, to occur after some number of minutes (1-5).
2. In a terminal, execute: systemd-inhibit --who="test sleep" --what="sleep" --why="testing sleep without screen lock" sleep infinity
3. The PowerDevil plasmoid will report that both sleep and screen locking are being blocked - and automatic screen locking will not take place (even though the requested inhibition should *only* prevent sleep, not automatic screen locking).


OBSERVED RESULT
Screen auto-lock does not lock, when systemd/logind inhibition requested is for "sleep" only, not "sleep:idle".

EXPECTED RESULT
Screen should auto-lock, after configured duration, when systemd/logind inhibition is requested for "sleep" only.
Screen should not auto-lock for an inhibition containing "idle".

SOFTWARE/OS VERSIONS
Windows: 
macOS: 
Linux/KDE Plasma: KUbuntu 22.04
(available in About System)
KDE Plasma Version: 5.24.7
KDE Frameworks Version: 5.92.0
Qt Version: 5.15.3

ADDITIONAL INFORMATION

This behavior *also* occurs outside of systemd/logind, by using the dbus interface.
The following python program causes the same misbehavior on the part of Powerdevil:

import time
from PyQt5.QtCore import QMetaType
from PyQt5.QtDBus import QDBusConnection, QDBusInterface, QDBusArgument
sessionbus = QDBusConnection.sessionBus()
iface = QDBusInterface("org.kde.Solid.PowerManagement.PolicyAgent", "/org/kde/Solid/PowerManagement/PolicyAgent", "org.kde.Solid.PowerManagement.PolicyAgent", sessionbus)
a1 = QDBusArgument(1, QMetaType.UInt)
a2 = QDBusArgument('test', QMetaType.QString)
a3 = QDBusArgument('test sleep', QMetaType.QString)
c = iface.call('AddInhibition', a1, a2, a3)
print(c.arguments())
print('Sleeping 15 minutes, unless interrupted...')
time.sleep(900)
Comment 1 Kai Uwe Broulik 2023-01-11 20:33:27 UTC
Policy "1" means "InterruptSession" which means suspending the system.

You might want to use "4" (which is change screen settings, which includes locking the screen) instead, or a combination of both (5). Generally this DBus interface isn't supposed to be used outside of Solid/KDE.

The label in battery monitor is misleading. PowerDevil functions as designed. It also doesn't post a logind inhibiton, it handles everything itself, so you won't see anything meaningful in systemd inhibit.
Comment 2 Kai Uwe Broulik 2023-01-11 20:38:51 UTC
Ah, wait, so your " systemd-inhibit --who="test sleep" --what="sleep" --why="testing sleep without screen lock" sleep infinity" call isn't working. Sorry, I was too focussed on the Python example. Re-opening.
Comment 3 Bug Janitor Service 2023-01-11 20:46:32 UTC
A possibly relevant merge request was started @ https://invent.kde.org/plasma/kscreenlocker/-/merge_requests/121
Comment 4 Victor J. Orlikowski 2023-01-11 22:00:11 UTC
Right - I was trying to make the point that the python code wasn't doing *quite* what I expected, either.

Basis for this issue...

I often have to ssh into remote systems while on battery power, but might have to be talking to a colleague (I work in higher ed).
I have, in my ~/.ssh/config:

LocalCommand ~/scripts/ssh_prevent_linux_idle_sleep.sh

Which looks like this:

#!/bin/sh
# Prevent a Linux box from sleeping while an SSH session is live.
#
# This is intended to be run using the LocalCommand function of ssh.
#
# It is recommended that this command only be used for hosts
# from which you do not desire to be disconnected, in the interest
# of power savings.

SSH_PID=$PPID
SSH_STARTTIME="$(ps -p $PPID -o lstart | grep -v '^.*STARTED')"

# Start a background child process that:
# 1) Starts "sleep infinity" in the background under systemd-inhibit
# 2) Monitors for the exit of the ssh parent process
# 3) Kills the "sleep" it started upon ssh parent exit
#
# We avoid a race condition that might prevent the systemd-inhibit process
# from being killed by starting it within the child.
(
systemd-inhibit --who="ssh" --what="sleep" --why="connected to remote host" sleep infinity &
INHIBITOR_PID=$!
while true
do
    checkstarttime="$(ps -p $SSH_PID -o lstart | grep -v '^.*STARTED')"
    rc=$?
    if [ ! \( $rc -eq 0 -a "$checkstarttime" = "$SSH_STARTTIME" \) ]; then
        kill $INHIBITOR_PID
        exit 0
    fi
    sleep 15
done
) &

In that case - and in the version of KDE present in KUbuntu 20.04 - what I want is for the system to *not* suspend, but be able to auto-lock (and, preferably, dim +/- turn off the screen, after the appropriate duration).

In the version of KDE in KUbuntu 22.04, the above script keeps the machine from suspending - and from auto-locking, and from dimming or powering off the screen, if I'm ssh'd into a host to which that LocalCommand applies.
Comment 5 Kai Uwe Broulik 2023-01-25 12:41:10 UTC
Git commit 9aee819f3d1b234892700ccbddfa1f5bd6f10e9f by Kai Uwe Broulik.
Committed on 25/01/2023 at 12:38.
Pushed by broulik into branch 'master'.

Only inhibit screen locker on "ChangeScreenSettings"

This is the policy for e.g. dimming and turning off the screen,
i.e. "user can't see stuff anymore" which includes locking the screen.

"InterruptSession" is for suspend where the computer will not
continue its current tasks. The screen can still lock when I am
copying files or burning a CD.

M  +1    -1    powermanagement_inhibition.cpp

https://invent.kde.org/plasma/kscreenlocker/commit/9aee819f3d1b234892700ccbddfa1f5bd6f10e9f
Comment 6 Nate Graham 2023-01-25 14:33:28 UTC
Git commit cb4844aa8c906665feec027dccc21052d1689883 by Nate Graham, on behalf of Kai Uwe Broulik.
Committed on 25/01/2023 at 14:33.
Pushed by ngraham into branch 'cherry-pick-9aee819f'.

Only inhibit screen locker on "ChangeScreenSettings"

This is the policy for e.g. dimming and turning off the screen,
i.e. "user can't see stuff anymore" which includes locking the screen.

"InterruptSession" is for suspend where the computer will not
continue its current tasks. The screen can still lock when I am
copying files or burning a CD.


(cherry picked from commit 9aee819f3d1b234892700ccbddfa1f5bd6f10e9f)

M  +1    -1    powermanagement_inhibition.cpp

https://invent.kde.org/plasma/kscreenlocker/commit/cb4844aa8c906665feec027dccc21052d1689883
Comment 7 Nate Graham 2023-02-17 20:40:30 UTC
*** Bug 465859 has been marked as a duplicate of this bug. ***