eminence / procfs

Rust library for reading the Linux procfs filesystem
Other
362 stars 106 forks source link

Don't hide process creation errors in all_processes #260

Closed tatref closed 1 year ago

tatref commented 1 year ago

This is followup of #188

Current implementation hide Process creation errors in all_processes, making this function nearly useless in a resource constrained environment. Errors are silently ignored, making look like only a few processes exist.

The fact that FDs are created for each process is not necessarily an issue, but users of the library must have a way to know if any operation worked as expected or not.

This PR shouldn't change much for users, because the Processes are wrapped in a Result either way

Example usage


let all_processes: Vec<Process> = procfs::process::all_processes()
    .unwrap()
    .filter_map(|p| match p {
        Ok(p) => Some(p),                              // happy path
        Err(e) => match e {
            procfs::ProcError::NotFound(_) => None,    // process vanished during iteration
            procfs::ProcError::Io(e, path) => None,    // can match on path to decide if we can continue
            x => {
                panic!("Can't read process {x:?}");    // some unknown error
            }
        },
    })
    .collect();
tatref commented 1 year ago

I rebased from master

eminence commented 1 year ago

Thanks, this change makes sense. As a followup TODO, some additional documentation about strategies to handle errors might be good (similar to the comment you made in the PR)