复现环境: - Ubuntu 24.04.3 LTS (noble), kernel 6.8.0-137-generic, amd64 (KVM 虚拟机) - systemd 255.4-1ubuntu8.17(apt 当前最新,无更新可用) - 单用户 lou,SSH 登录运行 tmux(3.4) + 长驻进程(pi coding agent) - loginctl enable-linger lou 已开启(Linger=yes 经 D-Bus 实测确认),KillUserProcesses=no(默认)
问题描述: 最后一个 SSH 会话断开时,logind 的垃圾回收(GC)错误地清理了 user 域: 1. [email protected] 的 main process (systemd --user) 被 SIGKILL: "Main process exited, code=killed, status=9/KILL",且没有先行的 "Stopping [email protected]" 日志(非正常停止流程) 2. [email protected] 被停止(StopWhenUnneeded=yes + [email protected] 的 Requires 连带) 3. user-1000.slice 被拆除 4. user 域内所有进程(tmux server、tmux 内运行的进程)全部被连带杀死 按 systemd 文档(man loginctl / man logind.conf),linger=true 时最后会话结束不应清理 user 域。 该问题同一天内自然复现 6 次,100% 命中(在无其他会话掩护时)。 复现步骤: 1. loginctl enable-linger lou 2. SSH 登录,systemd-run --scope --user tmux new -s pi(user 域 tmux),内部运行任意进程 3. 断开 SSH(最后一个会话) 4. 等待 1~3 分钟(sshd 检测 TCP 半开连接断开的时间) 5. 结果: [email protected] 被 SIGKILL,tmux 与其中进程全部被杀,tmux ls = no server running 日志证据: 正常对比(07:09:52,存在其他会话时,user 域存活): Aug 11 07:09:52 sshd: pam_unix(sshd:session): session closed for user lou Aug 11 07:09:52 systemd[1]: session-5664.scope: Deactivated successfully. Aug 11 07:09:52 systemd-logind: Session 5664 logged out. Waiting for processes to exit. Aug 11 07:09:52 systemd-logind: Removed session 5664. (结束——logind 未触碰 user 域) 异常(08:00:35,最后会话结束,user 域团灭): Aug 11 08:00:35 sshd: pam_unix(sshd:session): session closed for user lou Aug 11 08:00:35 systemd[1]: session-5183.scope: Deactivated successfully. Aug 11 08:00:35 systemd[1]: [email protected]: Main process exited, code=killed, status=9/KILL Aug 11 08:00:35 systemd[1]: [email protected]: Killing process 3165504 (pi) with signal SIGKILL. Aug 11 08:00:35 systemd[1]: [email protected]: Killing process 3175333 (python) with signal SIGKILL. Aug 11 08:00:35 systemd[1]: [email protected]: Failed to kill control group /user.slice/user-1000.slice/[email protected], ignoring: Invalid argument Aug 11 08:00:35 systemd[1]: [email protected]: Failed with result 'signal'. Aug 11 08:00:35 systemd[1]: [email protected]: Unit process 3165504 (pi) remains running after unit stopped. Aug 11 08:00:35 systemd-logind: Removed session 5183. Aug 11 08:00:35 systemd[1]: session-5715.scope: Deactivated successfully. Aug 11 08:00:35 systemd-logind: Session 5715 logged out. Waiting for processes to exit. Aug 11 08:00:35 systemd[1]: Stopping [email protected] Aug 11 08:00:35 systemd-logind: Removed session 5715. Aug 11 08:00:35 systemd[1]: Stopped [email protected] Aug 11 08:00:35 systemd[1]: Removed slice user-1000.slice 第二次复现(10:03:16,重启后 8.17 依然触发): Aug 11 10:03:16 sshd: pam_unix(sshd:session): session closed for user lou Aug 11 10:03:16 systemd-logind: Session 3 logged out. Waiting for processes to exit. Aug 11 10:03:16 systemd[1]: [email protected]: Main process exited, code=killed, status=9/KILL Aug 11 10:03:16 systemd[1]: [email protected]: Killing process 9797 (python) with signal SIGKILL. Aug 11 10:03:16 systemd[1]: [email protected]: Failed to kill control group ..., ignoring: Invalid argument Aug 11 10:03:16 systemd[1]: [email protected]: Unit process 9797 (python) remains running after unit stopped. Aug 11 10:03:16 systemd[1]: session-3.scope: Deactivated successfully. Aug 11 10:03:16 systemd[1]: Stopping [email protected] Aug 11 10:03:16 systemd-logind: Removed session 3. Aug 11 10:03:16 systemd[1]: Removed slice user-1000.slice 根因分析(v255 源码 src/login/logind-user.c): bool user_may_gc(User *u, bool drop_not_started) { if (u->sessions) return false; // 有会话不GC if (延迟窗口内) return false; if (user_check_linger_file(u) > 0 && user_unit_active(u)) return false; // linger保护:依赖 D-Bus 查询 unit 状态 return true; // GC → user_stop() → 停 user 域 } user_unit_active() 通过 D-Bus 查询 [email protected] / [email protected] / user-1000.slice 的 active 状态;查询失败(r<0)时仅记 debug 日志,然后当作"不活跃"处理, 导致 linger 保护静默失效 → GC 放行。该查询发生在"最后会话结束"的清理竞态窗口内, 偶发失败 → 行为随机(同一天 6 次触发,但相邻 4 次断开未触发,与竞态特征吻合)。 user_stop() → user_stop_service() 停止 [email protected] (StopWhenUnneeded=yes)→ [email protected] 因 Requires=user-runtime-dir@%i.service 连带停止 → KillMode=mixed 最终 SIGKILL 整个 user 域。 上游状态: - v256 同区域回归: issue #33488(gc_mode,PR #30884 引入),已在 v256.3 修复(PR #33786, 2024-07-19,含 "logind-user: take gc_mode into account when reporting user state" 等) - v255-stable 未包含这些修复;Ubuntu 24.04 的 255.4-1ubuntu8.17 仅有安全补丁,apt 无更新可用 影响: - 最后 SSH 会话断开时 user 域内所有进程(tmux/screen/长驻任务)被 SIGKILL,无警告 - 与文档相悖(linger=true 时 user manager 应保持运行) - 附带异常: "Failed to kill control group ... Invalid argument"(cgroup.kill EINVAL) Workaround(已验证生效): /etc/systemd/logind.conf.d/keep-user.conf: [Login] UserStopDelaySec=infinity systemctl restart systemd-logind 后生效(官方语义: user 服务首次登录后永不终止直到关机)。 期望: 将上游 v256.3 的相关修复(或等效 v255 版本: user_may_gc() 的 linger 保护不应依赖易失败的 D-Bus 查询)backport 到 Ubuntu 24.04 的 systemd 255.4-1ubuntu8.x。 -- You received this bug notification because you are a member of Ubuntu Bugs, which is subscribed to Ubuntu. https://bugs.launchpad.net/bugs/2163239 Title: systemd 255.4-1ubuntu8.17: logind GC kills [email protected] on last SSH session logout despite Linger=yes (regression fixed upstream in v256.3 PR #33786, please backport) To manage notifications about this bug go to: https://bugs.launchpad.net/ubuntu/+source/systemd/+bug/2163239/+subscriptions -- ubuntu-bugs mailing list [email protected] https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs
