about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-06-16 18:46:45 +0000
committerbors <bors@rust-lang.org>2014-06-16 18:46:45 +0000
commit0973eb4419d0598c1134106adef2ee8dc2a2b5ff (patch)
tree104b15384719ebd66718f846f9dbeedaed85e0d0 /src/libstd
parent4b672a63da3083ab6d4673cdb223bc1c978ed7d2 (diff)
parent2fe926431bb198a052a5eae92ff820a4f572fd92 (diff)
downloadrust-0973eb4419d0598c1134106adef2ee8dc2a2b5ff.tar.gz
rust-0973eb4419d0598c1134106adef2ee8dc2a2b5ff.zip
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
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/io/process.rs23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/libstd/io/process.rs b/src/libstd/io/process.rs
index 38d8475ddf7..1dcf08b2322 100644
--- a/src/libstd/io/process.rs
+++ b/src/libstd/io/process.rs
@@ -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`.
@@ -262,6 +263,7 @@ impl Command {
                 });
                 Process {
                     handle: p,
+                    forget: false,
                     stdin: io.next().unwrap(),
                     stdout: io.next().unwrap(),
                     stderr: io.next().unwrap(),
@@ -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
+    /// 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());
@@ -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());
+    })
 }