fix exiting breakpoint interactive context

Commit 8d27f81a to change how background jobs are handled (killed rather
than left running) when the shell is exited did not correctly handle
the nested interactive context created by the `breakpoint` command. This
fixes that mistake. Now any background jobs that already existed, or were
created within the `breakpoint` context, are left running when exiting
that context.
This commit is contained in:
Kurtis Rader 2016-12-19 20:35:57 -08:00
parent 8d27f81a7b
commit 9870cf838a

View file

@ -14,29 +14,30 @@
#include "config.h" #include "config.h"
// IWYU pragma: no_include <type_traits> // IWYU pragma: no_include <type_traits>
#include <assert.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include <pthread.h> #include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#include <wctype.h>
#include <algorithm>
#include <stack>
#ifdef HAVE_SIGINFO_H #ifdef HAVE_SIGINFO_H
#include <siginfo.h> #include <siginfo.h>
#endif #endif
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_SYS_SELECT_H #ifdef HAVE_SYS_SELECT_H
#include <sys/select.h> #include <sys/select.h>
#endif #endif
#include <assert.h> #include <sys/time.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h> #include <sys/types.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#include <wchar.h> #include <wchar.h>
#include <wctype.h>
#include <algorithm>
#include <stack>
#include <memory> #include <memory>
#include "color.h" #include "color.h"
@ -2222,17 +2223,18 @@ bool shell_is_exiting() {
/// This function is called when the main loop notices that end_loop has been set while in /// This function is called when the main loop notices that end_loop has been set while in
/// interactive mode. It checks if it is ok to exit. /// interactive mode. It checks if it is ok to exit.
static void handle_end_loop() { static void handle_end_loop() {
bool bg_jobs = false; if (!reader_exit_forced()) {
bool is_breakpoint = false;
const parser_t &parser = parser_t::principal_parser(); const parser_t &parser = parser_t::principal_parser();
for (size_t i = 0; i < parser.block_count(); i++) { for (size_t i = 0; i < parser.block_count(); i++) {
if (parser.block_at_index(i)->type() == BREAKPOINT) { if (parser.block_at_index(i)->type() == BREAKPOINT) {
is_breakpoint = true; // We're executing within a breakpoint so we do not want to terminate the shell and
break; // kill background jobs.
return;
}
} }
} }
bool bg_jobs = false;
job_iterator_t jobs; job_iterator_t jobs;
while (job_t *j = jobs.next()) { while (job_t *j = jobs.next()) {
if (!job_is_completed(j)) { if (!job_is_completed(j)) {
@ -2241,21 +2243,22 @@ static void handle_end_loop() {
} }
} }
if (!reader_exit_forced() && !data->prev_end_loop && bg_jobs && !is_breakpoint) { if (!data->prev_end_loop && bg_jobs) {
writestr(_(L"There are stopped or running jobs.\n")); writestr(_(L"There are stopped or running jobs.\n"));
writestr(_(L"A second attempt to exit will force their termination.\n")); writestr(_(L"A second attempt to exit will force their termination.\n"));
reader_exit(0, 0); reader_exit(0, 0);
data->prev_end_loop = 1; data->prev_end_loop = 1;
} else { return;
// Kill background jobs. }
job_iterator_t jobs;
// Kill background jobs before exiting.
jobs.reset();
while (job_t *j = jobs.next()) { while (job_t *j = jobs.next()) {
if (!job_is_completed(j)) { if (!job_is_completed(j)) {
if (job_is_stopped(j)) job_signal(j, SIGCONT); if (job_is_stopped(j)) job_signal(j, SIGCONT);
job_signal(j, SIGHUP); job_signal(j, SIGHUP);
} }
} }
}
} }
static bool selection_is_at_top() { static bool selection_is_at_top() {