Skip to content
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

RFC: Add std::process::exit #1011

Merged
merged 5 commits into from
Mar 31, 2015
Merged

Conversation

alexcrichton
Copy link
Member

Add a new function to the std::process module to exit the process immediately
with a specified exit code.

Rendered

Add a new function to the `std::process` module to exit the process immediately
with a specified exit code.
[win]: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682658%28v=vs.85%29.aspx

This function is also not marked `unsafe`, despite the risk of leaking
allocated resources (e.g. destructor smany not be run). It is already possible
Copy link
Member

Choose a reason for hiding this comment

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

s/destructor smany/destructors may/

@alexcrichton alexcrichton self-assigned this Mar 25, 2015

* To what degree should the documentation imply that `rt::at_exit` handlers are
run? Implementation-wise their execution is guaranteed, but we may not wish
for this to always be so.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should guarantee it. If we wish to make this not necessarily be true in the future, then I think that calls for adding a second function that explicitly bypasses rt::at_exit (and consequently is marked unsafe).

@lilyball
Copy link
Contributor

I'm sympathetic to the idea that we should let main() be optionally typed as fn() -> i32, but even if we decided to do that, we'd still have a use for std::process::exit().

@lilyball
Copy link
Contributor

👍, although I find it mildly odd to put this in std::process, because everything else in that module is for dealing with child processes, not for dealing with the current process. That said, std::env::exit() would also be slightly odd, and I can't think of any other better place, so I guess std::process is fine.

@alexcrichton
Copy link
Member Author

but even if we decided to do that, we'd still have a use for std::process::exit()

This is a good point, and I feel the same way! I'll add some text related to this in the RFC.

I find it mildly odd to put this in std::process, because everything else in that module is for dealing with child processes, not for dealing with the current process.

Our plan is to expand the module to also allow inspection of remote processes, sending signals, etc. In that sense it may not always be specifically about child processes. I also consider process::exit along the lines of thread::sleep where the top level functions are affecting the current process or current thread.

@blaenk
Copy link
Contributor

blaenk commented Mar 26, 2015

yes please

@nagisa
Copy link
Member

nagisa commented Mar 26, 2015

Since we’re adding a function for this (yay), we should also guarantee at_exit-registered functions are executed.

EDIT: std::rt::at_exit, of course.
EDIT 2: sigh, this is what I get by not reading the full RFC before commenting…

that status (e.g. Rust was called from C).

The purpose of this RFC is to provide at least one method on the path to
stabilization which will provide a method to exit a process with a nonzero exit
Copy link
Member

Choose a reason for hiding this comment

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

s/a nonzero/an arbitrary/, or, perhaps, there’s any reason why calling exit(0) is invalid?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah yes this is definitely meant to allow exit(0), just botched the wording ab it.

@alexcrichton
Copy link
Member Author

@nagisa yeah I'm not quite sure how to word the documentation given that rt::at_exit is #[unstable] right now. For now I don't think we buy much providing a hard guarantee that those closures are run (due to it not mattering to otherwise-stable code). I do think, however, that this should definitely play into the stabilization of std::rt::at_exit!

@quantheory
Copy link
Contributor

Hmm, there's another major motivation for this that I can think of, which is ending the process without risking unwinding across an FFI boundary, e.g. in a Rust function that has been passed to a C library as a callback.

IIRC, unwinding past an FFI boundary is undefined behavior, but we don't have a very solid recommendation for what you should actually do if you need to terminate the process and a foreign function is (or may be) on the stack. About the only thing that you can do to prevent this right now is either call the libc::exit, or maybe try to stop the unwinding by triggering a double panic in a destructor in code nearest to the FFI?

Edit: I guess that the RFC obliquely mentions this. I guess my point is that this would be a reasonable justification for having an exit in std somewhere even if the exit code wasn't an issue. Unrelatedly, I also thought that putting it in process was a bit odd. I guess I would have expected it in env, since process contains more stuff related to IPC, whereas operations related to the current process are in env (e.g. set_var and set_current_dir).

alexcrichton added a commit to alexcrichton/rust that referenced this pull request Mar 31, 2015
This commit is an implementation of [RFC rust-lang#1011][rfc] which adds an `exit`
function to the standard library for immediately terminating the current process
with a specified exit code.

[rfc]: rust-lang/rfcs#1011
@aturon aturon merged commit fc47393 into rust-lang:master Mar 31, 2015
@aturon
Copy link
Member

aturon commented Mar 31, 2015

This was discussed at today's meeting which echoed the support found on this RFC thread. This API is clearly important to provide in some form, and there's little question as to its API. Landing it now will make it possible to yield an exit code without having to stabilize the problematic set_exit_code.

Tracking issue

alexcrichton added a commit to alexcrichton/rust that referenced this pull request Mar 31, 2015
This commit is an implementation of [RFC rust-lang#1011][rfc] which adds an `exit`
function to the standard library for immediately terminating the current process
with a specified exit code.

[rfc]: rust-lang/rfcs#1011

Closes rust-lang#23914
@Centril Centril added the A-process Proposals relating to spawning and communicating with processes. label Nov 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-process Proposals relating to spawning and communicating with processes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants