Skip to content

Commit 6f1014f

Browse files
committed
Auto merge of #28069 - alexcrichton:rt-atexit, r=brson
This adds a call to `rt::cleanup` on `process::exit` to make sure we clean up after ourselves on the way out from Rust. Closes #28065
2 parents b4de424 + 04c09f9 commit 6f1014f

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
lines changed

src/libstd/process.rs

+1
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ impl Child {
582582
/// to run.
583583
#[stable(feature = "rust1", since = "1.0.0")]
584584
pub fn exit(code: i32) -> ! {
585+
::rt::cleanup();
585586
::sys::os::exit(code)
586587
}
587588

src/libstd/rt/args.rs

-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ mod imp {
6464

6565
pub unsafe fn cleanup() {
6666
take();
67-
LOCK.destroy();
6867
}
6968

7069
pub fn take() -> Option<Vec<Vec<u8>>> {

src/libstd/rt/at_exit_imp.rs

-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
//!
1313
//! Documentation can be found on the `rt::at_exit` function.
1414
15-
// FIXME: switch this to use atexit. Currently this
16-
// segfaults (the queue's memory is mysteriously gone), so
17-
// instead the cleanup is tied to the `std::rt` entry point.
18-
1915
use alloc::boxed::FnBox;
2016
use boxed::Box;
2117
use ptr;

src/libstd/rt/mod.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#![allow(missing_docs)]
2424

2525
use prelude::v1::*;
26+
use sync::Once;
2627
use sys;
2728
use thread;
2829

@@ -124,16 +125,11 @@ pub fn at_exit<F: FnOnce() + Send + 'static>(f: F) -> Result<(), ()> {
124125
}
125126

126127
/// One-time runtime cleanup.
127-
///
128-
/// This function is unsafe because it performs no checks to ensure that the
129-
/// runtime has completely ceased running. It is the responsibility of the
130-
/// caller to ensure that the runtime is entirely shut down and nothing will be
131-
/// poking around at the internal components.
132-
///
133-
/// Invoking cleanup while portions of the runtime are still in use may cause
134-
/// undefined behavior.
135-
pub unsafe fn cleanup() {
136-
args::cleanup();
137-
sys::stack_overflow::cleanup();
138-
at_exit_imp::cleanup();
128+
pub fn cleanup() {
129+
static CLEANUP: Once = Once::new();
130+
CLEANUP.call_once(|| unsafe {
131+
args::cleanup();
132+
sys::stack_overflow::cleanup();
133+
at_exit_imp::cleanup();
134+
});
139135
}

src/test/run-pass/exit-flushes.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::env;
12+
use std::process::{exit, Command};
13+
14+
fn main() {
15+
if env::args().len() > 1 {
16+
print!("hello!");
17+
exit(0);
18+
} else {
19+
let out = Command::new(env::args().next().unwrap()).arg("foo")
20+
.output().unwrap();
21+
assert!(out.status.success());
22+
assert_eq!(String::from_utf8(out.stdout).unwrap(), "hello!");
23+
assert_eq!(String::from_utf8(out.stderr).unwrap(), "");
24+
}
25+
}

0 commit comments

Comments
 (0)