Skip to content

fix(lock): KDE 解锁后清理残留的锁屏界面进程,避免再次锁屏变成透明锁屏 - #251

Merged
VallierDev merged 6 commits into
VallierDev:mainfrom
wzk4work:fix/kde-stale-greeter
Nov 19, 2026
Merged

VallierDev merged 6 commits into
VallierDev:mainfrom
wzk4work:fix/kde-stale-greeter

Conversation

@wzk4work

@wzk4work wzk4work commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

现象

KDE X11(Debian 13 / Plasma 6.3,带指纹的笔记本)上,远程用密码解锁后很快再次锁屏,会得到一个“透明锁屏”:键盘鼠标被抓住,但桌面内容仍然可见,远控端和本机屏幕都一样,指纹灯常亮。此时 ksmserver 日志为:

QProcess::start: Process is already running

原因

对照 kscreenlocker v6.3.5 源码:

  • 密码通过后 greeter 打印 Unlocked,ksld 解锁,greeter 退出事件循环。
  • ~PamAuthenticator 等待指纹 PAM 线程,而 pam_fprintd.so timeout=-1 只有读到手指才返回,取消只对 PAM 会话回调有效。远程用户按不到指纹,进程一直留着。
  • 下一次锁屏时 ksld 抓住输入并显示锁窗口,但 QProcess::start 因旧进程仍在而不启动新的 greeter。
  • 旧进程最终以 0 退出时,ksld 把它当作一次正常解锁。

这是 kscreenlocker 的问题,本机日志里早于安装本项目就出现过;远控只是让它容易触发。

处理

会话由锁定变为未锁定时,记下本用户的 kscreenlocker_greet(pid + 启动时间),3 秒后仍是同一进程就 SIGKILL:

  • greeter 的 SIGTERM 处理在已退出的事件循环里,不起作用,所以用 SIGKILL。
  • 未锁定时 ksld 忽略这次退出(s_lockProcessRequestedExit),不会重新锁屏。
  • 如果用户已经再次锁屏,ksld 按 greeter 崩溃处理,重启一个正常的锁屏界面。
  • 用启动时间区分 pid 复用;只处理本用户的进程;没有 greeter 的桌面上只是扫一遍 /proc。

验证

  • cargo test --locked:397 通过。
  • 实机(ThinkPad T14s,Debian 13,Plasma 6.3 X11):远程密码解锁后再锁屏,锁屏界面正常显示。
  • 未验证:KDE Wayland 会话、其他发行版。

追加:自动解锁停在黑屏

现象

远控端开启“自动解锁被控端”后连接锁屏中的主机,画面全黑,只能看到鼠标光标。主机日志为 Auto unlock result: Failed,会话此后一直是锁定状态。

原因

同一个 greeter 挂起,从另一条路径触发(kscreenlocker v6.3.5 ksldapp.cpp):

  • ksld 收到 logind 的 Unlock(requestUnlock)时只置 s_logindExit 并给 greeter 发 SIGTERM,真正的解锁在 QProcess::finished 里。
  • greeter 退出事件循环、关掉自己的窗口,然后卡在 pam_fprintd.so timeout=-1 的指纹线程上,进程不结束。
  • ksld 仍认为锁定,只剩它自己的黑色锁窗口;光标是远控端自己画的。
  • 原有的清理只在观察到“锁定 → 未锁定”之后触发,这条路径上会话没有解开,所以不会执行。

处理

unlock_with 发出 unlock-session 2 秒后会话仍锁定时,SIGKILL 该 logind 会话里在请求之前启动的 greeter(沿用已有的会话和启动时间筛选)。ksld 已置 s_logindExit,进程结束即完成解锁。正常的 greeter 在几毫秒内退出,不受影响;GNOME 等没有 kscreenlocker_greet 的桌面只是多扫一遍 /proc。

验证

  • cargo test --locked:402 通过。
  • 实机(同上,Plasma 6.3.5 X11):锁屏状态下连续三次自动解锁,日志均为 Lock screen greeter ignored the unlock request; removed: count=1 → Desktop lock state: unlocked → Auto unlock result: Success,约 2 秒进入桌面;断开后自动锁屏每次都启动了新的 greeter。
  • 未验证:KDE Wayland 会话、没有指纹的 KDE 主机上的自动解锁耗时。

- 现象:KDE X11 上远程用密码解锁后马上再锁屏,输入被抓住但桌面内容可见,
  ksmserver 日志为 "QProcess::start: Process is already running"
- 原因(对照 kscreenlocker v6.3.5):密码通过后 greeter 打印 "Unlocked" 并退出事件循环,
  ~PamAuthenticator 等指纹 PAM 线程,而 pam_fprintd.so timeout=-1 只有读到手指才返回;
  远程用户按不到指纹,进程一直留着,下一次锁屏起不了新的 greeter,
  旧进程以 0 退出时 ksld 还会把它当成解锁
