@@ -19,6 +19,7 @@ extern crate tracing;
1919
2020use rustc_ast as ast;
2121use rustc_codegen_ssa:: { traits:: CodegenBackend , CodegenErrors , CodegenResults } ;
22+ use rustc_const_eval:: CTRL_C_RECEIVED ;
2223use rustc_data_structures:: profiling:: {
2324 get_resident_set_size, print_time_passes_entry, TimePassesFormat ,
2425} ;
@@ -1518,6 +1519,22 @@ pub fn init_logger(early_dcx: &EarlyDiagCtxt, cfg: rustc_log::LoggerConfig) {
15181519 }
15191520}
15201521
1522+ /// Install our usual `ctrlc` handler, which sets [`rustc_const_eval::CTRL_C_RECEIVED`].
1523+ /// Making this handler optional lets tools can install a different handler, if they wish.
1524+ pub fn install_ctrlc_handler( ) {
1525+ ctrlc:: set_handler( move || {
1526+ // Indicate that we have been signaled to stop. If we were already signaled, exit
1527+ // immediately. In our interpreter loop we try to consult this value often, but if for
1528+ // whatever reason we don't get to that check or the cleanup we do upon finding that
1529+ // this bool has become true takes a long time, the exit here will promptly exit the
1530+ // process on the second Ctrl-C.
1531+ if CTRL_C_RECEIVED . swap( true , Ordering :: Relaxed ) {
1532+ std:: process:: exit( 1 ) ;
1533+ }
1534+ } )
1535+ . expect( "Unable to install ctrlc handler" ) ;
1536+ }
1537+
15211538pub fn main( ) -> ! {
15221539 let start_time = Instant :: now( ) ;
15231540 let start_rss = get_resident_set_size( ) ;
@@ -1528,6 +1545,8 @@ pub fn main() -> ! {
15281545 signal_handler:: install( ) ;
15291546 let mut callbacks = TimePassesCallbacks :: default ( ) ;
15301547 let using_internal_features = install_ice_hook( DEFAULT_BUG_REPORT_URL , |_| ( ) ) ;
1548+ install_ctrlc_handler( ) ;
1549+
15311550 let exit_code = catch_with_exit_code( || {
15321551 RunCompiler :: new( & args:: raw_args( & early_dcx) ?, & mut callbacks)
15331552 . set_using_internal_features( using_internal_features)
0 commit comments