Skip to content

Commit

Permalink
Changes Process.with_name to sort pids by id (#589)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Eein authored Nov 6, 2022
1 parent 45ccd90 commit 67cb40a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 5 additions & 2 deletions crates/livesplit-auto-splitting/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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 67cb40a

Please sign in to comment.