Skip to content

std: Support consuming a Process without waiting #14551

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

Merged
merged 1 commit into from
Jun 16, 2014
Merged
Changes from all 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
23 changes: 23 additions & 0 deletions src/libstd/io/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ use c_str::CString;
/// ```
pub struct Process {
handle: Box<RtioProcess + Send>,
forget: bool,

/// Handle to the child's stdin, if the `stdin` field of this process's
/// `ProcessConfig` was `CreatePipe`. By default, this handle is `Some`.
Expand Down Expand Up @@ -262,6 +263,7 @@ impl Command {
});
Process {
handle: p,
forget: false,
stdin: io.next().unwrap(),
stdout: io.next().unwrap(),
stderr: io.next().unwrap(),
Expand Down Expand Up @@ -540,10 +542,23 @@ impl Process {
error: stderr.recv().ok().unwrap_or(Vec::new()),
})
}

/// Forgets this process, allowing it to outlive the parent
///
/// This function will forcefully prevent calling `wait()` on the child
Copy link
Contributor

Choose a reason for hiding this comment

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

What makes it forceful?

/// process in the destructor, allowing the child to outlive the
/// parent. Note that this operation can easily lead to leaking the
/// resources of the child process, so care must be taken when
/// invoking this method.
pub fn forget(mut self) {
self.forget = true;
}
}

impl Drop for Process {
fn drop(&mut self) {
if self.forget { return }

// Close all I/O before exiting to ensure that the child doesn't wait
// forever to print some text or something similar.
drop(self.stdin.take());
Expand Down Expand Up @@ -933,4 +948,12 @@ mod tests {
rx.recv();
rx.recv();
})

iotest!(fn forget() {
let p = sleeper();
let id = p.id();
p.forget();
assert!(Process::kill(id, 0).is_ok());
assert!(Process::kill(id, PleaseExitSignal).is_ok());
})
}