Skip to content

std: Run at_exit cleanup on process::exit #28069

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

Merged
merged 1 commit into from
Sep 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libstd/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ impl Child {
/// to run.
#[stable(feature = "rust1", since = "1.0.0")]
pub fn exit(code: i32) -> ! {
::rt::cleanup();
::sys::os::exit(code)
}

Expand Down
1 change: 0 additions & 1 deletion src/libstd/rt/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ mod imp {

pub unsafe fn cleanup() {
take();
LOCK.destroy();
}

pub fn take() -> Option<Vec<Vec<u8>>> {
Expand Down
4 changes: 0 additions & 4 deletions src/libstd/rt/at_exit_imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
//!
//! Documentation can be found on the `rt::at_exit` function.

// FIXME: switch this to use atexit. Currently this
// segfaults (the queue's memory is mysteriously gone), so
// instead the cleanup is tied to the `std::rt` entry point.

use alloc::boxed::FnBox;
use boxed::Box;
use sys_common::mutex::Mutex;
Expand Down
20 changes: 8 additions & 12 deletions src/libstd/rt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#![allow(missing_docs)]

use prelude::v1::*;
use sync::Once;
use sys;
use thread;

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

/// One-time runtime cleanup.
///
/// This function is unsafe because it performs no checks to ensure that the
/// runtime has completely ceased running. It is the responsibility of the
/// caller to ensure that the runtime is entirely shut down and nothing will be
/// poking around at the internal components.
///
/// Invoking cleanup while portions of the runtime are still in use may cause
/// undefined behavior.
pub unsafe fn cleanup() {
args::cleanup();
sys::stack_overflow::cleanup();
at_exit_imp::cleanup();
pub fn cleanup() {
static CLEANUP: Once = Once::new();
CLEANUP.call_once(|| unsafe {
args::cleanup();
sys::stack_overflow::cleanup();
at_exit_imp::cleanup();
});
}
25 changes: 25 additions & 0 deletions src/test/run-pass/exit-flushes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::env;
use std::process::{exit, Command};

fn main() {
if env::args().len() > 1 {
print!("hello!");
exit(0);
} else {
let out = Command::new(env::args().next().unwrap()).arg("foo")
.output().unwrap();
assert!(out.status.success());
assert_eq!(String::from_utf8(out.stdout).unwrap(), "hello!");
assert_eq!(String::from_utf8(out.stderr).unwrap(), "");
}
}