- 处理:会话由锁定变为未锁定时记下本用户的 kscreenlocker_greet(pid + 启动时间),
  3 秒后仍是同一进程就 SIGKILL。greeter 的 SIGTERM 处理在已退出的事件循环里,不起作用;
  未锁定时 ksld 忽略这次退出,已再次锁屏时 ksld 按 greeter 崩溃重启一个正常的锁屏界面
- 验证:Debian 13 / Plasma 6.3 X11(ThinkPad T14s,带指纹)上远程解锁后再锁屏恢复正常

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@VallierDev

Copy link
Copy Markdown
Owner

感谢详细的根因分析。已审阅 diff:仅在 锁定→解锁 时扫描本用户 kscreenlocker_g,3 秒后用 pid+starttime 校验再 SIGKILL,stat 解析(第 22 字段、comm 含括号)与测试都对得上,没有隐私或协议方面的问题。已批准 CI 并请 Codex 审查,结果返回、CI 通过后再合并。

已请 Codex 审查。@codex review

🤖 Generated with Claude Code

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T18:06:29.502435Z 169db0b Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7160a892f7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/session_lock/stale_greeter.rs Outdated
Comment on lines +43 to +44
if entry.metadata().ok()?.uid() != uid { return None; }
Some((pid, start_time(pid)?))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Scope greeter cleanup to the unlocked graphical session

If the same UID has two graphical sessions and the other session remains locked, unlocking the session monitored by logind::locked() (User.Display) also selects the other session's greeter here, since only UID and process name are checked. That healthy greeter can retain the same start time beyond the three-second grace period and therefore receive SIGKILL, terminating its authentication UI even though its session never unlocked. Filter candidates by the unlocked logind session/display rather than treating every same-user greeter as stale.

AGENTS.md reference: AGENTS.md:L272-L274

Useful? React with 👍 / 👎.

@VallierDev

Copy link
Copy Markdown
Owner

Codex 的 P2(stale_greeter.rs:44)核实成立:候选只按 UID + 进程名筛选,同一用户若有两个图形会话、另一个仍处于锁定,解锁被监视的会话时会把另一个会话里正常的 greeter 一并 SIGKILL。建议把候选限定到刚解锁的那个 logind 会话,例如比对 /proc/<pid>/sessionid(audit session id,与 logind 会话号一致)或 /proc/<pid>/cgroup 里的 session-<id>.scope,与 logind::locked() 监视的会话对齐;读不到时宁可不杀。改完后补一条测试,我们再请 Codex 复审。

🤖 Generated with Claude Code

候选原先只按 UID + 进程名筛选:同一用户有两个图形会话、另一个仍锁定时,
解锁被监视的会话会把另一个会话里正常的 greeter 一并杀掉(Codex P2)。

现在只选属于 User.Display 会话的 greeter:先看 cgroup 的
session-<id>.scope,没有时看进程环境里的 XDG_SESSION_ID;会话判断不出来就不动。

没有用 /proc/<pid>/sessionid:systemd 托管的 Plasma(实测 6.3)里 ksmserver
和 greeter 跑在 user@<uid>.service 下,cgroup 是 plasma-ksmserver.service,
sessionid 是用户管理器自己的会话(实测 4),而图形会话是 3,两者对不上。

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@wzk4work

Copy link
Copy Markdown
Contributor Author

已按建议把候选限定到刚解锁的 logind 会话(cbe7ed1),并补了测试;会话判断不出来时不杀。

有一点和建议不同:没有用 /proc/<pid>/sessionid。systemd 托管的 Plasma(实测 6.3)里 ksmserver 和 greeter 跑在 user@<uid>.service 下,cgroup 是 plasma-ksmserver.service,sessionid 是用户管理器自己的会话(实测 4),而图形会话(User.Display)是 3,对不上。所以先看 cgroup 的 session-<id>.scope,没有时用进程环境里的 XDG_SESSION_ID。

@VallierDev

Copy link
Copy Markdown
Owner

cbe7ed1 看过:候选限定为刚解锁的 logind 会话(cgroup session-<id>.scope 优先,回退进程环境的 XDG_SESSION_ID,都判断不了就不动),测试覆盖了另一会话、前缀不误匹配和未知会话,解释了为何不用 /proc/<pid>/sessionid,合理。已批准 CI 并请 Codex 复审。

@codex review

🤖 Generated with Claude Code

上一个提交实机无效:kscreenlocker_greet 启动时 prctl(PR_SET_DUMPABLE, 0),
/proc/<pid>/environ 读不到,会话判断为未知,残留 greeter 没被清理,
透明锁屏复现(ksmserver 再次报 QProcess::start: Process is already running)。

