Skip to content

Commit 9713083

Browse files
authored
refactor: remove child ownership from Exit filter (#207)
Pid is used instead
1 parent 7d29a93 commit 9713083

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

src/os/kqueue.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::Async;
77

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

243244
impl Exit {
244245
/// Create a new `Exit` object.
245246
pub fn new(child: Child) -> Self {
246-
Self(Some(child))
247+
Self(
248+
NonZeroI32::new(child.id().try_into().expect("unable to parse pid"))
249+
.expect("cannot register pid with zero value"),
250+
)
251+
}
252+
253+
/// Create a new `Exit` object from a PID.
254+
///
255+
/// # Safety
256+
///
257+
/// The PID must be tied to an actual child process.
258+
pub unsafe fn from_pid(pid: NonZeroI32) -> Self {
259+
Self(pid)
247260
}
248261
}
249262

250263
impl QueueableSealed for Exit {
251264
fn registration(&mut self) -> Registration {
252-
Registration::Process(self.0.take().expect("Cannot reregister child"))
265+
Registration::Process(self.0)
253266
}
254267
}
255268
impl Queueable for Exit {}

src/reactor/kqueue.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use polling::{Event, PollMode, Poller};
77

88
use std::fmt;
99
use std::io::Result;
10+
use std::num::NonZeroI32;
1011
use std::os::unix::io::{AsRawFd, BorrowedFd, RawFd};
11-
use std::process::Child;
1212

1313
/// The raw registration into the reactor.
1414
///
@@ -27,8 +27,8 @@ pub enum Registration {
2727
/// Raw signal number for signal delivery.
2828
Signal(Signal),
2929

30-
/// Process for process termination.
31-
Process(Child),
30+
/// Pid for process termination.
31+
Process(NonZeroI32),
3232
}
3333

3434
impl fmt::Debug for Registration {
@@ -62,8 +62,8 @@ impl Registration {
6262
Self::Signal(signal) => {
6363
poller.add_filter(PollSignal(signal.0), token, PollMode::Oneshot)
6464
}
65-
Self::Process(process) => poller.add_filter(
66-
unsafe { Process::new(process, ProcessOps::Exit) },
65+
Self::Process(pid) => poller.add_filter(
66+
unsafe { Process::from_pid(*pid, ProcessOps::Exit) },
6767
token,
6868
PollMode::Oneshot,
6969
),
@@ -82,8 +82,8 @@ impl Registration {
8282
Self::Signal(signal) => {
8383
poller.modify_filter(PollSignal(signal.0), interest.key, PollMode::Oneshot)
8484
}
85-
Self::Process(process) => poller.modify_filter(
86-
unsafe { Process::new(process, ProcessOps::Exit) },
85+
Self::Process(pid) => poller.modify_filter(
86+
unsafe { Process::from_pid(*pid, ProcessOps::Exit) },
8787
interest.key,
8888
PollMode::Oneshot,
8989
),
@@ -100,8 +100,8 @@ impl Registration {
100100
poller.delete(fd)
101101
}
102102
Self::Signal(signal) => poller.delete_filter(PollSignal(signal.0)),
103-
Self::Process(process) => {
104-
poller.delete_filter(unsafe { Process::new(process, ProcessOps::Exit) })
103+
Self::Process(pid) => {
104+
poller.delete_filter(unsafe { Process::from_pid(*pid, ProcessOps::Exit) })
105105
}
106106
}
107107
}

0 commit comments

Comments
 (0)