Skip to content

Commit c957cec

Browse files
committed
Improve exit behavior
This makes so that nushell kills all active jobs when exiting in both interactive and noninteractive form, including when exiting from natural cause (i.e `main` function return).
1 parent 8676dc9 commit c957cec

File tree

5 files changed

+25
-17
lines changed

5 files changed

+25
-17
lines changed

crates/nu-cli/src/repl.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,8 @@ fn loop_iteration(ctx: LoopContext) -> (bool, Stack, Reedline) {
697697

698698
cleanup_exit((), engine_state, 0);
699699

700-
return (false, stack, line_editor);
700+
// if cleanup_exit didn't exit, we should keep running
701+
return (true, stack, line_editor);
701702
}
702703
Err(err) => {
703704
let message = err.to_string();

crates/nu-command/src/experimental/job_spawn.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ impl Command for JobSpawn {
7979
.run_with_input(Value::nothing(head).into_pipeline_data())
8080
.and_then(|data| data.into_value(head))
8181
.unwrap_or_else(|err| {
82-
8382
if !job_state.signals().interrupted() {
8483
report_shell_error(&job_state, &err);
8584
}

crates/nu-engine/src/exit.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,26 @@ use nu_protocol::engine::EngineState;
1212
pub fn cleanup_exit<T>(tag: T, engine_state: &EngineState, exit_code: i32) -> T {
1313
let mut jobs = engine_state.jobs.lock().expect("failed to lock job table");
1414

15-
if engine_state.is_interactive && jobs.iter().next().is_some() {
16-
if engine_state.exit_warning_given.load(Ordering::SeqCst) {
17-
let _ = jobs.kill_all();
18-
} else {
19-
let job_count = jobs.iter().count();
15+
if engine_state.is_interactive
16+
&& jobs.iter().next().is_some()
17+
&& !engine_state.exit_warning_given.load(Ordering::SeqCst)
18+
{
19+
let job_count = jobs.iter().count();
2020

21-
println!("There are still {} background jobs running.", job_count);
21+
println!("There are still background jobs running ({}).", job_count);
2222

23-
println!("Running `exit` a second time will kill all of them.");
23+
println!("Running `exit` a second time will kill all of them.");
2424

25-
engine_state
26-
.exit_warning_given
27-
.store(true, Ordering::SeqCst);
25+
engine_state
26+
.exit_warning_given
27+
.store(true, Ordering::SeqCst);
2828

29-
return tag;
30-
}
29+
return tag;
3130
}
3231

32+
let _ = jobs.kill_all();
33+
3334
drop(tag);
35+
3436
std::process::exit(exit_code);
3537
}

crates/nu-protocol/src/engine/jobs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl Default for Jobs {
2020
Self {
2121
next_job_id: 1,
2222
last_frozen_job_id: None,
23-
jobs: HashMap::default()
23+
jobs: HashMap::default(),
2424
}
2525
}
2626
}

src/main.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use command::gather_commandline_args;
2121
use log::{trace, Level};
2222
use miette::Result;
2323
use nu_cli::gather_parent_env_vars;
24-
use nu_engine::convert_env_values;
24+
use nu_engine::{convert_env_values, exit::cleanup_exit};
2525
use nu_lsp::LanguageServer;
2626
use nu_path::canonicalize_with;
2727
use nu_protocol::{
@@ -489,6 +489,8 @@ fn main() -> Result<()> {
489489
input,
490490
entire_start_time,
491491
);
492+
493+
cleanup_exit(0, &engine_state, 0);
492494
} else if !script_name.is_empty() {
493495
run_file(
494496
&mut engine_state,
@@ -499,6 +501,8 @@ fn main() -> Result<()> {
499501
args_to_script,
500502
input,
501503
);
504+
505+
cleanup_exit(0, &engine_state, 0);
502506
} else {
503507
// Environment variables that apply only when in REPL
504508
engine_state.add_env_var("PROMPT_INDICATOR".to_string(), Value::test_string("> "));
@@ -534,7 +538,9 @@ fn main() -> Result<()> {
534538
stack,
535539
parsed_nu_cli_args,
536540
entire_start_time,
537-
)?
541+
)?;
542+
543+
cleanup_exit(0, &engine_state, 0);
538544
}
539545

540546
Ok(())

0 commit comments

Comments
 (0)