Skip to content

Commit

Permalink
Changes Process.with_name to sort pids by id
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Eein committed Oct 31, 2022
1 parent 2298fc2 commit 4412da1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions crates/livesplit-auto-splitting/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,17 @@ 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

let mut procs: Vec<&sysinfo::Process> = processes.map(|p| p).collect();

procs.sort_unstable_by_key(|p| p.pid().as_u32());

let pid = procs
.iter()
.max_by_key(|p| p.start_time())
.context(ProcessDoesntExist)?
.pid()
Expand Down
2 changes: 1 addition & 1 deletion src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

0 comments on commit 4412da1

Please sign in to comment.