greeter 自己判断不出会话时,改看启动它的父进程(ksmserver / kwin_wayland 里的
ksld,ppid 取自 /proc/<pid>/stat 第 4 字段),它与 greeter 同属一个会话且可读。
父进程也判断不出来时仍然不动。

验证:三个改名为 kscreenlocker_g 的不可读进程,父进程分别在会话 3、会话 7、
无 XDG_SESSION_ID;监视会话 3 时只有第一个被清理。

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: cbe7ed15b2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@wzk4work

Copy link
Copy Markdown
Contributor Author

补充:cbe7ed1 实机无效。kscreenlocker_greet 启动时 prctl(PR_SET_DUMPABLE, 0),/proc/<pid>/environ 读不到,会话被判为未知而跳过。2c83c93 改为 greeter 自己判断不出时看父进程(ksmserver,ppid 取自 stat 第 4 字段)。已在 Plasma 6.3 X11 实机验证:连续三次远程解锁后 3 秒清理残留 greeter,再锁屏正常,ksmserver 不再报 Process is already running。

@VallierDev

Copy link
Copy Markdown
Owner

2c83c93 看过:greeter 设了不可 dump 读不到 environ 时,回退看父进程(ksmserver/kwin)所属会话,仍判断不出就不动,并有 Plasma 6.3 X11 实机验证,合理。CI 失败是 Ubuntu 软件源超时,已重跑。

@codex review

