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

refactor: remove child ownership from Exit filter #207

Merged
merged 5 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions examples/kqueue-process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,36 @@
target_os = "dragonfly",
))]
fn main() -> std::io::Result<()> {
use std::process::Command;
use std::{num::NonZeroI32, process::Command};

use async_io::os::kqueue::{Exit, Filter};
use futures_lite::future;

future::block_on(async {
// Spawn a process.
let process = Command::new("sleep")
let mut process = Command::new("sleep")
.arg("3")
.spawn()
.expect("failed to spawn process");

// Wrap the process in an `Async` object that waits for it to exit.
let process = Filter::new(Exit::new(process))?;
let process_handle = unsafe {
Filter::new(Exit::from_pid(
NonZeroI32::new(process.id().try_into().expect("invalid process pid"))
.expect("non zero pid"),
))?
};
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer keeping the example simple.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sgtm 610773a


// Wait for the process to exit.
process.ready().await?;
process_handle.ready().await?;

println!(
"Process exit code {:?}",
process
.try_wait()
.expect("error while waiting process")
.expect("process did not exit yet")
);
Ok(())
})
}
Expand Down
19 changes: 16 additions & 3 deletions src/os/kqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::Async;

use std::future::Future;
use std::io::{Error, Result};
use std::num::NonZeroI32;
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, OwnedFd, RawFd};
use std::pin::Pin;
use std::process::Child;
Expand Down Expand Up @@ -238,18 +239,30 @@ impl Queueable for Signal {}
/// When registered into [`Async`](crate::Async) via [`with_filter`](AsyncKqueueExt::with_filter),
/// it will return a [`readable`](crate::Async::readable) event when the child process exits.
#[derive(Debug)]
pub struct Exit(Option<Child>);
pub struct Exit(NonZeroI32);

impl Exit {
/// Create a new `Exit` object.
pub fn new(child: Child) -> Self {
Self(Some(child))
Self(
NonZeroI32::new(child.id().try_into().expect("unable to parse pid"))
.expect("cannot register pid with zero value"),
)
}

/// Create a new `Exit` object from a PID.
///
/// # Safety
///
/// The PID must be tied to an actual child process.
pub unsafe fn from_pid(pid: NonZeroI32) -> Self {
Self(pid)
}
}

impl QueueableSealed for Exit {
fn registration(&mut self) -> Registration {
Registration::Process(self.0.take().expect("Cannot reregister child"))
Registration::Process(self.0)
}
}
impl Queueable for Exit {}
Expand Down
18 changes: 9 additions & 9 deletions src/reactor/kqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use polling::{Event, PollMode, Poller};

use std::fmt;
use std::io::Result;
use std::num::NonZeroI32;
use std::os::unix::io::{AsRawFd, BorrowedFd, RawFd};
use std::process::Child;

/// The raw registration into the reactor.
///
Expand All @@ -27,8 +27,8 @@ pub enum Registration {
/// Raw signal number for signal delivery.
Signal(Signal),

/// Process for process termination.
Process(Child),
/// Pid for process termination.
Process(NonZeroI32),
}

impl fmt::Debug for Registration {
Expand Down Expand Up @@ -62,8 +62,8 @@ impl Registration {
Self::Signal(signal) => {
poller.add_filter(PollSignal(signal.0), token, PollMode::Oneshot)
}
Self::Process(process) => poller.add_filter(
unsafe { Process::new(process, ProcessOps::Exit) },
Self::Process(pid) => poller.add_filter(
unsafe { Process::from_pid(*pid, ProcessOps::Exit) },
token,
PollMode::Oneshot,
),
Expand All @@ -82,8 +82,8 @@ impl Registration {
Self::Signal(signal) => {
poller.modify_filter(PollSignal(signal.0), interest.key, PollMode::Oneshot)
}
Self::Process(process) => poller.modify_filter(
unsafe { Process::new(process, ProcessOps::Exit) },
Self::Process(pid) => poller.modify_filter(
unsafe { Process::from_pid(*pid, ProcessOps::Exit) },
interest.key,
PollMode::Oneshot,
),
Expand All @@ -100,8 +100,8 @@ impl Registration {
poller.delete(fd)
}
Self::Signal(signal) => poller.delete_filter(PollSignal(signal.0)),
Self::Process(process) => {
poller.delete_filter(unsafe { Process::new(process, ProcessOps::Exit) })
Self::Process(pid) => {
poller.delete_filter(unsafe { Process::from_pid(*pid, ProcessOps::Exit) })
}
}
}
Expand Down
Loading