Skip to content

Commit 117bc47

Browse files
Rollup merge of rust-lang#157360 - sanidhyasin:document-command-spawn-errors, r=LawnGnome
Document error conditions for `Command::{spawn, output, status}` Fixes rust-lang#150361. `Command::spawn`, `Command::output`, and `Command::status` all return `io::Result`, but the docs never explain *why* they can fail, and they do not mention the (easy to get wrong) distinction between a failure to spawn and a child that runs but exits unsuccessfully or is killed by a signal. This adds an `# Errors` section to each method: - **`spawn`** describes the common reasons spawning fails — the program not being found, lacking permission to execute it (e.g. not executable, or blocked by a policy such as `seccomp`), and the OS being unable to create the process due to resource exhaustion. It also clarifies that an error is only returned for failures *while spawning*; once the child has started, anything that happens to it (including signals) is reported through its `ExitStatus`. - **`output`** and **`status`** refer back to `spawn` for the spawn failures, and explicitly note that a child which exits unsuccessfully or is terminated by a signal is **not** an error — they still return `Ok`, with the outcome reflected in the resulting `ExitStatus`. The error conditions and the signal/`ExitStatus` behavior were confirmed by `@bjorn3` in the issue discussion. Docs-only change; no code blocks were added or modified, and all intra-doc links (`io::Error`, `ExitStatus`, `Output`, `spawn`) follow link patterns already used in this module. r? libs
2 parents 7e5b58e + b82f09a commit 117bc47

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

library/std/src/process.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,26 @@ impl Command {
10621062
///
10631063
/// By default, stdin, stdout and stderr are inherited from the parent.
10641064
///
1065+
/// # Errors
1066+
///
1067+
/// This method returns an [`io::Error`] if the child process could not be
1068+
/// spawned. Common reasons include:
1069+
///
1070+
/// * the program could not be found (for example, it does not exist, or,
1071+
/// when given a bare name, it is not present in the `PATH`);
1072+
/// * the current process does not have permission to execute the program
1073+
/// (for example, the file is not marked executable, or execution is
1074+
/// denied by a security policy such as `seccomp`);
1075+
/// * the operating system could not create the new process because of
1076+
/// resource exhaustion (for example, a limit on the number of processes
1077+
/// was reached).
1078+
///
1079+
/// An error is only returned for failures that occur while the child is
1080+
/// being spawned. Once the child has started successfully, anything that
1081+
/// happens to it afterwards — including being terminated by a signal — is
1082+
/// reported through its [`ExitStatus`] rather than as an error from the
1083+
/// spawning method.
1084+
///
10651085
/// # Examples
10661086
///
10671087
/// ```no_run
@@ -1084,6 +1104,20 @@ impl Command {
10841104
/// attempt by the child process to read from the stdin stream will result
10851105
/// in the stream immediately closing.
10861106
///
1107+
/// # Errors
1108+
///
1109+
/// Like [`spawn`], this method returns an [`io::Error`] if the child
1110+
/// process could not be spawned; see [`spawn`] for the common reasons. It
1111+
/// may also return an error if reading the child's output or waiting on the
1112+
/// child fails.
1113+
///
1114+
/// Note that this method does **not** return an error if the child runs and
1115+
/// then exits unsuccessfully, or is terminated by a signal. In those cases
1116+
/// it still returns [`Ok`], and the outcome is reflected in the
1117+
/// [`ExitStatus`] stored in the returned [`Output`].
1118+
///
1119+
/// [`spawn`]: Command::spawn
1120+
///
10871121
/// # Examples
10881122
///
10891123
/// ```should_panic
@@ -1111,6 +1145,19 @@ impl Command {
11111145
///
11121146
/// By default, stdin, stdout and stderr are inherited from the parent.
11131147
///
1148+
/// # Errors
1149+
///
1150+
/// Like [`spawn`], this method returns an [`io::Error`] if the child
1151+
/// process could not be spawned; see [`spawn`] for the common reasons. It
1152+
/// may also return an error if waiting on the child fails.
1153+
///
1154+
/// Note that this method does **not** return an error if the child runs and
1155+
/// then exits unsuccessfully, or is terminated by a signal. In those cases
1156+
/// it still returns [`Ok`], and the outcome is reflected in the returned
1157+
/// [`ExitStatus`].
1158+
///
1159+
/// [`spawn`]: Command::spawn
1160+
///
11141161
/// # Examples
11151162
///
11161163
/// ```should_panic

0 commit comments

Comments
 (0)