🤖 Generated with Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2c83c93737

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/session_lock/stale_greeter.rs Outdated
Comment on lines +94 to +96
let spawned = std::thread::Builder::new().name("uu-greeter-reap".into()).spawn(|| {
let Some(session) = super::display_session() else { return };
let seen = greeters(&session);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Tie greeter candidates to the observed unlock

If the same session is locked again before this background display_session() query and /proc scan finish, seen can contain the healthy greeter created for the new lock, even when the previous greeter exited normally. That new process survives the grace period because authentication is still pending, then receives SIGKILL, unnecessarily interrupting the authentication UI. A controlled probe with a same-session, non-dumpable test greeter started after reap_after_unlock() but before discovery completed reproduced this kill. Record candidates, or an eligibility start-time cutoff, at the unlock notification so greeters created for subsequent locks cannot be selected.

AGENTS.md reference: AGENTS.md:L274-L275

Useful? React with 👍 / 👎.

@VallierDev

Copy link
Copy Markdown
Owner

2c83c93 的父进程回退和实机验证都看过了。Codex 新的 P2 核实成立:候选是在后台 display_session() 查询和 /proc 扫描完成时才收集的,若这段时间内同一会话又被锁上,新锁屏对应的正常 greeter 也会进入候选,3 秒后仍在认证中就会被 SIGKILL。建议在收到解锁通知时记下截止时间(如当时的系统启动后 jiffies,与 stat 第 22 字段同单位),只把 starttime 早于该截止点的 greeter 列为候选;补一条「解锁后新启动的 greeter 不被选中」的测试,我们再请 Codex 复审。

🤖 Generated with Claude Code

候选是在后台线程查完会话、扫完 /proc 之后才收集的。这段时间里同一会话若又被
锁上,新锁屏对应的正常 greeter 也会进入候选,3 秒后仍在认证就被杀掉(Codex P2)。

收到解锁通知时在调用线程上记下时刻(CLOCK_BOOTTIME,换算成 stat 第 22 字段的
时钟计数),只把启动时间早于该时刻的 greeter 列为候选;取不到时刻就不动。

验证:会话查询人为延迟 2 秒,查询前后各起一个同会话、不可读的测试 greeter,
只有解锁前启动的那个被清理。

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@wzk4work

Copy link
Copy Markdown
Contributor Author

已按建议修改(08d4397):收到解锁通知时在调用线程上记下时刻(CLOCK_BOOTTIME 换算成 stat 第 22 字段的时钟计数),只把启动时间早于该时刻的 greeter 列为候选,取不到时刻就不动。补了「解锁后新启动的 greeter 不被选中」的测试,以及该时刻与 /proc 启动时间同一时钟的测试。

验证:会话查询人为延迟 2 秒,查询前后各起一个同会话、不可读的测试 greeter,只有解锁前启动的被清理。Plasma 6.3 X11 实机复测:连续三次远程解锁后 3 秒清理残留 greeter,再锁屏正常,ksmserver 不再报 Process is already running。

@VallierDev

Copy link
Copy Markdown
Owner

08d4397 看过:解锁时在调用线程记 CLOCK_BOOTTIME 截止点、只收早于它的 greeter,并补了定序测试与实机复测,思路正确。请 Codex 复审。

@codex review

🤖 Generated with Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 08d43974d6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/session_lock/stale_greeter.rs Outdated
Comment on lines +110 to +111
let Some(session) = super::display_session() else { return };
let seen = greeters(&session, unlocked_at);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Carry the observed session ID into the cleanup worker

If User.Display changes from the just-unlocked session A to an already-locked session B while this worker is queued or resolving the display—for example, A logs out while the host remains running—this lookup selects B rather than the session whose LockedHint produced the transition in logind::locked(). B's healthy greeter predates unlocked_at, so both the cutoff and belongs_to accept it, and it is killed after three seconds. A gated probe against the current module reproduced this by changing the session resolver from A to B before releasing discovery. Carry the observed session ID along with the lock-state snapshot instead of looking it up again here.

AGENTS.md reference: AGENTS.md:L274-L274

Useful? React with 👍 / 👎.

ksld 收到 logind 的 Unlock 只给 greeter 发 SIGTERM,等它退出才解锁
(kscreenlocker v6.3.5 ksldapp.cpp requestUnlock / QProcess::finished)。
pam_fprintd.so timeout=-1 时 greeter 退出事件循环后卡在指纹线程上,
会话一直锁定,远控端只看到黑屏和光标,自动解锁返回 Failed。

unlock-session 发出 2 秒后会话仍锁定时,SIGKILL 该会话在请求之前
启动的 greeter;ksld 已置 s_logindExit,进程结束即完成解锁。

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@VallierDev

Copy link
Copy Markdown
Owner

看到 1463a3e:自动解锁 2 秒后会话仍锁定时,SIGKILL 请求前启动的 greeter(引用 kscreenlocker requestUnlock / QProcess::finished 的行为),复用了截止点与会话过滤,思路清楚。

另外 08d4397 的 Codex 复审还有一条 P2 尚未处理,核实成立:清理线程里重新查询 display_session()(stale_greeter.rs)——若排队/查询期间 User.Display 从刚解锁的会话 A 变成另一个仍锁定的会话 B(如 A 注销而服务仍在运行),会选中 B,而 B 的正常 greeter 早于截止点、也属于该会话,3 秒后会被误杀。建议在 logind::locked() 观察到解锁时就把该会话 ID 和截止点一起带进清理线程,不再重新查询;补一条对应测试。改完后连同 1463a3e 一起请 Codex 复审。

🤖 Generated with Claude Code

清理线程原先自己查询 User.Display。排队或查询期间图形会话若从刚解锁的
A 变成仍锁定的 B(如 A 注销而服务仍在运行),会选中 B,B 的正常
greeter 早于截止点,3 秒后被误杀。

logind 快照现在把会话 ID 和 LockedHint 一起返回,LockState 记住状态
来自哪个会话;只有同一个会话先锁定、后未锁定才算解锁,并把该会话 ID
连同截止点交给清理线程。图形会话从锁定的 A 换成未锁定的 B 不再触发清理。

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@wzk4work

Copy link
Copy Markdown
Contributor Author

已按建议修改(169db0b):logind 快照把会话 ID 和 LockedHint 一起返回,观察到解锁时把该会话 ID 连同截止点交给清理线程,线程里不再查询 User.Display。

比建议多了一点:只有同一个会话先被看到锁定、再被看到未锁定才算解锁。图形会话从锁定的 A 直接换成未锁定的 B 时不再触发清理(之前会被当成一次解锁)。补了对应测试。

自动解锁路径(1463a3e)从发出 unlock-session 到清理用的是同一个会话 ID,没有这个问题,未改。

验证:cargo test --locked 403 通过;隔离 D-Bus 的监视测试(含 Display A→B 切换)通过。Plasma 6.3.5 X11 实机:两次自动解锁均 Auto unlock result: Success;两次手动密码解锁后 3 秒清理残留 greeter,再锁屏正常,ksmserver 不再报 Process is already running。

@VallierDev

Copy link
Copy Markdown
Owner

169db0b 看过:解锁时把 logind 快照中的会话 ID 连同截止点交给清理线程、线程内不再查询 User.Display,并要求同一会话先锁定后解锁才算解锁(A 锁定→B 未锁定不触发),补了测试与实机复测,处理了上条 P2。请 Codex 复审 1463a3e + 169db0b。

@codex review

🤖 Generated with Claude Code

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 169db0bb1b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@VallierDev
VallierDev merged commit 22882cb into VallierDev:main Nov 19, 2026
6 checks passed
@VallierDev

Copy link
Copy Markdown
Owner

Codex 复审 169db0b 无问题、CI 全绿,已 squash 合并,将随下个版本发布。感谢 @wzk4work 的修复和 Plasma 实机复测!

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants