Skip to content

Commit 0973eb4

Browse files
committed
auto merge of #14551 : alexcrichton/rust/issue-14467, r=kballard
Forking off a child which survives the parent is often a useful task, and is currently not possible because the Process type will invoke `wait()` in its destructor in order to prevent leaking resources. This function adds a new safe method, `daemonize`, which can be used to consume an instance of `Process` which will then not call `wait` in the destructor. This new method is clearly documented as a leak of resources, but it must be forcibly opted in to. Closes #14467
2 parents 4b672a6 + 2fe9264 commit 0973eb4

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/libstd/io/process.rs

+23
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ use c_str::CString;
5959
/// ```
6060
pub struct Process {
6161
handle: Box<RtioProcess + Send>,
62+
forget: bool,
6263

6364
/// Handle to the child's stdin, if the `stdin` field of this process's
6465
/// `ProcessConfig` was `CreatePipe`. By default, this handle is `Some`.
@@ -262,6 +263,7 @@ impl Command {
262263
});
263264
Process {
264265
handle: p,
266+
forget: false,
265267
stdin: io.next().unwrap(),
266268
stdout: io.next().unwrap(),
267269
stderr: io.next().unwrap(),
@@ -540,10 +542,23 @@ impl Process {
540542
error: stderr.recv().ok().unwrap_or(Vec::new()),
541543
})
542544
}
545+
546+
/// Forgets this process, allowing it to outlive the parent
547+
///
548+
/// This function will forcefully prevent calling `wait()` on the child
549+
/// process in the destructor, allowing the child to outlive the
550+
/// parent. Note that this operation can easily lead to leaking the
551+
/// resources of the child process, so care must be taken when
552+
/// invoking this method.
553+
pub fn forget(mut self) {
554+
self.forget = true;
555+
}
543556
}
544557

545558
impl Drop for Process {
546559
fn drop(&mut self) {
560+
if self.forget { return }
561+
547562
// Close all I/O before exiting to ensure that the child doesn't wait
548563
// forever to print some text or something similar.
549564
drop(self.stdin.take());
@@ -933,4 +948,12 @@ mod tests {
933948
rx.recv();
934949
rx.recv();
935950
})
951+
952+
iotest!(fn forget() {
953+
let p = sleeper();
954+
let id = p.id();
955+
p.forget();
956+
assert!(Process::kill(id, 0).is_ok());
957+
assert!(Process::kill(id, PleaseExitSignal).is_ok());
958+
})
936959
}

0 commit comments

Comments
 (0)