Skip to content
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

Use process groups to avoid delay-loops when waiting for child PIDs #104

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Porges
Copy link

@Porges Porges commented Sep 5, 2023

This is a bit of a proof-of-concept that it works:

  1. invoke setsid in pre-exec when spawning a new process
  2. when waiting for a process, wait for its pgid instead of pid

The loop inside wait_for_tracees is a bit of a lie, really we expect there to be one pgid so we are only ever going to visit the first tracee and get the pgid there, but doing it this way involved less code churn.

This assumes that no tracee is going to mess with its own process group. If they do, then we'd need to go back to a loop over multiple process groups like before… perhaps:

  1. collect PGs of all tracees
  2. if there's just one, block on it
  3. otherwise fall back to the existing loop

(Apologies for the messy diff but it got rustfmt'd.)

Comment on lines +400 to +403
cmd.pre_exec(|| match setsid() {
Ok(_pid) => Ok(()),
Err(e) => Err(io::Error::from(e)),
});
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First change: setsid when spawning

Comment on lines +441 to +445
let pgid = nix::unistd::getpgid(Some(pid))
.expect("tracee should have a process group due to pre-exec");

match wait::waitpid(pid, Some(flag)) {
// negative pid means treat it as a pgid
match wait::waitpid(Pid::from_raw(-pgid.as_raw()), Some(flag)) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second change: get PGID for tracee and wait on that instead.

/// Known tracees, and their state.
tracees: BTreeMap<i32, State>,
}

const DEFAULT_POLL_DELAY: Duration = Duration::from_millis(100);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Poll delays removed entirely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant