-
-
Notifications
You must be signed in to change notification settings - Fork 326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Processes started after program using sysinfo
aren't added to processes list, sometimes
#1339
Comments
Did you give a try to |
|
Weird. And when you list all processes, do you see the ones you're looking for? |
Nope, not unless they were open before I started checking (Just did this and then searched in my terminal) for (_pid, process) in sys.processes() {
trace!("{}", process.name().to_str().unwrap());
} |
Your issue is very weird. It is tested in different tests like here: the binary is run then only we refresh processes and it's working on mac as well. I'm really confused about what's going wrong on your side... |
I cloned
It may be worth noting that I'm on macOS 15.1 Beta (24B5024e), though I read through the release notes and didn't see any indication that this is a known issue |
This one is flaky (on mac). For some reasons, sometimes it cannot retrieve process environment. No clue why. It's part of mac's "magic". ^^' But if all tests pass, it seems to mean that the problem might not be directly in |
Here's pretty much all the code that currently is actually used in my program: // main.rs
use std::{error::Error, process::ExitCode};
use discord_rich_presence::{DiscordIpc, DiscordIpcClient};
use log::{debug, error, trace};
use sysinfo::{ProcessRefreshKind, ProcessesToUpdate};
const CLIENT_ID: &str = "1273805325954187386";
fn try_main() -> Result<(), Box<dyn Error>> {
env_logger::init();
debug!("Initialized logging");
if !sysinfo::IS_SUPPORTED_SYSTEM {
return Err("This system is not supported by sysinfo, a crucial dependency".into());
}
let mut sys = sysinfo::System::new();
let mut discord_client = None;
loop {
sys.refresh_processes_specifics(ProcessesToUpdate::All, ProcessRefreshKind::new());
let discord_is_open = sys
.processes_by_exact_name("Discord".as_ref())
.next()
.is_some();
let apple_music_is_open = sys
.processes_by_exact_name("Music".as_ref())
.next()
.is_some();
if discord_is_open && apple_music_is_open {
trace!("Time to work!");
if discord_client.is_none() {
discord_client = Some(DiscordIpcClient::new(CLIENT_ID)?);
discord_client.as_mut().unwrap().connect()?;
debug!("Connected Discord client");
}
// TODO
} else {
trace!("Idling...");
}
}
}
fn main() -> ExitCode {
if let Err(err) = try_main() {
error!("{err}");
return ExitCode::FAILURE;
}
ExitCode::SUCCESS // Impossible to reach?
} # Cargo.toml
[package]
name = "apple-music-rich-presence"
version = "0.1.0"
edition = "2021"
[dependencies]
discord-rich-presence = { git = "https://github.com/vionya/discord-rich-presence.git", rev = "5620e8901566290a583f9354205686b18628ba1b", version = "0.2.4" }
env_logger = "0.11.5"
log = "0.4.22"
sysinfo = { version = "0.31.2", default-features = false, features = ["system"] } Some stuff was omitted since it's old and not actually used Sorry for the massive comment! You would think most of this couldn't be causing this issue but, who knows |
Your code looks good. Then let's try taking a different approach: if you ask for the PIDs when you start the program and then see what information |
I switched out for (pid, process) in sys.processes() {
trace!("{pid}: {process:?}");
} Launched Discord and Apple Music then the program. Found those two in the logs:
Another very strange thing I noticed is that programs (such as "VisualizerService") which are launched alongside Apple Music, are detected by
|
Definitely strange. Don't hesitate to ask if you have any question. But I'm glad that a new bug was found. :D |
A revelation! This code fixed the issue: sleep(Duration::from_secs(5)); ... So the problem is that the loop is too fast when not limited? |
Wait really? If true then it's really not great. T_T
|
Did a little bit of testing, I was able to get as low as 1 second to consistently detect processes correctly, and as low as 100 milliseconds to inconsistently detect processes correctly |
But no process with the given PID you're looking for is there right? If so, I suppose the |
If there's some time between refreshes, the processes I'm looking for do appear. |
So it seems like the system API is not refreshing very quickly. How strange. |
Describe the bug
It seems that if a program is using
sysinfo
to check the processes currently running and a new process starts later, it won't be added to the list of processes despite refreshing. Inexplicably, though, while this happens most of the time, there are some times where a newly launched program will be detected.sysinfo
version: 0.31.2sysinfo
features:system
To Reproduce
The text was updated successfully, but these errors were encountered: