From a114971a67451e71fcfed801a49f3b86fa95138e Mon Sep 17 00:00:00 2001 From: Lily Foster Date: Sun, 22 Sep 2024 17:04:23 -0400 Subject: [PATCH] Fix race condition when getting login session If `org.freedesktop.login1.Manager.GetSessionByPID` is called after cosmic-session puts it in a new systemd scope, it will no longer find the login session as it would have been removed out of the login session scope. Most of the time, cosmic-greeter gets to that line before the scope is changed, but sometimes (particularly on slower systems or virtual machines) this race can be lost and lead to cosmic-greeter failing to start and preventing lockscreen from working. This commit solves that race condition by always checking the parent PID instead (which would be cosmic-session which is always present in the login session scope), and all POSIX-y systems should support using parent PID. --- src/logind.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/logind.rs b/src/logind.rs index 912e10e..44f24da 100644 --- a/src/logind.rs +++ b/src/logind.rs @@ -6,7 +6,7 @@ use logind_zbus::{ manager::{InhibitType, ManagerProxy}, session::SessionProxy, }; -use std::{any::TypeId, error::Error, os::fd::OwnedFd, process, sync::Arc}; +use std::{any::TypeId, error::Error, os::fd::OwnedFd, sync::Arc}; use tokio::time; use zbus::Connection; @@ -68,7 +68,7 @@ pub fn subscription() -> Subscription { pub async fn handler(msg_tx: &mut mpsc::Sender) -> Result<(), Box> { let connection = Connection::system().await?; let manager = ManagerProxy::new(&connection).await?; - let session_path = manager.get_session_by_PID(process::id()).await?; + let session_path = manager.get_session_by_PID(std::os::unix::process::parent_id()).await?; let session = SessionProxy::builder(&connection) .path(&session_path)? .build()