From 67cb40a0b238f42e0710e410d2f22be5adcd22dd Mon Sep 17 00:00:00 2001 From: William Volin Date: Sun, 6 Nov 2022 14:27:16 -0700 Subject: [PATCH] Changes Process.with_name to sort pids by id (#589) * Changes Process.with_name to sort pids by id Prior to selecting max key by start time, this change sorts the keys to ascending to take the higher PID in case of all records having an equal `start_time()` see: https://doc.rust-lang.org/nightly/src/core/iter/traits/iterator.rs.html#3011-3012 This is to fix a case where attempting to attach to steam proton processes on linux may return two PIDs with the same start_time, potentially selecting the wrong pid indefinitely. --- crates/livesplit-auto-splitting/src/process.rs | 7 +++++-- src/settings/mod.rs | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/livesplit-auto-splitting/src/process.rs b/crates/livesplit-auto-splitting/src/process.rs index ee6e0789..f6ce6252 100644 --- a/crates/livesplit-auto-splitting/src/process.rs +++ b/crates/livesplit-auto-splitting/src/process.rs @@ -44,10 +44,13 @@ impl Process { process_list.refresh(); let processes = process_list.processes_by_name(name); - // Use the process that was started the most recently, it's more + // Sorts the processes (asc) by numeric pid, to allow max_by_key to + // select the higher pid in case all records are equally maximum; otherwise + // use the process that was started the most recently, it's more // predictable for the user. + let pid = processes - .max_by_key(|p| p.start_time()) + .max_by_key(|p| (p.start_time(), p.pid().as_u32())) .context(ProcessDoesntExist)? .pid() .as_u32() as Pid; diff --git a/src/settings/mod.rs b/src/settings/mod.rs index db978b78..17d46c6e 100644 --- a/src/settings/mod.rs +++ b/src/settings/mod.rs @@ -20,5 +20,5 @@ pub use self::{ image::{CachedImageId, Image, ImageData}, semantic_color::SemanticColor, settings_description::SettingsDescription, - value::{Error as ValueError, Result as ValueResult, Value, ColumnKind}, + value::{ColumnKind, Error as ValueError, Result as ValueResult, Value}, };