# Why doesn't exit unwind?

**URL:** <https://internals.rust-lang.org/t/why-doesnt-exit-unwind/1806>\
**Category:** libs\
**Created:** [April 3, 2015, 2:20pm UTC](https://internals.rust-lang.org/t/why-doesnt-exit-unwind/1806 "2015-04-03T14:20:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stebalien](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/stebalien/32/228_2.png) [@stebalien](https://internals.rust-lang.org/u/stebalien)\
**Post date:** [April 3, 2015, 2:20pm UTC](https://internals.rust-lang.org/t/why-doesnt-exit-unwind/1806/1 "2015-04-03T14:20:43Z")

</div>

It seems a little odd that a safe function will bypass destructors. Is there any reason `std::process::exit` doesn’t unwind the stack? Personally, I think we need two functions:

```rust
fn std::process::exit(code: i32);
unsafe fn std::process:exit_now(code: i32);

```

---

<div class="post-metadata">

**Author:** ![alexcrichton](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/alexcrichton/32/4501_2.png) [@alexcrichton](https://internals.rust-lang.org/u/alexcrichton)\
**Post date:** [April 3, 2015, 3:54pm UTC](https://internals.rust-lang.org/t/why-doesnt-exit-unwind/1806/2 "2015-04-03T15:54:40Z")

</div>

It is currently not considered `unsafe` to leak memory or resources in Rust. For example:

- You can trivially create a cycle using `Rc` + `RefCell` that will leak and never destroy its contents
- You can exit the main thread while other threads are running, not running their destructors

As a result, not running destructors is not considered `unsafe`, so this function was introduced as safe

---

<div class="post-metadata">

**Author:** ![stebalien](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/stebalien/32/228_2.png) [@stebalien](https://internals.rust-lang.org/u/stebalien)\
**Post date:** [April 3, 2015, 4:32pm UTC](https://internals.rust-lang.org/t/why-doesnt-exit-unwind/1806/3 "2015-04-03T16:32:52Z")

</div>

Got it. I always forget that `unsafe` only refers to memory access, not general behavior.

> As a result, not running destructors is not considered unsafe, so this function was introduced as safe.

Just for complete correctness in case anyone comes across this thread later ~~, this isn't always true. Failing to call a destructor before freeing or failing to call a destructor on drop is unsafe.~~ there is no guarantee that a destructor will ever be called.

---

<div class="post-metadata">

**Author:** ![lambda-fairy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/lambda-fairy/32/2045_2.png) [@lambda-fairy](https://internals.rust-lang.org/u/lambda-fairy)\
**Post date:** [April 14, 2015, 10:42am UTC](https://internals.rust-lang.org/t/why-doesnt-exit-unwind/1806/4 "2015-04-14T10:42:45Z")

</div>

Unsafe or not, the design of this function seems off to me. What’s the reason behind not calling destructors by default?

I see two distinct use cases here:

1. Choosing a custom error code

2. Aborting the program without doing any cleanup

The current `exit()` conflates these two cases, when most of the time you only want the first.

---

<div class="post-metadata">

**Author:** ![dwrensha](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dwrensha/32/371_2.png) [@dwrensha](https://internals.rust-lang.org/u/dwrensha)\
**Post date:** [April 14, 2015, 11:48am UTC](https://internals.rust-lang.org/t/why-doesnt-exit-unwind/1806/5 "2015-04-14T11:48:40Z")

</div>

For what it’s worth, the [KJ](https://github.com/sandstorm-io/capnproto/tree/master/c%2B%2B/src/kj) C++ library also encourages RAII style together with a non-unwinding `exit()`, and its documentation gives some [well thought out justification](https://github.com/sandstorm-io/capnproto/blob/0d6b9704e6ba30ee09c167d5032991a4f9c39023/c%2B%2B/src/kj/main.h#L43).

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:24am UTC](https://internals.rust-lang.org/t/why-doesnt-exit-unwind/1806/6 "2019-03-25T08:24:10Z")

</div>

Closed on March 25, 2019, 8:24am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
